VBScript Tutorial 6
(VBScript Built inward Functions Part-2, File System Operations part-1)
I) VBScript Built inward Functions Part-2
20) LCase Function
Converts Upper representative 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 representative 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 information into Integer sub type.
When Conversion is required?
Whenever nosotros read information in addition to hence VBScript considers equally String type data, inward gild to perform
mathemetical calculations nosotros require 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.
23) Cdbl Function
Converts String type information 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 information sub type in addition to 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 2 strings based on compare mode.
It supports 3 agency comparison
Two agency comparing - True/False
Three agency comparing - >, <, =
Compare modes
0 for Binary (based on ANSI grapheme codes) comparison
1 for Textual comparison
Note:
Default compare fashion is Binary mode.
Result Criteria:
if str1 > str2 in addition to hence 1
if str1 < str2 in addition to hence -1
if str1 = str2 in addition to hence 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 inward a specified class.
Syntax:
Set variable = CreateObject("Class Value")
Ex:
Dim objFso, objExcel, objWord, objConnection, objRecordset, objDictionary, objQTP
'Create File organization object, it is used to function amongst Drives, Folders in addition to 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 lexicon 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 characteristic of operating system, used to function amongst Drives, Folders in addition to 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 information etc...
---------------------------
How halt user performs File organization Operations?
End user performs File organization operations manually amongst the assistance of Input devices, if it is ascendence trouble operating organization amongst the assistance of OS commands.
How to perform automatic file organization operations inward VBScript?
Using File organization Object nosotros tin perform automatic File organization operations.
-------------------------------
Create File System Object
Set objFso = CreateObject("Scripting.FileSystemObject")
Set - VBScript Statement
objFso - Variable
CreateObject - VBScript Built inward Function
"Scripting.FileSystemObject" - Class Value
Note: Class value entirely varies from ane 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 liberate the memory
Note:
Set objFso = Nothing contention for releasing the retentivity immediatly, otherwise memory
will endure released later re-launching the UFT tool.
It is non mandatory statement, simply best exercise (Standard)
Follow the iii steps:
i) Create Automation Object
ii) Use Object
(* role available methods in addition to properties)
iii) Close Object
---------------------------------------------
2) Check the being of QTP folder, if non exists in addition to hence do 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 being of QTP folder, if exists in addition to hence 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
----------------------------------
Sumber http://www.gcreddy.com/
(VBScript Built inward Functions Part-2, File System Operations part-1)
I) VBScript Built inward Functions Part-2
20) LCase Function
Converts Upper representative 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 representative 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 information into Integer sub type.
When Conversion is required?
Whenever nosotros read information in addition to hence VBScript considers equally String type data, inward gild to perform
mathemetical calculations nosotros require 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.
23) Cdbl Function
Converts String type information 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 information sub type in addition to 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 2 strings based on compare mode.
It supports 3 agency comparison
Two agency comparing - True/False
Three agency comparing - >, <, =
Compare modes
0 for Binary (based on ANSI grapheme codes) comparison
1 for Textual comparison
Note:
Default compare fashion is Binary mode.
Result Criteria:
if str1 > str2 in addition to hence 1
if str1 < str2 in addition to hence -1
if str1 = str2 in addition to hence 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 inward a specified class.
Syntax:
Set variable = CreateObject("Class Value")
Ex:
Dim objFso, objExcel, objWord, objConnection, objRecordset, objDictionary, objQTP
'Create File organization object, it is used to function amongst Drives, Folders in addition to 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 lexicon 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 characteristic of operating system, used to function amongst Drives, Folders in addition to 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 information etc...
---------------------------
How halt user performs File organization Operations?
End user performs File organization operations manually amongst the assistance of Input devices, if it is ascendence trouble operating organization amongst the assistance of OS commands.
How to perform automatic file organization operations inward VBScript?
Using File organization Object nosotros tin perform automatic File organization operations.
-------------------------------
Create File System Object
Set objFso = CreateObject("Scripting.FileSystemObject")
Set - VBScript Statement
objFso - Variable
CreateObject - VBScript Built inward Function
"Scripting.FileSystemObject" - Class Value
Note: Class value entirely varies from ane 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 liberate the memory
Note:
Set objFso = Nothing contention for releasing the retentivity immediatly, otherwise memory
will endure released later re-launching the UFT tool.
It is non mandatory statement, simply best exercise (Standard)
Follow the iii steps:
i) Create Automation Object
ii) Use Object
(* role available methods in addition to properties)
iii) Close Object
---------------------------------------------
2) Check the being of QTP folder, if non exists in addition to hence do 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 being of QTP folder, if exists in addition to hence 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
----------------------------------