VBScript Tutorial 6

VBScript Tutorial 6

(VBScript Built in Functions Part-2, File System Operations part-1)

I) VBScript Built in Functions Part-2

20) LCase Function

Converts Upper case values to lower case.

Ex:

Dim val
val = “HYDERABAD”
Msgbox LCase(val) ‘hyderabad
Msgbox LCase(“HYDERABAD”) ‘hyderabad
Msgbox LCase(“hyderabad”) ‘hyderabad
Msgbox LCase(“HYDerabad”) ‘hyderabad
Msgbox LCase(“HYD123”) ‘hyd123
Msgbox LCase(123) ‘123
—————————-
21) UCase Function

Converts Lower case values to Upper case

Ex:

Dim val
val = “HYDERABAD”
Msgbox UCase(val) ‘HYDERABAD
Msgbox UCase(“HYDERABAD”) ‘HYDERABAD
Msgbox UCase(“hyderabad”) ‘HYDERABAD
Msgbox UCase(“HYDerabad”) ‘HYDERABAD
Msgbox UCase(“HYD123”) ‘HYD123
Msgbox UCase(123) ‘123
—————————————
22) Cint Function

Converts String type data into Integer sub type.

When Conversion is required?

Whenever we read data then VBScript considers as String type data, in order to perform
mathemetical calculations we need to convert the data.

Ex:

Dim val, Tickets
val = InputBox (“Enter a Value”)
Msgbox VarType(val) ‘8 for String
val = Cint(val)
Msgbox VarType(val)

Tickets = Window(“Flight Reservation”).WinEdit(“Tickets:”).GetROProperty(“text”)
Msgbox VarType(Tickets) ‘8 for string
Msgbox VarType(Cint (Tickets)) ‘2 for Integer

Note: We can’t convert Alfa bytes into Integer sub type or double sub type.

VBScript Built Functions

23) Cdbl Function

Converts String type data into double sub type.

Ex:
Dim val, Price
val = InputBox (“Enter a Value”)
Msgbox VarType(val) ‘8 for String
val = Cdbl(val)
Msgbox VarType(val)

Price = Window(“Flight Reservation”).WinEdit(“Price:”).GetROProperty(“text”)
msgbox VarType(Price) ‘8 for String
Msgbox VarType(Cdbl(Price)) ‘5 for Double
——————————————-
24) VarType Function

It checks data sub type and returns constant based result.

Ex:

Dim a, b(3)
Msgbox VarType(a) ‘0 for Empty / Uninitialized
a = 100
Msgbox VarType(a) ‘2 for Integer
a = “100”
Msgbox VarType(a) ‘8 for String

Msgbox VarType(“abcd”) ‘8
Msgbox VarType(1.234)’5 for Double
Msgbox VarType(#10/10/2010#) ‘7 for date
Set a = CreateObject(“Scripting.FilesystemObject”)
Msgbox varType(a) ‘9 for Automation object
Msgbox varType(b)
b(0) = “abc”
b(1) = 123
b(2) = 1.23
b(3) = #10/10/2010#
Msgbox varType(b(0))’ 8
Msgbox varType(b(1)) ‘2
Msgbox varType(b(2))’5
Msgbox varType(b(3)) ‘7
————————————
25) Len Function

It checks length of a string or number.

Ex:

Dim a
a = “INDIA”
Msgbox Len(a) ‘5
Msgbox Len(“INDIA”) ‘5
Msgbox Len(120) ‘3
Msgbox Len(1.234) ‘5
Msgbox Len(#10/10/2010#) ’10
Msgbox Len(#10/10/10#)’10
Msgbox Len(#Sep/10/2010#) ‘9
Msgbox Len(#December/10/2010#) ’10
—————————————-
26) Split Function

Splits a String based on delimiter, default delimiter is space.

Dim a, b
a = “VB Script Language”
b = Split(a)
Msgbox IsArray(b)’True
Msgbox b(1) ‘Script
————————–
Dim a, b
a = “VB,Script,Language”
b = Split(a, “,”)
Msgbox IsArray(b)’True
Msgbox b(1) ‘Script
———————————–
Dim a, b
a = “VB#$%Script#$%Language”
b = Split(a, “#$%”)
Msgbox IsArray(b)’True
Msgbox b(1) ‘Script
—————————-
27) Join Function

Joins all elements of Array variable.

Ex:
Dim a(2)
a(0) = “VB”
a(1) =”Script”
a(2) = “Language”
Msgbox Join(a)
—————-
Dim a(2)
a(0) = “VB”
a(1) =”Script”
a(2) = “Language”
Msgbox Join(a,”@”)
——————————–
28) Lbound, UBound Functions

Lbound Returns lower boundary of Array

UBound Returns upper boundary fo Array

Ex:
Dim a, b(3)
a =Array (1, 2, 3, 4, 5, 2.3, “abc”)
Msgbox LBound(a)’0
Msgbox UBound(a)’6

b(1) = 1
b(2) = 2
b(3) = 1.2
Msgbox LBound(a)’0
Msgbox UBound(a)’6
—————————————
29) StrComp Function

It compares two strings based on compare mode.

It supports 3 way comparison

Two way comparison – True/False
Three way comparison – >, <, =

Compare modes

0 for Binary (based on ANSI character codes) comparison

1 for Textual comparison

Note:

Default compare mode is Binary mode.

Result Criteria:

if str1 > str2 then 1
if str1 < str2 then -1
if str1 = str2 then 0
——————-
Example:
Dim str1, str2
str1 = “UFT”
str2 = “uft”

Msgbox StrComp(str1, str2, 0) ‘-1
Msgbox StrComp(str1, str2) ‘-1
Msgbox StrComp(str1, str2, 1) ‘0

str1 = “uFT”
str2 = “Uft”

Msgbox StrComp(str1, str2, 0) ‘1
Msgbox StrComp(str1, str2) ‘1
Msgbox StrComp(str1, str2, 1) ‘0
————————————–
30) InputBox Function

