tag:blogger.com,1999:blog-6954163082309450632024-02-07T05:51:44.428-08:00Vbscript TutorialSagarhttp://www.blogger.com/profile/06686177535004668246noreply@blogger.comBlogger32125tag:blogger.com,1999:blog-695416308230945063.post-20132376203233065252014-03-29T07:52:00.001-07:002014-04-03T03:58:44.558-07:00Functions in VBScript<div dir="ltr" style="text-align: left;" trbidi="on">
Functions are used to perform the specific task.Functions are used to increase the reusability of the code. Functions are similar to the procedures except one difference that functions can return value.<br />
<br />
<h2 style="text-align: left;">
Simple example of the function</h2>
<br />
Suppose you want to find the sum of 2 numbers. Without functions, you will write below code.<br />
<br />
<pre>a = 10
b = 20
c = a+ b
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> c</pre>
<br />
To add another 2 different numbers, you will use below code<br />
<br />
<pre>a = 33
b = 65
c = a+ b
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> c</pre>
<br />
In above examples, we have to add 2 numbers. So the operation is same (Repeating) So we can write the function which will take 2 parameters as shown below.<br />
<br />
<pre><span style="color: #7f0055; font-weight: bold;">function</span> sum(<span style="color: #7f0055; font-weight: bold;">byref</span> a, <span style="color: #7f0055; font-weight: bold;">byref</span> b)
sum = <span style="color: #7f0055; font-weight: bold;">cint</span>(a) + <span style="color: #7f0055; font-weight: bold;">cint</span>(b)
<span style="color: #7f0055; font-weight: bold;">end</span> <span style="color: #7f0055; font-weight: bold;">function</span></pre>
<br />
<h2 style="text-align: left;">
Calling functions in VBScript</h2>
To call the procedures we can use below statements.<br />
<br />
<pre>c = sum(10,20)
<span style="color: #7f0055; font-weight: bold;">msgbox</span> c
c = sum(33,65)
<span style="color: #7f0055; font-weight: bold;">msgbox</span> c</pre>
<br />
Thus we can call functions many times in the code. This reduces the lines of code as well as maintainability of the code.<br />
<h2 style="text-align: left;">
Passing arguments to the function</h2>
<div>
We can pass the arguments to the function by 2 ways.</div>
<div>
<ol>
<li>Pass by reference </li>
<li>Pass by value</li>
</ol>
<div>
<div>
<b><u>Pass by reference</u></b></div>
<div>
<br /></div>
<div>
By default, values are passed to the function by reference. When we pass the values by reference the changes made to the variables in the called function are reflected in the calling function/procedure.</div>
<div>
<br /></div>
<div>
<pre>a = 10
c = findsqr(a)
<span style="color: #7f0055; font-weight: bold;">msgbox</span> a <span style="color: #3f7f59;">'prints 100</span>
<span style="color: #7f0055; font-weight: bold;">msgbox</span> c <span style="color: #3f7f59;">'prints 100</span>
<span style="color: #7f0055; font-weight: bold;">function</span> findsqr(<span style="color: #7f0055; font-weight: bold;">byref</span> a)
a = a*a
findsqr = a
<span style="color: #7f0055; font-weight: bold;">end</span> <span style="color: #7f0055; font-weight: bold;">function</span></pre>
</div>
</div>
</div>
<div>
<br /></div>
<br />
<div>
<div>
<b><u>Pass by value</u></b></div>
<div>
<br /></div>
<div>
When we pass the values by value, the changes made to the variables in the called function are not reflected in the calling procedure or function.</div>
<div>
<br /></div>
<div>
<pre>a = 10
c = findsqr(a)
<span style="color: #7f0055; font-weight: bold;">msgbox</span> a <span style="color: #3f7f59;">'prints 10</span>
<span style="color: #7f0055; font-weight: bold;">msgbox</span> c <span style="color: #3f7f59;">'prints 100</span>
<span style="color: #7f0055; font-weight: bold;">function</span> findsqr(<span style="color: #7f0055; font-weight: bold;">byval</span> a)
a = a*a
findsqr = a
<span style="color: #7f0055; font-weight: bold;">end</span> <span style="color: #7f0055; font-weight: bold;">function</span></pre>
</div>
<div>
<br />
<h2 style="text-align: left;">
Other built-in functions in VBScript</h2>
<ol style="text-align: left;">
<li>msgbox</li>
<li>inputbox</li>
<li>eval </li>
<li>execute</li>
</ol>
<div>
msgbox function is used to show message to the user.</div>
</div>
</div>
inputbox function is used to read the value from the user.<br />
<br />
<pre>a = inputbox(<span style="color: #2a00ff;">"Enter the number"</span>)
<span style="color: #7f0055; font-weight: bold;">msgbox</span> a <span style="color: #3f7f59;">'prints 33</span></pre>
<br />
<table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"><tbody>
<tr><td style="text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh4fllYSlVC-ywz9at_-Y17VOfFUwye1bwwGBPtr57X9W7smoV-71supEy1opISCBdCM5w5IcMIMLsySNfvmaDdbwzYgrpI4qsjqYfcjX23NM4OgaaOuOIbxrsLIGjw21oju6jT6zW9V1g/s1600/inputbox+function+in+vbscript.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh4fllYSlVC-ywz9at_-Y17VOfFUwye1bwwGBPtr57X9W7smoV-71supEy1opISCBdCM5w5IcMIMLsySNfvmaDdbwzYgrpI4qsjqYfcjX23NM4OgaaOuOIbxrsLIGjw21oju6jT6zW9V1g/s1600/inputbox+function+in+vbscript.png" height="168" width="400" /></a></td></tr>
<tr><td class="tr-caption" style="text-align: center;"><b>Inputbox function in vbscript</b></td></tr>
</tbody></table>
<b>Eval and execute functions</b> are used to execute any valid vbscript expression. Difference between 2 functions is that the operator = is handled in different ways.<br />
<br />
<pre>Eval always uses = <span style="color: #7f0055; font-weight: bold;">as</span> a comparison operator.
Execute always uses = <span style="color: #7f0055; font-weight: bold;">as</span> a assignment operator.</pre>
<pre>
</pre>
<pre>Example -</pre>
<pre>a=20
<span style="color: #7f0055; font-weight: bold;">msgbox</span> eval(<span style="color: #2a00ff;">"a=10"</span>) <span style="color: #3f7f59;">'will print false</span>
<span style="color: #7f0055; font-weight: bold;">msgbox</span> a
a=20
execute(<span style="color: #2a00ff;">"a=10"</span>)
<span style="color: #7f0055; font-weight: bold;">msgbox</span> a <span style="color: #3f7f59;">'will print 10</span></pre>
<br />
<br />
<br />
<br />
<br />
<br /></div>
Sagarhttp://www.blogger.com/profile/06686177535004668246noreply@blogger.com0tag:blogger.com,1999:blog-695416308230945063.post-2443089093224187622014-03-29T07:38:00.003-07:002014-04-03T03:48:47.891-07:00Sub Procedures in VBScript<div dir="ltr" style="text-align: left;" trbidi="on">
Sub procedures are used to perform the specific task. Sub procedures are used to increase the reusability of the code.<br />
<br />
<h2 style="text-align: left;">
Simple example of the procedure</h2>
<br />
Suppose you want to find the sum of 2 numbers. Without procedures, you will write below code.<br />
<br />
<pre>a = 10
b = 20
c = a+ b
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> c</pre>
<br />
To add another 2 different numbers, you will use below code<br />
<br />
<pre>a = 33
b = 65
c = a+ b
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> c</pre>
<br />
In above examples, we have to add 2 numbers. So the operation is same (Repeating) So we can write the procedure which will take 2 parameters as shown below.<br />
<br />
<pre><span style="color: #7f0055; font-weight: bold;">sub</span> sum(<span style="color: #7f0055; font-weight: bold;">byref</span> a, <span style="color: #7f0055; font-weight: bold;">byref</span> b)
<span style="color: #7f0055; font-weight: bold;">msgbox</span> <span style="color: #7f0055; font-weight: bold;">cint</span>(a) + <span style="color: #7f0055; font-weight: bold;">cint</span>(b)
<span style="color: #7f0055; font-weight: bold;">end</span> <span style="color: #7f0055; font-weight: bold;">sub</span></pre>
<h2 style="text-align: left;">
Calling procedures in VBScript</h2>
To call the procedures we can use below statements.<br />
<br />
<pre><span style="color: #7f0055; font-weight: bold;">Call</span> sum(10,20)
<span style="color: #7f0055; font-weight: bold;">Call</span> sum(33,65)</pre>
<br />
Thus we can call procedures many times in the code. This reduces the lines of code as well as maintainability of the code.<br />
<h2>
Passing arguments to the procedure</h2>
<div>
We can pass the arguments to the procedure by 2 ways.</div>
<div>
<ol style="text-align: left;">
<li>Pass by reference </li>
<li>Pass by value</li>
</ol>
<div>
<div>
<br /></div>
<div style="text-align: left;">
<b><u>Pass by reference</u></b></div>
<div style="text-align: left;">
<br /></div>
<div>
By default, values are passed to the procedure by reference. When we pass the values by reference the changes made to the variables in the called procedure are reflected in the calling procedure.</div>
<div>
<br /></div>
<div>
<pre>a = 10
<span style="color: #7f0055; font-weight: bold;">call</span> findsqr(a)
<span style="color: #7f0055; font-weight: bold;">msgbox</span> a <span style="color: #3f7f59;">'prints 100</span>
<span style="color: #7f0055; font-weight: bold;">sub</span> findsqr(<span style="color: #7f0055; font-weight: bold;">byref</span> a)
a = a*a
<span style="color: #7f0055; font-weight: bold;">msgbox</span> a <span style="color: #3f7f59;">'prints 100</span>
<span style="color: #7f0055; font-weight: bold;">end</span> <span style="color: #7f0055; font-weight: bold;">sub</span></pre>
</div>
</div>
</div>
<div>
<br /></div>
<br />
<div>
<div>
<b><u>Pass by value</u></b></div>
<div>
<br /></div>
<div>
When we pass the values by value, the changes made to the variables in the called procedure are not reflected in the calling procedure.</div>
<div>
<br /></div>
<div>
<pre>a = 10
<span style="color: #7f0055; font-weight: bold;">call</span> findsqr(a)
<span style="color: #7f0055; font-weight: bold;">msgbox</span> a <span style="color: #3f7f59;">'prints 10</span>
<span style="color: #7f0055; font-weight: bold;">sub</span> findsqr(<span style="color: #7f0055; font-weight: bold;">byval</span> a)
a = a*a
<span style="color: #7f0055; font-weight: bold;">msgbox</span> a <span style="color: #3f7f59;">'prints 100</span>
<span style="color: #7f0055; font-weight: bold;">end</span> <span style="color: #7f0055; font-weight: bold;">sub</span></pre>
</div>
</div>
<br />
<br />
<br />
<br /></div>
Sagarhttp://www.blogger.com/profile/06686177535004668246noreply@blogger.com0tag:blogger.com,1999:blog-695416308230945063.post-88674639987478510112014-03-29T00:28:00.001-07:002014-03-31T23:27:52.566-07:00Basic Syntax in VBScript<div dir="ltr" style="text-align: left;" trbidi="on">
<b><u>Basic VBScript Syntax</u></b><br />
Remember below points about VBScript Syntax.<br />
<ol>
<li>VBScript is loosely typed language that means you do not need to declare the variables with data type</li>
<li>VBScript comments start with symbol '. You can also comment using rem keyword.</li>
<li>You do not need to put semicolon at the end of the statement like C,C++ and JAVA</li>
</ol>
<b>VBScript </b>also provides below functions that can be used to find out more information about the variables.<br />
<br />
<ol style="text-align: left;">
<li>IsArray - This function can be used to check if the given variable is an array or not</li>
<li>IsDate - This function can be used to check if the given variable is a valid date or not</li>
<li>IsEmpty - This function can be used to check if the given variable is empty or not</li>
<li>IsNull - This function can be used to check if the given variable is a null or not</li>
<li>IsNumeric - This function can be used to check if the given variable is valid number or not</li>
<li>IsObject - This function can be used to check if the given variable is valid object or not</li>
<li>TypeName - This function returns the data type of the given variable.</li>
</ol>
<div>
Examples - </div>
<div>
<br /></div>
<br />
<div>
<div style="overflow: auto;">
<pre style="background-color: white;"><pre><span style="color: #7f0055; font-weight: bold;">dim</span> a(4)
<span style="color: #7f0055; font-weight: bold;">dim</span> b
b=10
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> <span style="color: #2a00ff;">"a is array? -> "</span> & <span style="color: #7f0055; font-weight: bold;">IsArray</span>(a) <span style="color: #3f7f59;">'prints true</span>
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> <span style="color: #2a00ff;">"b is a date? -> "</span> & <span style="color: #7f0055; font-weight: bold;">IsDate</span>(a) <span style="color: #3f7f59;">'prints false</span>
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> <span style="color: #2a00ff;">"b is empty? -> "</span> & <span style="color: #7f0055; font-weight: bold;">IsEmpty</span>(b) <span style="color: #3f7f59;">'prints false</span>
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> <span style="color: #2a00ff;">"b is null? -> "</span> & <span style="color: #7f0055; font-weight: bold;">IsNull</span>(b) <span style="color: #3f7f59;">'prints false</span>
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> <span style="color: #2a00ff;">"b is numeric? -> "</span> & <span style="color: #7f0055; font-weight: bold;">IsNumeric</span>(b) <span style="color: #3f7f59;">'prints true</span>
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> <span style="color: #2a00ff;">"b is object? -> "</span> & <span style="color: #7f0055; font-weight: bold;">IsObject</span>(b) <span style="color: #3f7f59;">'prints false</span>
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> <span style="color: #2a00ff;">"TypeName of b -> "</span> & <span style="color: #7f0055; font-weight: bold;">TypeName</span>(b) <span style="color: #3f7f59;">'prints integer</span>
</pre>
<div>
<span style="color: #3f7f59;">
</span></div>
</pre>
</div>
</div>
<div>
<br /></div>
</div>
Sagarhttp://www.blogger.com/profile/06686177535004668246noreply@blogger.com0tag:blogger.com,1999:blog-695416308230945063.post-76524099943172919542014-03-28T08:13:00.000-07:002014-03-29T20:30:20.468-07:00Vbscript Tutorial for Beginners<div dir="ltr" style="text-align: left;" trbidi="on">
We are going to cover all major topics on vbscript in this tutorial for beginners.<br />
<br />
<ol style="text-align: left;">
<li>VBScript - <a href="http://vbscript-interview-questions.blogspot.in/2013/05/what-is-vbscript.html" target="_blank">Intro and applications</a></li>
<li><a href="http://vbscript-interview-questions.blogspot.in/2013/05/types-of-variables-in-vbscript.html" target="_blank">Variables</a>, <a href="http://vbscript-interview-questions.blogspot.in/2013/05/data-types-in-vbscript.html" target="_blank">Data Types (Conversions)</a> and <a href="http://vbscript-interview-questions.blogspot.in/2013/09/what-are-different-operators-in-vbscript.html" target="_blank">Operators</a>, <a href="http://vbscript-interview-questions.blogspot.in/2014/03/basic-syntax-in-vbscript.html" target="_blank">comments</a>, Set in VBScript and IsArray, IsDate, IsEmpty , IsNull , IsNumeric , IsObject ,TypeName ,VarType</li>
<li><a href="http://vbscript-interview-questions.blogspot.in/2013/05/how-we-can-define-array-in-vb-script.html" target="_blank">Arrays </a>in VBScript - <a href="http://vbscript-interview-questions.blogspot.in/2013/05/types-of-array-in-vb-script.html" target="_blank">types of array</a></li>
<li>Control Statement - <a href="http://vbscript-interview-questions.blogspot.in/2013/09/what-are-different-conditional.html" target="_blank">Conditional</a> and <a href="http://vbscript-interview-questions.blogspot.in/2013/09/loops-in-vbscript.html" target="_blank">looping</a> </li>
<li><a href="http://vbscript-interview-questions.blogspot.in/2014/03/sub-procedures-in-vbscript.html" target="_blank">Procedures</a> and <a href="http://vbscript-interview-questions.blogspot.in/2014/03/functions-in-vbscript.html" target="_blank">Functions - eval, execute, inputbox, msgbox</a> and <a href="http://vbscript-interview-questions.blogspot.in/2013/05/difference-between-procedures-and.html" target="_blank">difference between function and sub</a></li>
<li><a href="http://vbscript-interview-questions.blogspot.in/2013/05/date-time-functions-in-vb-script.html" target="_blank">Date and Time</a></li>
<li><a href="http://vbscript-interview-questions.blogspot.in/2013/05/string-functions-in-vbscript.html" target="_blank">Strings</a></li>
<li><a href="http://vbscript-interview-questions.blogspot.in/2013/05/maths-functions-in-vbscript.html" target="_blank">Maths Functions</a></li>
<li><a href="http://vbscript-interview-questions.blogspot.in/2014/03/classes-in-vbscript.html" target="_blank">Classes</a> in VBScript - Constructors</li>
<li><a href="http://vbscript-interview-questions.blogspot.in/2013/05/regular-expressions-in-vb-script-example.html" target="_blank">Regular Expressions</a> in Vbscript </li>
<li><a href="http://vbscript-interview-questions.blogspot.in/2013/05/dictionary-object-in-vb-script-example.html" target="_blank">Dictionary </a>in VBScript</li>
<li><a href="http://vbscript-interview-questions.blogspot.in/2013/05/file-system-object-in-vb-script.html" target="_blank">File System</a> in VBScript</li>
<li><a href="http://vbscript-interview-questions.blogspot.in/2014/03/error-handling-in-vbscript.html" target="_blank">Error Handling</a></li>
<li><a href="http://vbscript-interview-questions.blogspot.in/2013/09/what-is-windows-script-host.html" target="_blank">WSH</a></li>
<li><a href="http://vbscript-interview-questions.blogspot.in/2013/09/what-is-wmi.html" target="_blank">WMI and Win32 classes</a></li>
</ol>
<br /></div>
Sagarhttp://www.blogger.com/profile/06686177535004668246noreply@blogger.com0tag:blogger.com,1999:blog-695416308230945063.post-25614064937870212182014-03-28T08:11:00.000-07:002014-03-31T01:56:50.827-07:00Classes in VBScript<div dir="ltr" style="text-align: left;" trbidi="on">
Here is an example that shows how we can create a class in QTP. Once we define the class, we can create its objects and then access its method and properties.<br />
<div>
<br /></div>
<div>
<br /></div>
<div>
<pre><span style="color: dimgrey;">'declare the class book.</span></pre>
<pre></pre>
<pre><span style="color: maroon; font-weight: bold;">Class</span> Book
<span style="color: maroon; font-weight: bold;">dim</span> bn<span style="color: #808030;">,</span>bp
<span style="color: dimgrey;">'class has 2 variable members bn and bp.</span>
<span style="color: maroon; font-weight: bold;">public</span> <span style="color: maroon; font-weight: bold;">Property</span> <span style="color: maroon; font-weight: bold;">Get</span> bookname<span style="color: #808030;">(</span><span style="color: #808030;">)</span>
<span style="color: dimgrey;">' Get bookname property gets the book name of the object of Book</span>
bookname <span style="color: #808030;">=</span> bn
<span style="color: maroon; font-weight: bold;">End</span> <span style="color: maroon; font-weight: bold;">Property</span>
<span style="color: maroon; font-weight: bold;">public</span> <span style="color: maroon; font-weight: bold;">Property</span> <span style="color: maroon; font-weight: bold;">Let</span> bookname<span style="color: #808030;">(</span>x<span style="color: #808030;">)</span>
bn <span style="color: #808030;">=</span> x
<span style="color: dimgrey;">' Let bookname property assigns value to the book name of the object of Book</span>
<span style="color: maroon; font-weight: bold;">End</span> <span style="color: maroon; font-weight: bold;">Property</span>
<span style="color: maroon; font-weight: bold;">public</span> <span style="color: maroon; font-weight: bold;">Property</span> <span style="color: maroon; font-weight: bold;">Get</span> price<span style="color: #808030;">(</span><span style="color: #808030;">)</span>
price <span style="color: #808030;">=</span> bp
<span style="color: maroon; font-weight: bold;">End</span> <span style="color: maroon; font-weight: bold;">Property</span>
<span style="color: maroon; font-weight: bold;">public</span> <span style="color: maroon; font-weight: bold;">Property</span> <span style="color: maroon; font-weight: bold;">Let</span> price<span style="color: #808030;">(</span>x<span style="color: #808030;">)</span>
bp <span style="color: #808030;">=</span> x
<span style="color: maroon; font-weight: bold;">End</span> <span style="color: maroon; font-weight: bold;">Property</span>
<span style="color: maroon; font-weight: bold;">function</span> discountedPrice<span style="color: #808030;">(</span><span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">print</span> bp<span style="color: #808030;">-</span><span style="color: #008c00;">20</span>
<span style="color: dimgrey;">'We can have functions and procedures inside class to process memeber variables</span>
<span style="color: maroon; font-weight: bold;">End</span> <span style="color: maroon; font-weight: bold;">function</span>
<span style="color: maroon; font-weight: bold;">Private</span> <span style="color: maroon; font-weight: bold;">Sub</span> Class_Initialize <span style="color: dimgrey;">' Setup Initialize event.</span>
<span style="color: maroon; font-weight: bold;">MsgBox</span><span style="color: #808030;">(</span><span style="color: #0000e6;">"Object of Book Class created"</span><span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">End</span> <span style="color: maroon; font-weight: bold;">Sub</span>
<span style="color: maroon; font-weight: bold;">Private</span> <span style="color: maroon; font-weight: bold;">Sub</span> Class_Terminate <span style="color: dimgrey;">' Setup Terminate event.</span>
<span style="color: maroon; font-weight: bold;">MsgBox</span><span style="color: #808030;">(</span><span style="color: #0000e6;">"Object of Book Class destroyed"</span><span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">End</span> <span style="color: maroon; font-weight: bold;">Sub</span>
<span style="color: maroon; font-weight: bold;">End</span> <span style="color: maroon; font-weight: bold;">Class</span>
<span style="color: maroon; font-weight: bold;">Set</span> b1 <span style="color: #808030;">=</span> <span style="color: maroon; font-weight: bold;">new</span> Book
<span style="color: dimgrey;">'createing the object b2 of the class Book.</span>
b1<span style="color: #008c00;">.</span>bookname <span style="color: #808030;">=</span> <span style="color: #0000e6;">"QTP Tutorials"</span>
b1<span style="color: #008c00;">.</span>price <span style="color: #808030;">=</span> <span style="color: #008c00;">220</span>
<span style="color: dimgrey;">'assigning value to the object b1</span>
<span style="color: maroon; font-weight: bold;">print</span> b1<span style="color: #008c00;">.</span>bookname<span style="color: #808030;">(</span><span style="color: #808030;">)</span>
<span style="color: dimgrey;">'getting the value of the property bookname.</span>
<span style="color: dimgrey;">'accessing the function in class</span>
b1<span style="color: #008c00;">.</span>discountedPrice<span style="color: #808030;">(</span><span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">Set</span> b1 <span style="color: #808030;">=</span> <span style="color: maroon; font-weight: bold;">nothing</span></pre>
<pre><span style="color: maroon; font-weight: bold;">
</span></pre>
<br />QTP does support object oriented programming to some extent. We can create constructors and destructors in vbscript classes using class_initialize and class_terminate methods.
class_initialize method acts like constructor function which gets called automatically when we create an object of the class.
This is how we can create and use the classes in QTP.<br /><pre><span style="color: maroon;"><b>
</b></span></pre>
</div>
</div>
Sagarhttp://www.blogger.com/profile/06686177535004668246noreply@blogger.com0tag:blogger.com,1999:blog-695416308230945063.post-87532181038001852402014-03-28T08:07:00.002-07:002014-04-03T04:18:18.391-07:00Error handling in VBScript<div dir="ltr" style="text-align: left;" trbidi="on">
VBScript provides below statements and keywords to handle errors.<br />
<div>
<ol style="text-align: left;">
<li>on error resume next</li>
<li>Err object - err.description, err.number</li>
</ol>
<div>
<br /></div>
</div>
<div>
<div>
Sample code to handle the error is given below</div>
<div>
<br /></div>
<div>
<pre><span style="color: #7f0055; font-weight: bold;">If</span> <span style="color: #7f0055; font-weight: bold;">err</span>.number <> 0 <span style="color: #7f0055; font-weight: bold;">Then</span>
<span style="color: #7f0055; font-weight: bold;">print</span> <span style="color: #7f0055; font-weight: bold;">err</span>.description
<span style="color: #7f0055; font-weight: bold;">else</span>
<span style="color: #7f0055; font-weight: bold;">print</span> <span style="color: #2a00ff;">"there was no error in above statement"</span>
<span style="color: #7f0055; font-weight: bold;">end</span> <span style="color: #7f0055; font-weight: bold;">if</span></pre>
</div>
<div>
<br /></div>
<br />
<div>
Whenever any error occurs in the script, We get the message window with detailed description of the error.</div>
<div>
<br /></div>
<div>
But when we are executing the scripts, we do not want this message box to appear to come as this will halt the execution of the script.</div>
<div>
<br /></div>
<div>
To prevent message box from appearing, we use below statement above the block of code.</div>
<div>
<br /></div>
<div>
<i>On error resume next</i></div>
<div>
<i>----------</i></div>
<div>
<i>----------</i></div>
<div>
<i>more statements</i></div>
<div>
<i><br /></i></div>
<div>
With <i>On error resume next</i> in place<i>, </i>VBScript runs even though error exists in the code. We can capture those errors using Err object as stated earlier.</div>
<div>
<br /></div>
<div>
When you are debugging the scripts, you should not use On error resume next statement as it will suppress the errors and you will not be able to figure out the issue in your script.</div>
</div>
</div>
Sagarhttp://www.blogger.com/profile/06686177535004668246noreply@blogger.com0tag:blogger.com,1999:blog-695416308230945063.post-66664041901485708332013-09-24T01:46:00.003-07:002013-09-24T01:46:43.691-07:00How to read webpage in vbscript<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
Here is an example in which I am using xmlhttp object to get the url source code.<br />
We can get data from any website using below code.<br />
<br />
<i><br /></i>
<i>url="http://www.makaninmumbai.com"</i><br />
<i>Set objHTTP = CreateObject("MSXML2.XMLHTTP")</i><br />
<i>Call objHTTP.Open("GET", url, FALSE)</i><br />
<i>objHTTP.Send</i><br />
<i>msgbox (objHTTP.ResponseText)</i><br />
<div>
<br /></div>
</div>
Sagarhttp://www.blogger.com/profile/06686177535004668246noreply@blogger.com0tag:blogger.com,1999:blog-695416308230945063.post-54262180489511346122013-09-24T00:34:00.002-07:002013-09-24T00:34:20.136-07:00How to terminate the process in vbscript<div dir="ltr" style="text-align: left;" trbidi="on">
Here is the vbscript that will terminate or close all the processes with given name<br />
<br />
In below script I am closing internet explorer process.<br />
<br />
'Create a WMI object<br />
strComputer = "."<br />
Set objWMIService = GetObject("winmgmts:" _<br />
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")<br />
<br />
<br />
'get all processes with name iexplore.exe<br />
Set colProcessList = objWMIService.ExecQuery _<br />
("Select * from Win32_Process Where Name = 'iexplore.exe'")<br />
<br />
'terminate each process<br />
For Each objProcess in colProcessList<br />
objProcess.Terminate()<br />
Next<br />
<div>
<br /></div>
</div>
Sagarhttp://www.blogger.com/profile/06686177535004668246noreply@blogger.com0tag:blogger.com,1999:blog-695416308230945063.post-4929823279673005742013-09-24T00:13:00.002-07:002013-09-24T00:13:24.509-07:00How to create a word document using vbscript<div dir="ltr" style="text-align: left;" trbidi="on">
' Create and Save a Word Document<br />
<br />
'Create the Word object<br />
Set objWord = CreateObject("Word.Application")<br />
<br />
'Set the caption of the Word<br />
objWord.Caption = "Welcome to Automated Word Script"<br />
objWord.Visible = True<br />
<br />
'Add new document<br />
Set objDoc = objWord.Documents.Add()<br />
Set objSelection = objWord.Selection<br />
<br />
objSelection.Font.Name = "Arial"<br />
objSelection.Font.Size = "18"<br />
objSelection.TypeText "" & Date()<br />
objSelection.TypeParagraph()<br />
objSelection.TypeParagraph()<br />
<br />
objSelection.Font.Size = "10"<br />
<br />
<br />
<br />
'Start writing to the word document<br />
<br />
For i=0 to 10<br />
<br />
objSelection.Font.Bold = True<br />
objSelection.TypeText "Square of " & i & " is " & i*i<br />
objSelection.Font.Bold = False<br />
objSelection.TypeParagraph()<br />
objSelection.TypeParagraph()<br />
<br />
Next<br />
<br />
'finally save the document.<br />
objDoc.SaveAs("C:\mydoc.doc")<br />
<br />
'Quit the application<br />
objWord.Quit<br />
<br />
<br />
<br />
<br /></div>
Sagarhttp://www.blogger.com/profile/06686177535004668246noreply@blogger.com0tag:blogger.com,1999:blog-695416308230945063.post-86829527995203998562013-09-23T23:49:00.001-07:002013-09-23T23:49:37.154-07:00How to get the IP Address of System in vbscript.<div dir="ltr" style="text-align: left;" trbidi="on">
Here is a program that can be run to get the IP address of system.<br />
<br />
'Get the WMI object for local computer<br />
strComputer = "."<br />
Set objWMIService = GetObject("winmgmts:" _<br />
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")<br />
<br />
'get the Ip address configuration setting using Win32_NetworkAdapterConfiguration Class<br />
'Filter IP configuration records using where condition - Where IPEnabled = True<br />
<br />
Set IPConfigSet = objWMIService.ExecQuery _<br />
("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")<br />
<br />
'Enumerate Each IP Address configuration<br />
<br />
For Each IP_Configuration_Record in IPConfigSet<br />
<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>If Not IsNull(IP_Configuration_Record.IPAddress) Then<br />
<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>For i=0 to UBound(IP_Configuration_Record.IPAddress)<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span> str = str & IP_Configuration_Record.IPAddress(i)<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Next<br />
<br />
End If<br />
<br />
Next<br />
<br />
'Display IP Address of the system<br />
msgbox str<br />
<br />
<br />
<br /></div>
Sagarhttp://www.blogger.com/profile/06686177535004668246noreply@blogger.com0tag:blogger.com,1999:blog-695416308230945063.post-27873662414139350172013-09-23T23:36:00.001-07:002013-09-23T23:36:17.625-07:00How to shutdown a computer in vbscript?<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
'Warning - if you run this script, your computer will shut down<br />
<br />
'Here you can specify name of computer you want to shutdown.....<br />
'. means local computer<br />
strComputer = "."<br />
<br />
'create WMI object<br />
<br />
Set objWMIService = GetObject_<br />
("winmgmts:{impersonationLevel=impersonate,(Shutdown)}\\" & _<br />
strComputer & "\root\cimv2")<br />
<br />
'Get the reference of all operating systems in your machine using Win32_OperatingSystem<br />
<br />
<br />
Set colOperatingSystems = objWMIService.ExecQuery _<br />
("Select * from Win32_OperatingSystem")<br />
<br />
<br />
'Enumerate each OS and shut down .<br />
For Each objOperatingSystem in colOperatingSystems<br />
objOperatingSystem.Win32Shutdown(1)<br />
Next</div>
Sagarhttp://www.blogger.com/profile/06686177535004668246noreply@blogger.com0tag:blogger.com,1999:blog-695416308230945063.post-47177963963424570982013-09-23T23:33:00.002-07:002013-09-23T23:33:22.897-07:00How to Restart a machine in vbscript?<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
'Warning - if you run this script, your computer will reboot/restart<br />
<br />
'Here you can specify name of computer you want to restart.....<br />
'. means local computer<br />
strComputer = "."<br />
<br />
'create WMI object<br />
Set objWMIService = GetObject("winmgmts:" _<br />
& "{impersonationLevel=impersonate,(Shutdown)}!\\" & _<br />
strComputer & "\root\cimv2")<br />
<br />
'Get the reference of all operating systems in your machine using Win32_OperatingSystem<br />
Set colOperatingSystems = objWMIService.ExecQuery _<br />
("Select * from Win32_OperatingSystem")<br />
<br />
'Enumerate each OS and restart.<br />
For Each Os in colOperatingSystems<br />
Os.Reboot()<br />
Next<br />
<div>
<br /></div>
</div>
Sagarhttp://www.blogger.com/profile/06686177535004668246noreply@blogger.com0tag:blogger.com,1999:blog-695416308230945063.post-42436840336528837432013-09-23T23:27:00.001-07:002013-09-23T23:27:09.649-07:00How to read the environment variables in your system in vbscript?<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
You can read the system variables using below program<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<br />
'Create the WMI object<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>strComputer = "."<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Set objWMIService = GetObject("winmgmts:" _<br />
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
'get all variables using Win32_Environment Class.<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Set colItems = objWMIService.ExecQuery("Select * from Win32_Environment")<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
'Enumerate each variable<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>For Each objItem in colItems<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>str = str & objItem.Name & " -----------------> " & objItem.VariableValue & vbcrlf<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Next<br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span>Msgbox str<br />
<br />
<br /></div>
Sagarhttp://www.blogger.com/profile/06686177535004668246noreply@blogger.com0tag:blogger.com,1999:blog-695416308230945063.post-84663262484943504082013-09-23T23:18:00.000-07:002013-09-23T23:18:01.851-07:00How to list all files in cookies in vbscript?<div dir="ltr" style="text-align: left;" trbidi="on">
Cookie is a file stored by the web server on web client machine.<br />
If you want to find out the cookies in your machine you can use below program.<br />
<br />
<i>Const COOKIES = &H21&</i><br />
<i><br /></i>
<i>Set objShell = CreateObject("Shell.Application")</i><br />
<i>Set objFolder = objShell.Namespace(COOKIES)</i><br />
<i>Set objFolderItem = objFolder.Self</i><br />
<i><br /></i>
<i>Msgbox "The path of cookies folder is -> " & objFolderItem.Path</i><br />
<i><br /></i>
<i>Set colItems = objFolder.Items</i><br />
<i>For Each cookie in colItems</i><br />
<i> Msgbox cookie.Name</i><br />
<i>Next</i></div>
Sagarhttp://www.blogger.com/profile/06686177535004668246noreply@blogger.com0tag:blogger.com,1999:blog-695416308230945063.post-26436014256371370212013-09-23T05:17:00.001-07:002013-09-23T23:14:56.581-07:00What is WMI?<div dir="ltr" style="text-align: left;" trbidi="on">
WMI stands for Windows Management Instrumentation. This is used to access system hardware and software features. It provides very powerful API to perform tasks like starting process, terminating processes etc.</div>
Sagarhttp://www.blogger.com/profile/06686177535004668246noreply@blogger.com0tag:blogger.com,1999:blog-695416308230945063.post-44264863538001949212013-09-23T05:15:00.001-07:002013-09-23T05:15:17.500-07:00What is Windows Script Host?<div dir="ltr" style="text-align: left;" trbidi="on">
WSH is a scripting
host for Windows Script compatible scripting engines.<br />
WSH supports various scripting languages like vbscript, javascript, python etc.<br />
<br />
You can do below operations using WSH.<br />
<br />
<ol style="text-align: left;">
<li>Map network printer/drive</li>
<li>Execute remote script</li>
<li>Send keys to application</li>
<li>Run other applications</li>
<li>Read/Write registry</li>
<li>Access special folders, shortcuts etc</li>
</ol>
</div>
Sagarhttp://www.blogger.com/profile/06686177535004668246noreply@blogger.com0tag:blogger.com,1999:blog-695416308230945063.post-84173411654388093872013-09-23T03:01:00.004-07:002014-03-31T23:58:58.645-07:00Loops in Vbscript<div dir="ltr" style="text-align: left;" trbidi="on">
Here is the list of different looping statements in vbscript.<br />
<br />
<ol style="text-align: left;">
<li>For ....Next</li>
<li>For Each ...Next</li>
<li>Do While...Loop</li>
<li>Do Until....Loop</li>
<li>While...Wend</li>
</ol>
<br />
<br />
<br />
'You can exit from For Loop with Exit For statement<br />
'For Loop<br />
<pre><span style="color: #7f0055; font-weight: bold;">For</span> i=1 <span style="color: #7f0055; font-weight: bold;">to</span> 3
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> i*i
<span style="color: #7f0055; font-weight: bold;">Next</span>
<span style="color: #3f7f59;">'For Each loop</span>
a = <span style="color: #7f0055; font-weight: bold;">array</span>(22,33,5,3)
<span style="color: #7f0055; font-weight: bold;">For</span> <span style="color: #7f0055; font-weight: bold;">each</span> e <span style="color: #7f0055; font-weight: bold;">in</span> a
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> a
<span style="color: #7f0055; font-weight: bold;">Next</span></pre>
<br />
<br />
'You can exit from Do Loop with Exit Do statement<br />
'do while ...loop<br />
<pre>i=1
<span style="color: #7f0055; font-weight: bold;">Do</span> <span style="color: #7f0055; font-weight: bold;">while</span> (i<3)
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> i*i
i=i+1
<span style="color: #7f0055; font-weight: bold;">Loop</span></pre>
<br />
'do until ....loop<br />
<pre>i=1
<span style="color: #7f0055; font-weight: bold;">Do</span> <span style="color: #7f0055; font-weight: bold;">until</span> i>3
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> i*i
i=i+1
<span style="color: #7f0055; font-weight: bold;">Loop</span></pre>
<br />
'You can not exit from while loop using exit statement.<br />
'while........wend<br />
<pre>i=1
<span style="color: #7f0055; font-weight: bold;">While</span> i<=3
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> i*i
i=i+1
<span style="color: #7f0055; font-weight: bold;">Wend</span></pre>
<br /></div>
Sagarhttp://www.blogger.com/profile/06686177535004668246noreply@blogger.com0tag:blogger.com,1999:blog-695416308230945063.post-81087190662020124952013-09-23T02:51:00.001-07:002014-03-31T23:51:51.333-07:00What are different conditional statements in vbscript?<div dir="ltr" style="text-align: left;" trbidi="on">
In Vbscript there are 2 types of conditional statements.<br />
<br />
<ol style="text-align: left;">
<li>if ...else</li>
<li>Select Case</li>
</ol>
<div>
<b><u>Example of if else - </u></b></div>
<br />
<div>
<br /></div>
<div>
<pre>a = 10
<span style="color: #7f0055; font-weight: bold;">If</span> a > 10 <span style="color: #7f0055; font-weight: bold;">Then</span>
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> <span style="color: #2a00ff;">"a is greater than 10"</span>
<span style="color: #7f0055; font-weight: bold;">ElseIf</span> a>0 <span style="color: #7f0055; font-weight: bold;">then</span>
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> <span style="color: #2a00ff;">"a is between 0 and 11"</span>
<span style="color: #7f0055; font-weight: bold;">Else</span>
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> <span style="color: #2a00ff;">"a is not greater than 0"</span>
<span style="color: #7f0055; font-weight: bold;">End</span> <span style="color: #7f0055; font-weight: bold;">If</span></pre>
</div>
<div>
<br /></div>
<br />
<div>
<br /></div>
<div>
<br /></div>
<div>
<b><u>Example of select case - </u></b></div>
<div>
<br /></div>
<br />
<div>
<pre>a = Inputbox(<span style="color: #2a00ff;">"enter the number"</span>)
<span style="color: #7f0055; font-weight: bold;">Select</span> <span style="color: #7f0055; font-weight: bold;">Case</span> <span style="color: #7f0055; font-weight: bold;">cint</span>(a)
<span style="color: #7f0055; font-weight: bold;">Case</span> 1
<span style="color: #7f0055; font-weight: bold;">msgbox</span> <span style="color: #2a00ff;">"You entered 1"</span>
<span style="color: #7f0055; font-weight: bold;">Case</span> 2
<span style="color: #7f0055; font-weight: bold;">msgbox</span> <span style="color: #2a00ff;">"You entered 2"</span>
<span style="color: #7f0055; font-weight: bold;">Case</span> 3
<span style="color: #7f0055; font-weight: bold;">msgbox</span> <span style="color: #2a00ff;">"You entered 3"</span>
<span style="color: #7f0055; font-weight: bold;">Case</span> <span style="color: #7f0055; font-weight: bold;">else</span>
<span style="color: #7f0055; font-weight: bold;">msgbox</span> <span style="color: #2a00ff;">"You entered invalid input"</span>
<span style="color: #7f0055; font-weight: bold;">End</span> <span style="color: #7f0055; font-weight: bold;">Select</span></pre>
</div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
<div>
<br /></div>
</div>
Sagarhttp://www.blogger.com/profile/06686177535004668246noreply@blogger.com0tag:blogger.com,1999:blog-695416308230945063.post-19009328483379556932013-09-23T02:35:00.001-07:002014-03-31T23:29:36.141-07:00What are the different operators in vbscript?<div dir="ltr" style="text-align: left;" trbidi="on">
At broad level there are 3 kinds of operators as mentioned below.<br />
<div>
<br /></div>
<div>
<ol style="text-align: left;">
<li>Arithmetic Operators</li>
<li>Comparison Operators</li>
<li>Logical Operators</li>
</ol>
<br />
<br />
<b><u>Arithmetic Operators in the order of precedence and their usage.</u></b><br />
Exponentiation ^<br />
Unary negation - <br />
Multiplication * <br />
Floating point Division / <br />
Integer division \ <br />
Modulus arithmetic Mod -gets the remainder<br />
Addition + <br />
Subtraction - <br />
String concatenation & - concatenates 2 strings<br />
<b><u><br /></u></b></div>
<div>
<span style="font-weight: bold;"><u>Comparison Operators and their usage.</u></span><br />
Equality = <br />
Inequality <> <br />
Less than < <br />
Greater than > <br />
Less than or equal to <= <br />
Greater than or equal to >= <br />
Object equivalence Is<br />
<br />
<b><u>Logical Operators </u></b><b><u>in the order of precedence</u></b><b><u> and their usage.</u></b></div>
<div>
Logical negation Not <br />
Logical conjunction And <br />
Logical disjunction Or <br />
Logical exclusion Xor <br />
Logical equivalence Eqv <br />
Logical implication Imp<br />
<br />
<br />
Example - </div>
<div>
<br /></div>
<div>
<pre><span style="color: #7f0055; font-weight: bold;">if</span> a>10 <span style="color: #7f0055; font-weight: bold;">and</span> b<99 <span style="color: #7f0055; font-weight: bold;">then</span>
<span style="color: #7f0055; font-weight: bold;">msgbox</span> <span style="color: #2a00ff;">"a is greater than 10 and b is less than 99"</span>
<span style="color: #7f0055; font-weight: bold;">end</span> <span style="color: #7f0055; font-weight: bold;">if</span>
<span style="color: #7f0055; font-weight: bold;">msgbox</span> <span style="color: #2a00ff;">"Remainder after dividing 10 by 3 is -> "</span> & (10 <span style="color: #7f0055; font-weight: bold;">mod</span> 3)</pre>
</div>
<div>
<br /></div>
<div>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<table cellspacing="0" class="dtTABLE"><tbody>
<tr valign="top"><th width="50%"><br /></th><th width="50%"><br /></th></tr>
<tr valign="top"><td width="50%"><br /></td><td width="50%"><br /></td></tr>
<tr valign="top"><td width="50%"><br /></td><td width="50%"><br /></td></tr>
<tr valign="top"><td width="50%"><br /></td><td width="50%"><br /></td></tr>
<tr valign="top"><td width="50%"><br /></td><td width="50%"><br /></td></tr>
<tr valign="top"><td width="50%"><br /></td><td width="50%"><br /></td></tr>
<tr valign="top"><td width="50%"><br /></td><td width="50%"><br /></td></tr>
</tbody></table>
</div>
</div>
Sagarhttp://www.blogger.com/profile/06686177535004668246noreply@blogger.com0tag:blogger.com,1999:blog-695416308230945063.post-4037414233638887582013-05-18T09:54:00.001-07:002014-04-01T00:16:44.586-07:00Date time functions in vb script<div dir="ltr" style="text-align: left;" trbidi="on">
It is very important that you know how to work with date and time in VBScript as most of the VBScript programs will have date and time involved in it.<br />
<br />
Below is the list of all date and time functions in vb script.<br />
<ol style="text-align: left;">
<li>date</li>
<li>dateadd</li>
<li>datediff</li>
<li>datepart</li>
<li>dateserial</li>
<li>datevalue</li>
<li>day</li>
<li>hour</li>
<li>minute</li>
<li>second</li>
<li>month </li>
<li>monthname</li>
<li>time</li>
<li>timeserial</li>
<li>timevalue</li>
<li>weekday</li>
<li>weekdayname</li>
<li>year</li>
</ol>
<div>
We are going to have a look at each of these functions and examples in VBScript.</div>
<div>
<pre><span style="color: #3f7f59;">'To find current system date</span>
<span style="color: #7f0055; font-weight: bold;">msgbox</span> <span style="color: #2a00ff;">" Current System date is -> "</span> & <span style="color: #7f0055; font-weight: bold;">date</span>
<span style="color: #3f7f59;">'****************************************************************************</span>
<span style="color: #3f7f59;">'To find the future or past date</span>
<span style="color: #7f0055; font-weight: bold;">msgbox</span> <span style="color: #2a00ff;">"Tommorrow's date will be "</span> & <span style="color: #7f0055; font-weight: bold;">dateadd</span>(<span style="color: #2a00ff;">"d"</span>,1,<span style="color: #7f0055; font-weight: bold;">date</span>)</pre>
</div>
<div>
<div>
<br /></div>
First argument is Interval Type and it can be of below types.<br />
<br />
yyyy - Year <br />
m - Month<br />
d - Day<br />
h - Hour <br />
n - Minute <br />
s - Second</div>
<div>
<br /></div>
<div>
'****************************************************************************<br />
<div>
<pre><span style="color: #3f7f59;">'To find the difference between 2 dates</span>
<span style="color: #7f0055; font-weight: bold;">msgbox</span> <span style="color: #2a00ff;">"Day Difference between today and tommorrow is "</span> & <span style="color: #7f0055; font-weight: bold;">datediff</span>(<span style="color: #2a00ff;">"yyyy"</span>,<span style="color: #2a00ff;">"9-jan-1986"</span>,<span style="color: #7f0055; font-weight: bold;">date</span>)</pre>
<pre>
</pre>
</div>
<div>
First argument is Interval Type and it can be of below types.<br />
<br />
yyyy - Year<br />
m - Month<br />
d - Day<br />
h - Hour<br />
n - Minute<br />
s - Second</div>
<div>
'****************************************************************************</div>
<div>
<pre><span style="color: #3f7f59;">'To find the current day like 1,2,3...28</span>
<span style="color: #7f0055; font-weight: bold;">msgbox</span> <span style="color: #2a00ff;">"Current day is -> "</span> & day(<span style="color: #7f0055; font-weight: bold;">date</span>)
<span style="color: #3f7f59;">'****************************************************************************</span>
<span style="color: #3f7f59;">'To find the current hour</span>
<span style="color: #7f0055; font-weight: bold;">msgbox</span> <span style="color: #2a00ff;">"current hour is -> "</span> & <span style="color: #7f0055; font-weight: bold;">hour</span>(<span style="color: #7f0055; font-weight: bold;">now</span>)
<span style="color: #3f7f59;">'****************************************************************************</span>
<span style="color: #3f7f59;">'To find the current minute</span>
<span style="color: #7f0055; font-weight: bold;">msgbox</span> <span style="color: #2a00ff;">"current minute is -> "</span> & <span style="color: #7f0055; font-weight: bold;">minute</span>(<span style="color: #7f0055; font-weight: bold;">now</span>)
<span style="color: #3f7f59;">'****************************************************************************</span>
<span style="color: #3f7f59;">'To find the current second</span>
<span style="color: #7f0055; font-weight: bold;">msgbox</span> <span style="color: #2a00ff;">"current second is -> "</span> & <span style="color: #7f0055; font-weight: bold;">second</span>(<span style="color: #7f0055; font-weight: bold;">now</span>)
<span style="color: #3f7f59;">'****************************************************************************</span>
<span style="color: #3f7f59;">'To find the current month number like 1,2....11,12</span>
<span style="color: #7f0055; font-weight: bold;">msgbox</span> <span style="color: #2a00ff;">"current month is -> "</span> & <span style="color: #7f0055; font-weight: bold;">month</span>(<span style="color: #7f0055; font-weight: bold;">now</span>)
<span style="color: #3f7f59;">'****************************************************************************</span>
<span style="color: #3f7f59;">'To find the month name like Jan, Feb.....Dec</span>
<span style="color: #7f0055; font-weight: bold;">msgbox</span> <span style="color: #2a00ff;">"current month name is -> "</span> & monthname(<span style="color: #7f0055; font-weight: bold;">month</span>(<span style="color: #7f0055; font-weight: bold;">now</span>))
<span style="color: #3f7f59;">'****************************************************************************</span>
<span style="color: #3f7f59;">'To find the current system time</span>
<span style="color: #7f0055; font-weight: bold;">msgbox</span> <span style="color: #2a00ff;">"current time is -> "</span> & <span style="color: #7f0055; font-weight: bold;">time</span>
<span style="color: #3f7f59;">'****************************************************************************</span>
<span style="color: #3f7f59;">'To find the weekday number like 1,2....7</span>
<span style="color: #7f0055; font-weight: bold;">msgbox</span> <span style="color: #2a00ff;">"current weekday is -> "</span> & <span style="color: #7f0055; font-weight: bold;">weekday</span>(<span style="color: #7f0055; font-weight: bold;">now</span>)
<span style="color: #3f7f59;">'****************************************************************************</span>
<span style="color: #3f7f59;">'To find the name of week day like sunday, monday...saturday</span>
<span style="color: #7f0055; font-weight: bold;">msgbox</span> <span style="color: #2a00ff;">"current weekday Name is -> "</span> & weekdayname(<span style="color: #7f0055; font-weight: bold;">weekday</span>(<span style="color: #7f0055; font-weight: bold;">now</span>))
<span style="color: #3f7f59;">'****************************************************************************</span>
<span style="color: #3f7f59;">'To find the current year</span>
<span style="color: #7f0055; font-weight: bold;">msgbox</span> <span style="color: #2a00ff;">"current year is ->"</span> & year(<span style="color: #7f0055; font-weight: bold;">now</span>)
<span style="color: #3f7f59;">'****************************************************************************</span>
<span style="color: #3f7f59;">'To find the parts of the given time stamp .equivalent to day, minute,hour, second etc</span>
<span style="color: #7f0055; font-weight: bold;">msgbox</span> <span style="color: #2a00ff;">"Day part of the current timestamp"</span> & <span style="color: #7f0055; font-weight: bold;">datepart</span>(<span style="color: #2a00ff;">"d"</span>,<span style="color: #7f0055; font-weight: bold;">now</span>)
<span style="color: #3f7f59;">'****************************************************************************</span>
<span style="color: #3f7f59;">'To convert the string to date</span>
<span style="color: #7f0055; font-weight: bold;">msgbox</span> <span style="color: #2a00ff;">"String to date -> "</span> & <span style="color: #7f0055; font-weight: bold;">datevalue</span>(<span style="color: #2a00ff;">"09-jan-1986"</span>)
<span style="color: #3f7f59;">'****************************************************************************</span>
<span style="color: #3f7f59;">'To convert the string to time</span>
<span style="color: #7f0055; font-weight: bold;">msgbox</span> <span style="color: #2a00ff;">"String to time-> "</span> & <span style="color: #7f0055; font-weight: bold;">TimeValue</span>(<span style="color: #2a00ff;">"7:15:49 PM"</span>)
<span style="color: #3f7f59;">'****************************************************************************</span>
<span style="color: #3f7f59;">'To create date from numbers </span>
<span style="color: #7f0055; font-weight: bold;">msgbox</span> <span style="color: #7f0055; font-weight: bold;">dateserial</span>(1986,01,09)
<span style="color: #3f7f59;">'****************************************************************************</span>
<span style="color: #3f7f59;">'To create time from numbers</span>
<span style="color: #7f0055; font-weight: bold;">msgbox</span> <span style="color: #7f0055; font-weight: bold;">timeserial</span>(17,01,09)
<span style="color: #3f7f59;">'****************************************************************************</span></pre>
</div>
</div>
</div>
Sagarhttp://www.blogger.com/profile/06686177535004668246noreply@blogger.com0tag:blogger.com,1999:blog-695416308230945063.post-3342105054827963142013-05-18T09:45:00.001-07:002014-04-01T00:08:06.544-07:00String functions in vbscript<div dir="ltr" style="text-align: left;" trbidi="on">
Below is the list of string functions in vb script<br />
<ol style="text-align: left;">
<li>lcase</li>
<li>ucase</li>
<li>len</li>
<li>left</li>
<li>right</li>
<li>mid</li>
<li>ltrim</li>
<li>rtrim</li>
<li>trim</li>
<li>replace</li>
<li>strreverse</li>
<li>string</li>
<li>Instr</li>
<li>Instrrev</li>
<li>strcomp</li>
</ol>
<div>
We are going to have a look at each String function mentioned above.</div>
<div>
<h2 style="text-align: left;">
Substring Extraction functions</h2>
Some of the commonly used string manipulation functions are given below.<br />
<ul>
<li><b>right</b></li>
<li><b>mid</b></li>
<li><b>left</b></li>
</ul>
<div>
All of the above functions are frequently used when performing any string operations in vbscript.</div>
<div>
All of the above functions extract the part of the string / Sub string.</div>
<div>
<br /></div>
<div>
Right function returns the fixed number of characters from right side of the string.</div>
<div>
Left function returns the fixed number of characters from left side of the string.</div>
<div>
Mid function can be used to get the characters/ sub string from the left, right or middle part of the string.</div>
<br />
<br />
Examples -<br />
<br />
<pre>myString = <span style="color: #2a00ff;">"Sachin Plays Cricket"</span>
<span style="color: #7f0055; font-weight: bold;">print</span> <span style="color: #7f0055; font-weight: bold;">right</span>(myString,7)
<span style="color: #3f7f59;">'will return the 7 characters from the right side of myString </span>
<span style="color: #3f7f59;">'Cricket</span>
<span style="color: #7f0055; font-weight: bold;">print</span> <span style="color: #7f0055; font-weight: bold;">left</span>(myString,6)
<span style="color: #3f7f59;">'will return the 6 characters from the left side of myString</span>
<span style="color: #3f7f59;">'Sachin</span>
<span style="color: #7f0055; font-weight: bold;">print</span> <span style="color: #7f0055; font-weight: bold;">mid</span>(myString,8,5)
<span style="color: #3f7f59;">'will return the 5 characters from the 8th position of myString</span>
<span style="color: #3f7f59;">'Plays</span></pre>
<br />
Syntax -<br />
Second parameter in left and right function tells how many characters to return from the string.<br />
In mid functions there are 2 parameters. First parameter tells from which position of the string we have to get the characters and second parameter tells how many characters to return.<br />
<h2 style="text-align: left;">
Converting Case of Strings</h2>
We can convert the string from lower case to upper case and vice versa using <b>ucase </b>and <b>lcase </b>functions in VBScript.<br />
<br />
<pre><span style="color: #7f0055; font-weight: bold;">str</span> = <span style="color: #2a00ff;">"We are learning strings in VBScript"</span>
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> <span style="color: #7f0055; font-weight: bold;">lcase</span>(<span style="color: #7f0055; font-weight: bold;">str</span>)
<span style="color: #3f7f59;">'It will print - we are learning strings in vbscript</span>
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> <span style="color: #7f0055; font-weight: bold;">ucase</span>(<span style="color: #7f0055; font-weight: bold;">str</span>)</pre>
'It will print - WE ARE LEARNING STRINGS IN VBSCRIPT<br />
<div>
<br /></div>
<h2 style="text-align: left;">
Replacing part of the string</h2>
Replace function can be used to replace the part of the string with other string. Syntax of replace is given below.</div>
<div>
<br /></div>
<div>
Replace(mainString,stringToFind,replaceString[,start_Index[,Replace_count[,comparison_mode]]])</div>
<div>
<br /></div>
<div>
More information on the parameters is given below.</div>
<div>
<ol style="text-align: left;">
<li>mainString - This is the original string</li>
<li>stringToFind - This is the string which will be searched and replaced</li>
<li>replaceString - This is the string which will replace other string in the original string</li>
<li>start_Index - Index position of the original string from where you have to search it</li>
<li>Replace_Count - How many occurrences of the string you want to replace</li>
<li>Comparison_Mode - Binary (Case Sensitive) or textual comparison (Case Insensitive). by default it is binary comparison.</li>
</ol>
</div>
<div>
<br /></div>
<div>
Examples -<br />
<br />
<pre>mainString = <span style="color: #2a00ff;">"Sachin plays cricket"</span>
<span style="color: #7f0055; font-weight: bold;">msgbox</span> replace(mainString,<span style="color: #2a00ff;">"Sachin"</span>,<span style="color: #2a00ff;">"Arjun"</span>)
<span style="color: #3f7f59;">'Prints Arjun plays cricket.</span></pre>
</div>
<div>
<br /></div>
<div>
<h2>
Finding the length of the string</h2>
Len function is used to find the length of the string.<br />
<br />
Example -<br />
<pre>mainString = <span style="color: #2a00ff;">"Sachin plays cricket"</span>
<span style="color: #7f0055; font-weight: bold;">msgbox</span> <span style="color: #7f0055; font-weight: bold;">len</span>(mainString)
<span style="color: #3f7f59;">'Prints 20</span></pre>
<br />
<h2>
Trim functions of the string</h2>
Trim functions are used to remove the blank spaces from the beginning and ending of the string.<br />
There are 3 functions in this category.<br />
<br />
<ol style="text-align: left;">
<li>ltrim - removes blank spaces from left side of the string</li>
<li>rtrim - removes blank spaces from right side of the string</li>
<li>trim - removes blank spaces from both left and right side of the string</li>
</ol>
<br />
Example -<br />
<pre>mainString = <span style="color: #2a00ff;">" Sachin plays cricket "</span>
<span style="color: #7f0055; font-weight: bold;">msgbox</span> <span style="color: #7f0055; font-weight: bold;">ltrim</span>(mainString)
<span style="color: #3f7f59;">'Prints "Sachin plays cricket "</span></pre>
<br />
msgbox rtrim(mainString)<br />
<pre><span style="color: #3f7f59;">'Prints " Sachin plays cricket"</span>
<span style="color: #7f0055; font-weight: bold;">msgbox</span> <span style="color: #7f0055; font-weight: bold;">trim</span>(mainString)
<span style="color: #3f7f59;">'Prints "Sachin plays cricket"</span></pre>
<div>
<h2>
Reverse the string in VBScript</h2>
We can use strreverse function to reverse the string in VBScript.</div>
<div>
<br /></div>
<div>
Example - </div>
<div>
<br /></div>
<div>
<pre>mainString = <span style="color: #2a00ff;">"Sachin"</span>
<span style="color: #7f0055; font-weight: bold;">msgbox</span> strreverse(mainString)
<span style="color: #3f7f59;">'Prints "nihcaS"</span></pre>
</div>
<div>
<div>
<h2>
String function in VBScript</h2>
We can use string function to get the specified character n times.</div>
<div>
<br /></div>
<div>
Example - </div>
</div>
<div>
<br /></div>
<div>
<pre><span style="color: #7f0055; font-weight: bold;">msgbox</span> <span style="color: #7f0055; font-weight: bold;">String</span>(5,<span style="color: #2a00ff;">"*"</span>)
<span style="color: #3f7f59;">'prints *****</span></pre>
</div>
<div>
<br /></div>
<div>
<br />
<div>
<h2>
Instr function in VBScript</h2>
We can use instr function to find the substring in given string. Searching happens from the beginning of the string.</div>
<div>
<br /></div>
<div>
<b>Syntax of Instr</b></div>
<div>
<pre><span style="color: #7f0055; font-weight: bold;">InStr</span>([start,]string1,string2[,<span style="color: #7f0055; font-weight: bold;">compare</span>])
<span style="color: #7f0055; font-weight: bold;">msgbox</span> <span style="color: #7f0055; font-weight: bold;">instr</span>(1,<span style="color: #2a00ff;">"sagaar"</span>,<span style="color: #2a00ff;">"g"</span>)
<span style="color: #3f7f59;">'prints 3</span></pre>
</div>
</div>
<div>
<br /></div>
<h2>
Instrrev function in VBScript</h2>
<div>
We can use instrrev function to find the substring in given string. Searching happens from the end of the string but the position of the character is counted from the beginning of the string.</div>
<div>
<br /></div>
<div>
<b>Syntax of Instrrev</b><br />
<b><br /></b></div>
<pre>InStrRev(string1,string2[,start[,<span style="color: #7f0055; font-weight: bold;">compare</span>]])
<span style="color: #7f0055; font-weight: bold;">msgbox</span> instrrev(<span style="color: #2a00ff;">"sagaar"</span>,<span style="color: #2a00ff;">"g"</span>)
<span style="color: #3f7f59;">'prints 3</span></pre>
<br />
<br />
<h2>
Comparing 2 strings in VBScript</h2>
<div>
We can use <b>StrComp </b>function to compare two strings.</div>
<div>
The StrComp function returns the values based upon comparison result.</div>
<div>
<ol style="text-align: left;">
<li>-1 (if string1 < string2)</li>
<li>0 (if string1 = string2)</li>
<li>1 (if string1 > string2)</li>
<li>Null (if string1 or string2 is Null)</li>
</ol>
</div>
The syntax of the StrComp function is -></div>
<div>
<br /></div>
<div>
<pre><span style="color: #7f0055; font-weight: bold;">StrComp</span> (string1, string2 [, comparison_mode])</pre>
</div>
<div>
<br /></div>
<div>
The last parameter determines whether comparison is binary or textual. By default it is binary comparison (Case Sensitive).</div>
<div>
<br /></div>
<div>
<div>
<pre><span style="color: #7f0055; font-weight: bold;">msgbox</span> <span style="color: #7f0055; font-weight: bold;">strcomp</span>(<span style="color: #2a00ff;">"Amol"</span>,<span style="color: #2a00ff;">"Sagar"</span>) <span style="color: #3f7f59;">'prints -1..since ascii value of A is less than that of S</span>
<span style="color: #7f0055; font-weight: bold;">msgbox</span> <span style="color: #7f0055; font-weight: bold;">strcomp</span>(<span style="color: #2a00ff;">"sagar"</span>,<span style="color: #2a00ff;">"sagar"</span>) <span style="color: #3f7f59;">'prints 0</span>
<span style="color: #7f0055; font-weight: bold;">msgbox</span> <span style="color: #7f0055; font-weight: bold;">strcomp</span>(<span style="color: #2a00ff;">"sagar"</span>,<span style="color: #2a00ff;">"Amol"</span>) <span style="color: #3f7f59;">'prints 1</span></pre>
</div>
<div>
<br /></div>
</div>
</div>
Sagarhttp://www.blogger.com/profile/06686177535004668246noreply@blogger.com0tag:blogger.com,1999:blog-695416308230945063.post-58961709882459536362013-05-18T09:39:00.003-07:002014-04-01T00:13:44.402-07:00Maths functions in vbscript<div dir="ltr" style="text-align: left;" trbidi="on">
Below is the list of all maths functions in vb script<br />
<br />
<ol>
<li>Abs </li>
<li>Atn </li>
<li>Cos </li>
<li>Exp </li>
<li>Fix </li>
<li>Int </li>
<li>Log </li>
<li>Rnd </li>
<li>Sgn </li>
<li>Sin </li>
<li>Sqr </li>
<li>Tan </li>
<li>round</li>
</ol>
<div>
Let us have a look at each of these functions with examples.</div>
<div>
<br /></div>
<div>
<div>
<pre><span style="color: #3f7f59;">'To find the absolute value </span>
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> <span style="color: #7f0055; font-weight: bold;">Abs</span>(-11) <span style="color: #3f7f59;">'prints 11</span>
<span style="color: #3f7f59;">'To round the number</span>
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> round(22.346,2) <span style="color: #3f7f59;">'prints 22.35</span>
<span style="color: #3f7f59;">'To find the square root of the number</span>
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> <span style="color: #7f0055; font-weight: bold;">Sqr</span> (4) <span style="color: #3f7f59;">'prints 2</span>
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> <span style="color: #7f0055; font-weight: bold;">Exp</span>(2) <span style="color: #3f7f59;">'e^2</span></pre>
</div>
<div>
<br /></div>
<div>
Difference between int and fix is that - If the number is negative, int will return smallest possible integer value while fix will return largest possible integer value </div>
<div>
For positive numbers, both int and fix work the same way.<br />
<br /></div>
<div>
<pre><span style="color: #7f0055; font-weight: bold;">Msgbox</span> <span style="color: #7f0055; font-weight: bold;">Int</span> (-8.4) <span style="color: #3f7f59;">' returns -9</span>
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> <span style="color: #7f0055; font-weight: bold;">Fix</span>(-8.4) <span style="color: #3f7f59;">'returns -8</span>
<span style="color: #3f7f59;">'Calculates natural logarithm to the base e</span>
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> <span style="color: #7f0055; font-weight: bold;">Log</span>(10)
<span style="color: #3f7f59;">'gets the random number </span>
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> <span style="color: #7f0055; font-weight: bold;">Rnd</span>()
<span style="color: #3f7f59;">'We must use Randomize function before Rnd to get different values</span>
<span style="color: #3f7f59;">'To get the random numbers between 2 integers</span>
max=100
min=1
<span style="color: #7f0055; font-weight: bold;">Randomize</span>
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> (<span style="color: #7f0055; font-weight: bold;">Int</span>((max-min+1)*<span style="color: #7f0055; font-weight: bold;">Rnd</span>+min))</pre>
</div>
<div>
<br /></div>
<div>
'This functions returns the integer number -1,0 or 1 depending upon the sign of the number.</div>
<div>
'If the sign of the number is negative, -1</div>
<div>
'if the number is zero , 0</div>
<div>
'If the sign of the number is positive, 1</div>
<div>
<br /></div>
<div>
<pre><span style="color: #7f0055; font-weight: bold;">Msgbox</span> <span style="color: #7f0055; font-weight: bold;">Sgn</span>(-11) <span style="color: #3f7f59;">'prints -1</span>
<span style="color: #3f7f59;">'Used for geometric calculations</span>
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> <span style="color: #7f0055; font-weight: bold;">Sin</span>(90)
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> <span style="color: #7f0055; font-weight: bold;">Tan</span>(45)
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> <span style="color: #7f0055; font-weight: bold;">Atn</span>(45)
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> <span style="color: #7f0055; font-weight: bold;">Cos</span>(0)</pre>
</div>
<div>
<br /></div>
</div>
</div>
Sagarhttp://www.blogger.com/profile/06686177535004668246noreply@blogger.com0tag:blogger.com,1999:blog-695416308230945063.post-88764355598052232772013-05-18T09:31:00.000-07:002014-04-03T04:16:33.310-07:00File system object in vb script<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
Filesystemobject can be used to work with drives, folders, and files.<br />
<br />
Filesystemobject has methods and properties that allow us to create, delete,
gain information about, and generally manipulate drives, folders, and files.<br />
<br />
Important Methods of filesystemobject in vb script<br />
<ol style="text-align: left;">
<li>CopyFile</li>
<li>CopyFolder </li>
<li>CreateFolder</li>
<li>CreateTextFile</li>
<li>DeleteFile</li>
<li>DeleteFolder </li>
<li>DriveExists</li>
<li>FileExists</li>
<li>FolderExists</li>
<li>GetAbsolutePathName</li>
<li>GetBaseName </li>
<li>GetDrive</li>
<li>GetDriveName</li>
<li>GetExtensionName</li>
<li>GetFile</li>
<li>GetFileName</li>
<li>GetFolder </li>
<li>GetParentFolderName</li>
<li>GetSpecialFolder</li>
<li>GetTempName</li>
<li>MoveFile</li>
<li>MoveFolder</li>
<li>OpenTextFile </li>
</ol>
<h2 style="text-align: left;">
<span style="font-size: large;">
Working with files</span></h2>
<br />
<b><u>Creating and writing to file</u></b><br />
You can create a text file using this object.<br />
<br />
<pre><span style="color: #7f0055; font-weight: bold;">Set</span> obj = <span style="color: #7f0055; font-weight: bold;">createobject</span>(<span style="color: #2a00ff;">"scripting.filesystemobject"</span>)
<span style="color: #7f0055; font-weight: bold;">set</span> f = obj.createtextfile(<span style="color: #2a00ff;">"g:\salunke.txt"</span>) <span style="color: #3f7f59;">' create a file</span>
f.write <span style="color: #7f0055; font-weight: bold;">str</span> <span style="color: #3f7f59;">' write some data into file</span>
<span style="color: #7f0055; font-weight: bold;">set</span> f= <span style="color: #7f0055; font-weight: bold;">nothing</span> <span style="color: #3f7f59;">' release memory</span>
<span style="color: #7f0055; font-weight: bold;">set</span> obj = <span style="color: #7f0055; font-weight: bold;">nothing</span></pre>
<br />
<b><u>Reading from the file character by character</u></b><br />
<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #7f0055; font-weight: bold;">Set</span> fo = <span style="color: #7f0055; font-weight: bold;">createobject</span>(<span style="color: #2a00ff;">"scripting.filesystemobject"</span>)<br />
<pre> <span style="color: #7f0055; font-weight: bold;">set</span> stream1= fo.OpenTextFile(<span style="color: #2a00ff;">"c:\abc.txt"</span>,1)
<span style="color: #7f0055; font-weight: bold;">msgbox</span> stream1.AtEndOfStream
<span style="color: #7f0055; font-weight: bold;">Do</span> <span style="color: #7f0055; font-weight: bold;">While</span> (stream1.AtEndOfStream <> <span style="color: #7f0055; font-weight: bold;">true</span> )
<span style="color: #7f0055; font-weight: bold;">msgbox</span> stream1.Read(1)
<span style="color: #7f0055; font-weight: bold;">loop</span>
stream1.<span style="color: #7f0055; font-weight: bold;">Close</span>
<span style="color: #7f0055; font-weight: bold;">Set</span> stream1 = <span style="color: #7f0055; font-weight: bold;">nothing</span></pre>
<br />
<b><u>Appending data to file</u></b><br />
<b><u><br /></u></b>
<span class="Apple-tab-span" style="white-space: pre;"> </span><span style="color: #7f0055; font-weight: bold;">set</span> stream1= fo.OpenTextFile(<span style="color: #2a00ff;">"c:\abc.txt"</span>,8)<br />
<pre> stream1.Write(<span style="color: #2a00ff;">"append it"</span>)
stream1.<span style="color: #7f0055; font-weight: bold;">Close</span>
<span style="color: #7f0055; font-weight: bold;">Set</span> stream1 = <span style="color: #7f0055; font-weight: bold;">nothing</span>
<span style="color: #7f0055; font-weight: bold;">Set</span> fo = <span style="color: #7f0055; font-weight: bold;">nothing</span></pre>
<br />
<h2 style="text-align: left;">
<span style="font-size: large;">
Working with Folders</span></h2>
<div>
We can create, delete folders.</div>
<div>
<h2 style="text-align: left;">
<span style="font-size: large;">
Working with Drives</span></h2>
</div>
<div>
<br /></div>
<br />
<br />
<br /></div>
Sagarhttp://www.blogger.com/profile/06686177535004668246noreply@blogger.com0tag:blogger.com,1999:blog-695416308230945063.post-83762884354488123292013-05-18T08:50:00.003-07:002014-04-03T04:11:49.318-07:00Dictionary Object in vb script Example<div dir="ltr" style="text-align: left;" trbidi="on">
Dictionary object is used to store the data in key-item pairs in qtp.<br />
<br />
A Dictionary object is just like an associative array.<br />
It stores the data in the form of key-item pairs.<br />
Each key has some data item associated with it.<br />
Key can be integer or string format.<br />
Data item can be integer or string or array of variants. It may contain other dictionary itself.<br />
<br />
<b><u>Methods of Dictionary Object</u></b><br />
<br />
Add ---------> Adds new key-item pair in the dictionary object<br />
Exists --------> returns true if the given key exists in the dictionary object<br />
Items --------> returns the array containing all items in the dictionary object<br />
Keys --------> returns the array containing all keys in the dictionary object<br />
Remove -----> removes the key-item pair with given key from the dictionary object<br />
RemoveAll --> removes all key-item pairs from the dictionary object<br />
<br />
<b><u>Properties of Dictionary Object</u></b><br />
<br />
Count-----------> returns the total number of keys in the dictionary object<br />
Item-------------> assigns or returns the item value with given key from the dictionary object<br />
Key-------------> sets the new key value for the given key<br />
CompareMode -> assigns or returns the comparison mode for comparing string keys in a Dictionary object.<br />
<br />
<b><u>Example with dictionary object in VBScript</u></b><br />
<br />
<pre><span style="color: #3f7f59;">'create new dictionary object</span>
<span style="color: #7f0055; font-weight: bold;">Set</span> dictionaryObject = <span style="color: #7f0055; font-weight: bold;">CreateObject</span>(<span style="color: #2a00ff;">"Scripting.Dictionary"</span>)
dictionaryObject.CompareMode = 1
<span style="color: #3f7f59;">'add some key-item pairs</span>
dictionaryObject.Add <span style="color: #2a00ff;">"1"</span>, <span style="color: #2a00ff;">"Sagar"</span>
dictionaryObject.Add <span style="color: #2a00ff;">"2"</span>, <span style="color: #2a00ff;">"Amol"</span>
dictionaryObject.Add <span style="color: #2a00ff;">"3"</span>, <span style="color: #2a00ff;">"Ganesh"</span>
dictionaryObject.Key(<span style="color: #2a00ff;">"3"</span>) = <span style="color: #2a00ff;">"Bro"</span>
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> dictionaryObject.Item(<span style="color: #2a00ff;">"Bro"</span>)
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> dictionaryObject.Item(<span style="color: #2a00ff;">"bro"</span>)
<span style="color: #7f0055; font-weight: bold;">If</span> dictionaryObject.Exists(<span style="color: #2a00ff;">"1"</span>) <span style="color: #7f0055; font-weight: bold;">Then</span>
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> <span style="color: #2a00ff;">"Dictionary contains key 1"</span>
<span style="color: #7f0055; font-weight: bold;">Else</span>
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> <span style="color: #2a00ff;">"Dictionary does not contain key 1"</span>
<span style="color: #7f0055; font-weight: bold;">End</span> <span style="color: #7f0055; font-weight: bold;">If</span>
<span style="color: #3f7f59;">'Display keys and items in the dictionary</span>
<span style="color: #7f0055; font-weight: bold;">for</span> <span style="color: #7f0055; font-weight: bold;">each</span> k <span style="color: #7f0055; font-weight: bold;">in</span> dictionaryObject.keys
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> k & <span style="color: #2a00ff;">" - "</span> & dictionaryObject.item(k)
<span style="color: #7f0055; font-weight: bold;">next</span>
<span style="color: #3f7f59;">'Display items</span>
<span style="color: #7f0055; font-weight: bold;">for</span> <span style="color: #7f0055; font-weight: bold;">each</span> i <span style="color: #7f0055; font-weight: bold;">in</span> dictionaryObject.items
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> i
<span style="color: #7f0055; font-weight: bold;">next</span>
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> <span style="color: #2a00ff;">"Total number of keys in the dictionary are -> "</span> & dictionaryObject.Count
<span style="color: #3f7f59;">'Remove the key-item pair with key = 1 from the dictionary object</span>
dictionaryObject.Remove(<span style="color: #2a00ff;">"1"</span>)
<span style="color: #3f7f59;">'Remove all key-item pairs from the dictionary object</span>
dictionaryObject.RemoveAll
<span style="color: #3f7f59;">'Release the object</span>
<span style="color: #7f0055; font-weight: bold;">Set</span> dictionaryObject = <span style="color: #7f0055; font-weight: bold;">nothing</span></pre>
<div>
<br /></div>
Please give your inputs, suggestions, feedback to Us about above VBScript topic. We value your thoughts.<br />
<br /></div>
Sagarhttp://www.blogger.com/profile/06686177535004668246noreply@blogger.com0tag:blogger.com,1999:blog-695416308230945063.post-62781981524162773312013-05-18T08:29:00.001-07:002014-04-03T04:07:02.219-07:00Regular Expressions in vb script example<div dir="ltr" style="text-align: left;" trbidi="on">
Regular expressions Basics<br />
<br />
<b><u>Definition </u></b>:<br />
A regular expression is a pattern of characters(meta characters and special characters).<br />
<div>
<br /></div>
<div>
<b><u>General Applications of Regular Expressions </u></b>:<br />
Where are regular expressions used.</div>
<div>
<ol>
<li>Pattern Matching in Strings</li>
<li>To find the occurrences of one string/pattern in given string.</li>
<li>To replace the patterns with another string in a given text</li>
</ol>
</div>
<div>
All programming languages support the use of regular expressions. </div>
<div>
<br />
<b><u>Examples on Regular Expressions</u></b>: </div>
<div>
As I said earlier We use regular expressions to check if the given string matches the specified pattern.<br />
For example - Consider a scenario where you have to validate that the given string should be a valid email address.<br />
<br />
So list of valid email addresses are - reply2sagar@gmail.com, ayx@jjjj.in etc<br />
Some of the invalid email addresses are - kjkjj@fdff, @dfdf.in etc<br />
<br />
With the help of regular expression, you can easily validate the email address.<br />
<br />
<b><u>Syntax of Regular Expression in VBScript:</u></b><br />
To write any VBScript program involving regular expressions, you will have to follow below steps.</div>
<div>
<ol>
<li>Create a regular expression object (RegExp)</li>
<li>Define the pattern using RegExp object's pattern property.</li>
<li>Use test method to check whether the given string matches with the pattern specified in step 2.</li>
</ol>
<pre><span style="color: #3f7f59;">'Create the regular expression object</span>
<span style="color: #7f0055; font-weight: bold;">Set</span> myRegEx = <span style="color: #7f0055; font-weight: bold;">New</span> RegExp
<span style="color: #3f7f59;">'Specify the pattern (Regular Expression)</span>
myRegEx.Pattern = <span style="color: #2a00ff;">"[a-z0-9]+@[a-z]+\.[a-z]+"</span>
<span style="color: #3f7f59;">'Specify whether the matching is to be done with case sensitivity on or off.</span>
myRegEx.IgnoreCase = <span style="color: #7f0055; font-weight: bold;">True</span>
<span style="color: #3f7f59;">'Use Test method to see if the given string is matching with the pattern</span>
isMatched = myRegEx.Test(<span style="color: #2a00ff;">"reply2sagar@gmail.com"</span>)
Variable isMatched will be <span style="color: #7f0055; font-weight: bold;">true</span> <span style="color: #7f0055; font-weight: bold;">if</span> the <span style="color: #7f0055; font-weight: bold;">string</span> <span style="color: #2a00ff;">"reply2sagar@gmail.com"</span> matches <span style="color: #7f0055; font-weight: bold;">with</span> the given pattern
<span style="color: #2a00ff;">"[a-z0-9]+@[a-z]+\.[a-z]+"</span></pre>
</div>
<div>
<br /></div>
<div>
<br />
<b><u>Another example on Regular Expression.</u></b><br />
<br /></div>
searchString = "Sachin tendulkar is the master blaster. Sachin lives in Mumbai and likes to play cricket."<br />
<pre>searchPattern = <span style="color: #2a00ff;">"Sachin"</span>
<span style="color: #7f0055; font-weight: bold;">Set</span> reObject= <span style="color: #7f0055; font-weight: bold;">New</span> RegExp <span style="color: #3f7f59;">' Create a regular expression.</span>
reObject.Pattern = searchPattern <span style="color: #3f7f59;">' Set pattern.</span>
reObject.IgnoreCase = <span style="color: #7f0055; font-weight: bold;">True</span> <span style="color: #3f7f59;">' Set case insensitivity.</span>
reObject.Global = <span style="color: #7f0055; font-weight: bold;">True</span> <span style="color: #3f7f59;">' Set global applicability.</span>
<span style="color: #7f0055; font-weight: bold;">Set</span> Matches = reObject.Execute(searchString) <span style="color: #3f7f59;">' Execute search.</span>
<span style="color: #7f0055; font-weight: bold;">For</span> <span style="color: #7f0055; font-weight: bold;">Each</span> M <span style="color: #7f0055; font-weight: bold;">in</span> Matches
<span style="color: #7f0055; font-weight: bold;">Str</span> = <span style="color: #7f0055; font-weight: bold;">Str</span> & M.Firstindex & <span style="color: #2a00ff;">" -> "</span> & M.Value & vbCRLF
<span style="color: #7f0055; font-weight: bold;">Next</span>
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> <span style="color: #7f0055; font-weight: bold;">Str</span>
<span style="color: #7f0055; font-weight: bold;">Msgbox</span> <span style="color: #2a00ff;">"String after replacing -> "</span> & vbcrlf & reObject.replace(searchString,<span style="color: #2a00ff;">"Arjun"</span>)</pre>
<br />
<br />
<b><u>Below is the list of all meta characters used in regular expressions in VBScript</u></b><br />
<br />
\ - indicates that the next character would be a special character, a literal or a backreference<br />
<br />
^ - Input String should be matched at the beginning.<br />
<br />
$ - Input String should be matched at the end.<br />
<br />
* - Matches the preceding character zero or more times. It is same as {0,}.<br />
<br />
+ - Matches the preceding character one or more times. It is same as {1,}.<br />
<br />
? - Matches the preceding character zero or one time. It is same as {0,1}<br />
<br />
{i} - Matches the previous character exactly i times.<br />
<br />
{i,} - Matches the previous character at least i times and at most any time.<br />
<br />
{i,j} -Matches the previous character at least i times and at the most j times.<br />
<br />
. - Matches any single character except "\n".<br />
<br />
(pattern) - Matches pattern and captures the match that can be used in backreferences.<br />
p|q - Matches either p or q. Please note that p and q could be more complex regular expressions<br />
<br />
[pqr] - A character set. Matches any one of the character inside the brackets.<br />
<br />
[^pqr] - A negative character set. Matches any character not inside the brackets.<br />
<br />
[p-z] - A range of characters. Matches any character in the specified range i.e p,q,r,....x,y,z.<br />
<br />
[^p-z] - A negative range characters. Matches any character not in the specified range i.e. a,b,c...m,n,o<br />
<br />
\b - Matches the boundary of the word<br />
<br />
\B - Matches middle part of the word.<br />
<br />
\d - Matches a digit character. same as [0-9].<br />
<br />
\D - Matches a nondigit character. same as [^0-9].<br />
<br />
\f , \n and \r - Matches a form-feed character, newline and carriage character.<br />
<br />
\s - Matches any white space character including space, tab, form-feed. Equivalent to [ \f\n\r\t\v].<br />
<br />
\S - Matches any non-white space character. Equivalent to [^ \f\n\r\t\v].<br />
<br />
\t , \v - Matches a horizontal and vertical tab character.<br />
<br />
\w - Matches alpha numeric character including underscore. Equivalent to '[A-Za-z0-9_]'.<br />
<br />
\W - Matches any non - alpha numeric character. Equivalent to '[^A-Za-z0-9_]'.<br />
<br />
\number- A reference back to captured matches.<br />
<br />
*********************************************************************************<br />
*********************************************************************************<br />
<br />
<u><b>Some examples on regular expressions:</b></u><br />
<ol>
<li>To match the 10 digit mobile number -> \d{10}</li>
<li>To match email address -> \w+@\w+\.\w+</li>
</ol>
Please give your inputs, suggestions, feedback to Us about above VBScript topic. We value your thoughts.<br />
<br />
<br /></div>
Sagarhttp://www.blogger.com/profile/06686177535004668246noreply@blogger.com0