Flat File Scripts
Computer File System
It is a feature of the Operating System, used to Create/Modify/view/delete
Drives, Folders and Files
OS Distribution
Operating System and Other Utilities
FileSystemObject
VBScript has Provided FileSystemObject to
perform file system operations through scripting
Dim objFso
'Creating an Automation Object in File System class, that can be used to
perform Operations on Computer File System
Set objFso=CreateObject("scripting.FileSystemObject")
1) Creating a File
Dim objFso
Set objFso=CreateObject("scripting.FileSystemObject")
objFso.CreateTextFile ("E:\Automatio.txt")
objFso.CreateTextFile ("E:\Automatio.doc")
objFso.CreateTextFile ("E:\Automatio.xls")
objFso.CreateTextFile ("E:\Automatio.pdf")
Note: We can Create other files also, but they act as
Text/Flat Files
Set objFile =
objFSO.CreateTextFile("E:\Automatio.txt")
2) Checking weather the File is
available or not, if not creating the File
strDirectory="E:\"
strFile="Automatio.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(strDirectory &
strFile) Then
Set objFolder =
objFSO.GetFolder(strDirectory)
Else
Set objFile =
objFSO.CreateTextFile("E:\Automatio.txt")
End if
3) Reading Data character by character
from a Flat File
Set objFSO =
CreateObject("Scripting.FileSystemObject")
Set objFile =
objFSO.OpenTextFile("E:\Automatio.txt", 1)
Do Until objFile.AtEndOfStream
strCharacters = objFile.Read(1)
msgbox strCharacters
Loop
4) Reading Data line by line from a Flat
File
Set objFSO =
CreateObject("Scripting.FileSystemObject")
Set objFile =
objFSO.OpenTextFile("E:\Automatio.txt", 1)
Do Until objFile.AtEndOfStream
strCharacters = objFile.Readline
msgbox strCharacters
Loop
5) Data Driven Testing by fetching Test
data directly from a Text file.
**************************************************
'Test Requirement: Data Driven Testing by Fetching Test data
directly from a Text file.
'Author: Rohit A.
'Date of Creation: 13-08-2010
'Pre-requisites:
'gcr.txt (Test Data)
'Test Flow:
'Creating an Automation Object in FileSystem class
'Opening the External Test Data file using the Object
'Read the Data & Split the Data
'Generating the Login Operation
'Pass Parameters
'*************************************************
Dim objFso, myFile, myLine, myField
Set objFso=CreateObject("Scripting.FileSystemObject")
Set myFile=objFso.OpenTextFile("C:\Documents and
Settings\auto.auto-9A12FBD3D9\Desktop\auto.txt",1) '1 for Read, 2-Write
& 8-Append
myFile.SkipLine
Do Until myFile.AtEndOfStream
myLine=myFile.ReadLine
myField=Split(myLine,",")
SystemUtil.Run "C:\Program Files\HP\QuickTest
Professional\samples\flight\app\flight4a.exe"
Dialog("text:=Login").Activate
Dialog("text:=Login").WinEdit("attached text:=Agent
Name:").Set myField(0)
Dialog("text:=Login").WinEdit("attached
text:=Password:").Set myField(1)
wait 2
Dialog("text:=Login").WinButton("text:=OK").Click
Window("text:=Flight Reservation").Close
Loop
myFile.Close
Set objFso=Nothing
6) Writing data to a text file
Dim Stuff, myFSO, WriteStuff, dateStamp
dateStamp = Date()
Stuff = "I am Preparing this
script: " &dateStamp
Set myFSO =
CreateObject("Scripting.FileSystemObject")
Set WriteStuff =
myFSO.OpenTextFile("e:\Automatio.txt", 8, True)
WriteStuff.WriteLine(Stuff)
WriteStuff.Close
SET WriteStuff = NOTHING
SET myFSO = NOTHING
7) Delete a text file
Set
objFSO=createobject("Scripting.filesystemobject")
Set txtFilepath =
objFSO.GetFile("E:\auto.txt")
txtFilepath.Delete()
8) Checking weather the File is
available or not, if available delete the File
strDirectory="E:\"
strFile="gcr.txt"
Set objFSO =
CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(strDirectory &
strFile) Then
Set objFile =
objFSO.Getfile(strDirectory & strFile)
objFile.delete ()
End if
9) Comparing two text files
Dim f1, f2
f1="e:\Automatio1.txt"
f2="e:\Automatio2.txt"
Public Function CompareFiles
(FilePath1, FilePath2)
Dim FS, File1, File2
Set FS =
CreateObject("Scripting.FileSystemObject")
If FS.GetFile(FilePath1).Size <>
FS.GetFile(FilePath2).Size Then
CompareFiles = True
Exit Function
End If
Set File1 =
FS.GetFile(FilePath1).OpenAsTextStream(1, 0)
Set File2 =
FS.GetFile(FilePath2).OpenAsTextStream(1, 0)
CompareFiles = False
Do While File1.AtEndOfStream = False
Str1 = File1.Read
Str2 = File2.Read
CompareFiles = StrComp(Str1, Str2, 0)
If CompareFiles <> 0 Then
CompareFiles = True
Exit Do
End If
Loop
File1.Close()
File2.Close()
End Function
Call Comparefiles(f1,f2)
If CompareFiles(f1, f2) = False Then
MsgBox "Files are identical."
Else
MsgBox "Files are different."
End If
10) Counting the number of times a word
appears in a file
sFileName="E:\auto.txt"
sString="automatio"
Const FOR_READING = 1
Dim oFso, oTxtFile, sReadTxt, oRegEx,
oMatches
Set oFso =
CreateObject("Scripting.FileSystemObject")
Set oTxtFile =
oFso.OpenTextFile(sFileName, FOR_READING)
sReadTxt = oTxtFile.ReadAll
Set oRegEx = New RegExp
oRegEx.Pattern = sString
oRegEx.IgnoreCase = bIgnoreCase
oRegEx.Global = True
Set oMatches = oRegEx.Execute(sReadTxt)
MatchesFound = oMatches.Count
Set oTxtFile = Nothing : Set oFso =
Nothing : Set oRegEx = Nothing
msgbox MatchesFound
11) Read a CSV File Using Database
Techniques
On Error Resume Next
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H0001
Set objConnection =
CreateObject("ADODB.Connection")
Set objRecordSet =
CreateObject("ADODB.Recordset")
strPathtoTextFile =
"C:\Databases\"
objConnection.Open
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & strPathtoTextFile & ";" & _
"Extended
Properties=""text;HDR=YES;FMT=Delimited"""
objRecordset.Open "SELECT * FROM
PhoneList.csv", _
objConnection, adOpenStatic, adLockOptimistic, adCmdText
Do Until objRecordset.EOF
Wscript.Echo
"Name: " & objRecordset.Fields.Item("Name")
Wscript.Echo
"Department: " & _
objRecordset.Fields.Item("Department")
Wscript.Echo
"Extension: " &
objRecordset.Fields.Item("Extension")
objRecordset.MoveNext
Loop
12) Read a Text File into an Array
Const ForReading = 1
Set objFSO =
CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile _
("e:\automatio.txt",
ForReading)
Do Until objTextFile.AtEndOfStream
strNextLine =
objTextFile.Readline
arrServiceList =
Split(strNextLine , ",")
Wscript.Echo
"Server name: " & arrServiceList(0)
For i = 1 to
Ubound(arrServiceList)
Wscript.Echo "Service: " & arrServiceList(i)
Next
Loop
13) 'Calculate size of a Text file
Dim objFso, File1,File2
File1="C:\Documents and Settings\1 RIGHATWAY\Desktop\xyz.txt"
Set objFso=CreateObject("
Scripting.FileSystemObject")
x= objFso.GetFile(File1).Size
Msgbox x& " Bytes"
14) 'Test Requirement: Open 1 to 10 orders
in Flight Reservation , Capture Customer names and Export into a Text file
'Test Flow:
'Login Operation
'Open Order Operation and form the Loop to open 1 to 10 Orders
'Capture Cusomer names using GetROProperty Method
'Create File system Object and Open the Text file using the Object and
Export Cusomer names
'----------------------------------------------------------------------------
Option Explicit
Dim Order_Number, Customer_Name, objFso, myFile
Set objFso=CreateObject("Scripting.FileSystemObject")
Set myFile= objFso.CreateTextFile ("C:\Documents and Settings\1
RIGHATWAY\Desktop\abcNew.txt",2)
myFile.WriteLine "Cusomer Names"
myFile.WriteLine "--------------------"
If Not Window("Flight Reservation").Exist(3) Then
SystemUtil.Run "C:\Program Files\HP\QuickTest
Professional\samples\flight\app\flight4a.exe","","C:\Program
Files\HP\QuickTest Professional\samples\flight\app\","open"
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set "asdf"
Dialog("Login").WinEdit("Password:").SetSecure
"4c48590870466b8dc050bbd24e816890c747ccf8"
Dialog("Login").WinButton("OK").Click
End If
For Order_Number= 1 to 10 step 1
Window("Flight Reservation").Activate
Window("Flight Reservation").WinButton("Button").Click
Window("Flight Reservation").Dialog("Open
Order").WinCheckBox("Order No.").Set "ON"
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set
Order_Number
Window("Flight Reservation").Dialog("Open
Order").WinButton("OK").Click
Customer_Name = Window("Flight
Reservation").WinEdit("Name:").GetROProperty("text")
wait (2)
myFile.WriteLine Customer_Name
Next
myFile.Close
Set objFso=Nothing
15)
'******************************************************
'Test Requirement: Capturing all Buttons Names from the Login Dialog box and exporting to a
Text file
'Author: Rohit A.
'Date of Creation: 13-08-2010
'Pre-requasites:
'gcr.txt
'Test Flow:
'Creating an Automation Object in FileSystem class
'Opening the External Test Data file using the Object
'Creating Collection object and Capturing Button Names using the Object
'Writing Button Names to an External Text file
'********************************************************
Dim objFso, myFile, oButton, Buttons, TotButtons
Set objFso=CreateObject("Scripting.FileSystemObject")
Set myFile=objFso.OpenTextFile("C:\Documents and Settings\gcr.GCRC-9A12FBD3D9\Desktop\automate.txt",2)
'1 for Read, 2-Write & 8-Append
myFile.WriteLine "Button Names"
myFile.WriteLine "----------"
Set oButton=Description.Create
oButton("micclass").value="WinButton"
SystemUtil.Run "C:\Program Files\HP\QuickTest
Professional\samples\flight\app\flight4a.exe"
Set Buttons=Dialog("text:=Login").ChildObjects(oButton)
TotButtons=Buttons.Count
For i= 0 to TotButtons-1
myButton=Buttons(i).GetRoProperty("text")
myFile.WriteLine myButton
Next
myFile.Close
Set objFso=Nothing
16) Delete a Text file if it is an empty
file
Dim objFso, FilePath
FilePath="C:\Documents and Settings\automatio\Desktop\abc.txt"
Set objFso=CreateObject("Scripting.FileSystemObject")
FileSize=objFso.GetFile(FilePath).Size
'Msgbox FileSize
If FileSize=0 Then
objFso.DeleteFile(FilePath)
End If
Set objFso=Nothing
OS Distribution
FileSystemObject
VBScript has Provided FileSystemObject to perform file system operations through scripting
Dim objFso
'Creating an Automation Object in File System class, that can be used to perform Operations on Computer File System
Set objFso=CreateObject("scripting.FileSystemObject")
Set objFso=CreateObject("scripting.FileSystemObject")
objFso.CreateTextFile ("E:\Automatio.txt")
objFso.CreateTextFile ("E:\Automatio.doc")
objFso.CreateTextFile ("E:\Automatio.xls")
objFso.CreateTextFile ("E:\Automatio.pdf")
Note: We can Create other files also, but they act as Text/Flat Files
'Test Requirement: Data Driven Testing by Fetching Test data directly from a Text file.
'Author: Rohit A.
'Date of Creation: 13-08-2010
'Pre-requisites:
'gcr.txt (Test Data)
'Test Flow:
'Creating an Automation Object in FileSystem class
'Opening the External Test Data file using the Object
'Read the Data & Split the Data
'Generating the Login Operation
'Pass Parameters
'*************************************************
Dim objFso, myFile, myLine, myField
Set objFso=CreateObject("Scripting.FileSystemObject")
Set myFile=objFso.OpenTextFile("C:\Documents and Settings\auto.auto-9A12FBD3D9\Desktop\auto.txt",1) '1 for Read, 2-Write & 8-Append
myFile.SkipLine
Do Until myFile.AtEndOfStream
myLine=myFile.ReadLine
myField=Split(myLine,",")
SystemUtil.Run "C:\Program Files\HP\QuickTest Professional\samples\flight\app\flight4a.exe"
Dialog("text:=Login").Activate
Dialog("text:=Login").WinEdit("attached text:=Agent Name:").Set myField(0)
Dialog("text:=Login").WinEdit("attached text:=Password:").Set myField(1)
wait 2
Dialog("text:=Login").WinButton("text:=OK").Click
Window("text:=Flight Reservation").Close
Loop
myFile.Close
Set objFso=Nothing
13) 'Calculate size of a Text file
Dim objFso, File1,File2
File1="C:\Documents and Settings\1 RIGHATWAY\Desktop\xyz.txt"
Set objFso=CreateObject("
Scripting.FileSystemObject")
x= objFso.GetFile(File1).Size
Msgbox x& " Bytes"
14) 'Test Requirement: Open 1 to 10 orders in Flight Reservation , Capture Customer names and Export into a Text file
'Test Flow:
'Login Operation
'Open Order Operation and form the Loop to open 1 to 10 Orders
'Capture Cusomer names using GetROProperty Method
'Create File system Object and Open the Text file using the Object and Export Cusomer names
'----------------------------------------------------------------------------
Option Explicit
Dim Order_Number, Customer_Name, objFso, myFile
Set objFso=CreateObject("Scripting.FileSystemObject")
Set myFile= objFso.CreateTextFile ("C:\Documents and Settings\1 RIGHATWAY\Desktop\abcNew.txt",2)
myFile.WriteLine "Cusomer Names"
myFile.WriteLine "--------------------"
If Not Window("Flight Reservation").Exist(3) Then
SystemUtil.Run "C:\Program Files\HP\QuickTest Professional\samples\flight\app\flight4a.exe","","C:\Program Files\HP\QuickTest Professional\samples\flight\app\","open"
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set "asdf"
Dialog("Login").WinEdit("Password:").SetSecure "4c48590870466b8dc050bbd24e816890c747ccf8"
Dialog("Login").WinButton("OK").Click
End If
For Order_Number= 1 to 10 step 1
Window("Flight Reservation").Activate
Window("Flight Reservation").WinButton("Button").Click
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON"
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set Order_Number
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
Customer_Name = Window("Flight Reservation").WinEdit("Name:").GetROProperty("text")
wait (2)
myFile.WriteLine Customer_Name
Next
myFile.Close
Set objFso=Nothing
15)
'******************************************************
'Test Requirement: Capturing all Buttons Names from the Login Dialog box and exporting to a Text file
'Author: Rohit A.
'Date of Creation: 13-08-2010
'Pre-requasites:
'gcr.txt
'Test Flow:
'Creating an Automation Object in FileSystem class
'Opening the External Test Data file using the Object
'Creating Collection object and Capturing Button Names using the Object
'Writing Button Names to an External Text file
'********************************************************
Dim objFso, myFile, oButton, Buttons, TotButtons
Set objFso=CreateObject("Scripting.FileSystemObject")
Set myFile=objFso.OpenTextFile("C:\Documents and Settings\gcr.GCRC-9A12FBD3D9\Desktop\automate.txt",2) '1 for Read, 2-Write & 8-Append
myFile.WriteLine "Button Names"
myFile.WriteLine "----------"
Set oButton=Description.Create
oButton("micclass").value="WinButton"
SystemUtil.Run "C:\Program Files\HP\QuickTest Professional\samples\flight\app\flight4a.exe"
Set Buttons=Dialog("text:=Login").ChildObjects(oButton)
TotButtons=Buttons.Count
For i= 0 to TotButtons-1
myButton=Buttons(i).GetRoProperty("text")
myFile.WriteLine myButton
Next
myFile.Close
Set objFso=Nothing
16) Delete a Text file if it is an empty file
Dim objFso, FilePath
FilePath="C:\Documents and Settings\automatio\Desktop\abc.txt"
Set objFso=CreateObject("Scripting.FileSystemObject")
FileSize=objFso.GetFile(FilePath).Size
'Msgbox FileSize
If FileSize=0 Then
objFso.DeleteFile(FilePath)
End If
Set objFso=Nothing
No comments:
Post a Comment