31) MsgBox Function
Dim val
val = InputBox (“Enter a Value”)
Msgbox “Value is: “& val
——————–
a = 100
b = 200
Msgbox “Hello UFT”
Msgbox “Hello UFT ” & a
Msgbox “hello UFT ” & a & ” Hello VBScript”
msgbox a & b
——————————————
32) CreateObject Function

creats an Automation object in a specified class.

Syntax:

Set variable = CreateObject(“Class Value”)

Ex:

Dim objFso, objExcel, objWord, objConnection, objRecordset, objDictionary, objQTP

‘Create File system object, it is used to work with Drives, Folders and Files.
Set objFso = CreateObject(“Scripting.FileSystemObject”)

‘Create excel Application object, It is used to perform operations on Excel Application
Set objExcel = CreateObject(“Excel.Application”)

‘Create Word Application object, It is used to perform opertions on Word Application
Set objWord = CreateObject(“Word.Application”)

‘Create Database Connection object, It is used to connect to a database
Set objConnection = CreateObject(“Adodb.Connection”)

‘Create Database Recordset object, It is used to perform operations on database records
Set objRecordset = CreateObject(“Adodb.Recordset”)

‘Create dictionary object, It is used to define key, value pairs
Set objDictionary = CreateObject(“scripting.Dictionary”)

‘Create QTP Application object, It is used to automate UFT tool operations
Set objQTP = CreateObject(“QuickTest.Application”)

II) File System Operations

What is Computer File System?

It is a feature of operating system, used to work with Drives, Folders and Files.

Examples for File System operations

Create a Folder

Copy a Folder

Delete a Folder

Create a Text file

Delete a Text file

Read data

Write data

Compare data

Search for data etc…
—————————
How end user performs File system Operations?

End user performs File system operations manually with the help of Input devices, if it is command line operating system with the help of OS commands.

How to perform automatic file system operations in VBScript?

Using File system Object we can perform automatic File system operations.
——————————-
Create File System Object

Set objFso = CreateObject(“Scripting.FileSystemObject”)

Set – VBScript Statement

objFso – Variable

CreateObject – VBScript Built in Function

“Scripting.FileSystemObject” – Class Value

Note: Class value only varies from one object model to another.
————————————————————-
Examples:

1) Create a Folder

Dim objFso
Set objFso = CreateObject(“Scripting.FileSystemObject”)
objFso.CreateFolder “C:\Users\gcreddy\Desktop\QTP”
Set objFso = Nothing ‘To release the memory

Note:

Set objFso = Nothing statement for releasing the memory immediatly, otherwise memory
will be released after re-launching the UFT tool.

It is not mandatory statement, but best practice (Standard)

Follow the three steps:

i) Create Automation Object

ii) Use Object
(* use available methods and properties)

iii) Close Object
———————————————
2) Check the existence of QTP folder, if not exists then create the folder.

Dim objFso, FolderPath
FolderPath = “C:\Users\gcreddy\Desktop\QTP”
Set objFso = CreateObject(“Scripting.FileSystemObject”)

If Not objFso.FolderExists(FolderPath) Then
objFso.CreateFolder Folderpath
End If

Set objFso = Nothing
———————————————-
3) Copy a Folder

Dim objFso
Set objFso = CreateObject(“Scripting.FileSystemObject”)
objFso.CopyFolder “C:\Users\gcreddy\Desktop\QTP”, “C:\”

Set objFso = Nothing
—————
Dim objFso
Set objFso = CreateObject(“Scripting.FileSystemObject”)
objFso.CopyFolder “C:\Users\gcreddy\Desktop\QTP”, “C:\UFT”

Set objFso = Nothing
—————————————
4) Delete a Folder
Dim objFso
Set objFso = CreateObject(“Scripting.FileSystemObject”)
objFso.DeleteFolder “C:\Users\gcreddy\Desktop\QTP”
Set objFso = Nothing
————————————-
5) Check the existence of QTP folder, if exists then delete the folder.

Dim objFso
Set objFso = CreateObject(“Scripting.FileSystemObject”)

If objFso.FolderExists(“C:\Users\gcreddy\Desktop\QTP”) Then
objFso.DeleteFolder “C:\Users\gcreddy\Desktop\QTP”
End If

Set objFso = Nothing
———————————-

Follow me on social media: