Visit: www.gcreddy.
com for QTP Information
www.gcreddy.com
File System Operations
Computer File System
OS Distribution
FileSystemObject
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”)
Examples:
i) Create a Folder
Dim objFso
Set objFso=CreateObject(“scripting.FileSystemObject”)
objFso.CreateFolder “C:Documents and Settings1 RIGHATWAYDesktophyderabad”
ii) Check if the Folder Exist or not? If not create the Folder
Dim objFso, myFolder
myFolder=”C:Documents and Settings1 RIGHATWAYDesktophyderabad”
Set objFso=CreateObject(“scripting.FileSystemObject”)
If Not objFso.FolderExists(myFolder) Then
objFso.CreateFolder (myFolder)
End If
1
Visit: www.gcreddy.com for QTP Information
iii) Copy a Folder
Dim objFso, myFolder
myFolder=”C:Documents and Settings1 RIGHATWAYDesktophyderabad”
Set objFso=CreateObject(“scripting.FileSystemObject”)
objFso.CopyFolder myFolder,”E:abcd”
iv) Delete a folder
Dim objFso, myFolder
myFolder=”C:Documents and Settings1 RIGHATWAYDesktophyderabad”
Set objFso=CreateObject(“scripting.FileSystemObject”)
objFso.DeleteFolder( myFolder)
2nd
Dim objFso, myFolder
myFolder=”C:Documents and Settings1 RIGHATWAYDesktophyderabad”
Set objFso=CreateObject(“scripting.FileSystemObject”)
If objFso.FolderExists(myFolder) Then
objFso.DeleteFolder( myFolder)
End If
v) Return a Collection of Disk Drives
Dim objFso, colDrives
Set objFso=CreateObject(“scripting.FileSystemObject”)
Set colDrives=objFso.Drives
For Each oDrive in colDrives
Msgbox oDrive
Next
vi) Get available space on a Drive
2
Visit: www.gcreddy.com for QTP Information
Dim objFso
Set objFso=CreateObject(“scripting.FileSystemObject”)
Set myDrive=objFso.GetDrive(“D:”)
Msgbox myDrive.AvailableSpace/(1024^3) & ” GB”
vii) Creating a Text File
Dim objFso
Set objFso=CreateObject(“scripting.FileSystemObject”)
objFso.CreateTextFile (“C:Documents and Settings1 RIGHATWAYDesktopabcd.txt”)
objFso.CreateTextFile (“C:Documents and Settings1 RIGHATWAYDesktopabcd.doc”)
objFso.CreateTextFile (“C:Documents and Settings1 RIGHATWAYDesktopabcd.xls”)
objFso.CreateTextFile (“C:Documents and Settings1 RIGHATWAYDesktopabcd.pdf”)
Note: We can Create other files also, but they act as Text/Flat Files
viii) Check if the File Exist or not? If not create the File
Dim objFso, myFile1,myFile2, myFile3, myFile4
myFile1=”C:Documents and Settings1 RIGHATWAYDesktopabcd.txt”
myFile2=”C:Documents and Settings1 RIGHATWAYDesktopabcd.doc”
myFile3=”C:Documents and Settings1 RIGHATWAYDesktopabcd.xls”
myFile4=”C:Documents and Settings1 RIGHATWAYDesktopabcd.pdf”
Set objFso=CreateObject(“scripting.FileSystemObject”)
If Not objFso.FileExists(myFile1) Then
objFso.CreateTextFile (myFile1)
End If
If Not objFso.FileExists(myFile2) Then
objFso.CreateTextFile (myFile2)
End If
3
Visit: www.gcreddy.com for QTP Information
If Not objFso.FileExists(myFile3) Then
objFso.CreateTextFile (myFile3)
End If
If Not objFso.FileExists(myFile4) Then
objFso.CreateTextFile (myFile4)
End If
ix) Read Data Character by Character from a text file
Dim objFso, myFile, myChar
Set objFso=CreateObject(“scripting.FileSystemObject”)
Set myFile=objFso.OpenTextFile(“C:Documents and Settings1
RIGHATWAYDesktopabcd.txt”,1) ’1 for Read, 2 for Write and 8 for Append
Do Until myFile.AtEndOfStream=True
myChar=myFile.Read(1)
Msgbox myChar
Loop
myFile.Close
Set objFso=Nothing
x)Read Line by Line from a Text File
Dim objFso, myFile, myChar
Set objFso=CreateObject(“scripting.FileSystemObject”)
Set myFile=objFso.OpenTextFile(“C:Documents and Settings1
RIGHATWAYDesktopabcd.txt”,1) ’1 for Read, 2 for Write and 8 for Append
Do Until myFile.AtEndOfStream=True
myChar=myFile.ReadLine
Msgbox myChar
Loop
4
Visit: www.gcreddy.com for QTP Information
myFile.Close
Set objFso=Nothing
xi) 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: xyz
‘Date of Creation: 24-08-2010
‘Pre-requasites:
‘abcd.txt (Test Data File)
‘Test Flow:
‘Create File System object
‘Open the file with Read mode and store reference into a variable
‘Skipe the first line
‘Read line by line and split the Data
‘Login Operation
‘Form Looping and pass Parameters
‘*******************************************************************
******************
Dim objFso, myFile, myLine, myField
Set objFso=CreateObject(“scripting.FileSystemObject”)
Set myFile=objFso.OpenTextFile(“C:Documents and Settings1
RIGHATWAYDesktopabcd.txt”,1) ’1 for Read, 2 for Write and 8 for Append
myFile.SkipLine
Do Until myFile.AtEndOfStream =True
myLine=myFile.ReadLine
5
Visit: www.gcreddy.com for QTP Information
myField=Split(myLine,”,”)
SystemUtil.Run “C:Program FilesHPQuickTest
Professionalsamplesflightappflight4a.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
xii) Write Data to a Text File
Dim objFso, myFile, Result, a, b
a=10: b=20
Result=a+b
Set objFso=CreateObject(“scripting.FileSystemObject”)
Set myFile=objFso.OpenTextFile(“C:Documents and Settings1
RIGHATWAYDesktopabcd.txt”,2) ’1 for Read, 2 for Write and 8 for Append
myFile.WriteLine “Addition of a, b is: “&Result
myFile.Close
Set objFso=Nothing
xiii) Delete a Text File
Dim objFso
Set objFso=CreateObject(“scripting.FileSystemObject”)
objFso.DeleteFile(“C:Documents and Settings1 RIGHATWAYDesktopabcd.doc”)
6
Visit: www.gcreddy.com for QTP Information
Set objFso=Nothing
xiv) Check if the File Exists or not? If Exists delete the File
———–
Dim objFso
Set objFso=CreateObject(“scripting.FileSystemObject”)
If objFso.FileExists(“C:Documents and Settings1 RIGHATWAYDesktopabcd.pdf”)
Then
objFso.DeleteFile(“C:Documents and Settings1 RIGHATWAYDesktopabcd.pdf”)
End If
Set objFso=Nothing
xv) Calculate size of a Text File
Dim objFso
Set objFso=CreateObject(“scripting.FileSystemObject”)
File_Size= objFso.GetFile(“C:Documents and Settings1
RIGHATWAYDesktopabcd.txt”).Size
Msgbox File_Size& ” Bytes”
Set objFso=Nothing
xvi)Compare Two Text File by Size, by Text and by Binary values
Option Explicit
Dim objFso, File1, File2, myFile1, myFile2, File_First, File_Second, Files_Compare
File1=”C:Documents and Settings1 RIGHATWAYDesktopabcd.txt”
File2=”C:Documents and Settings1 RIGHATWAYDesktopxyz.txt”
Set objFso=CreateObject(“scripting.FileSystemObject”)
‘Comaring two text files by Size
If objFso.GetFile(File1).Size= objFso.GetFile(File2).Size Then
Msgbox “Files are Same in Size”
7
Visit: www.gcreddy.com for QTP Information
Else
Msgbox “Files are Not Same”
End If
‘Comaring two text files by Text
Set File_First=objFso.OpenTextFile(File1)
Set File_Second=objFso.OpenTextFile(File2)
myFile1=File_First.ReadAll
myFile2=File_Second.ReadAll
‘Msgbox myFile1
Files_Compare=strComp(myFile1,myFile2,1) ’1 for Texual Comparision
If Files_Compare=0 Then
Msgbox “Files are having Same Text”
Else
Msgbox “Files are having Different Text”
End If
‘Binary Comparision of Two Text Files
Files_Compare=strComp(myFile1,myFile2,0) ’0 for Binary Comparision (It is Default
mode)
If Files_Compare=0 Then
Msgbox “Files are Equal”
Else
Msgbox “Files are Not Equal”
End If
Set objFso=Nothing
xvii) Count the number of times a word appears in a Text File
8
Visit: www.gcreddy.com for QTP Information
Option Explicit
Dim objFso, File1, myWord, myData, myFile, objRegEx, MatchesFound, TotMatches
File1=”C:Documents and Settings1 RIGHATWAYDesktopabcd.txt”
Set objFso=CreateObject(“scripting.FileSystemObject”)
Set myFile=objFso.OpenTextFile(File1)
myData=myFile.ReadAll
myWord=”QTP”
Set objRegEx= New RegExp ‘Creating Regular Expression Object
objRegEx.Pattern=myWord ‘Search string
objRegEx.Global=True ‘ Finding all Matches
objRegEx.IgnoreCase=True ‘ Ignoring Case
Set MatchesFound=objRegex.Execute(myData) ‘Executing the Total file data to find
natches
TotMatches=MatchesFound.Count
Msgbox “Matches: “&TotMatches
Set objFso=Nothing
xviii) Capture all Button Names from Login dialog Box and Export to a Text
File
Option Explicit
Dim objFso, FilePath, myFile, oButton, myButton, Buttons, i, TotButtons
FilePath=”C:Documents and Settings1 RIGHATWAYDesktopabcd.txt”
Set objFso=CreateObject(“scripting.FileSystemObject”)
Set myFile=objFso.OpenTextFile(FilePath,2)
myFile.WriteLine “Button Names”
myFile.WriteLine “————”
Set oButton=Description.Create
9
Visit: www.gcreddy.com for QTP Information
oButton(“micclass”).value=”WinButton”
SystemUtil.Run “C:Program FilesHPQuickTest
Professionalsamplesflightappflight4a.exe”
Set Buttons=Dialog(“text:=Login”).ChildObjects(oButton)
TotButtons=Buttons.Count
For i= 0 to TotButtons-1 Step 1
myButton=Buttons(i).GetRoProperty(“text”)
myFile.WriteLine myButton
Next
myFile.Close
Set objFso=Nothing
xix) Capture Customer Names from 1 to 10 Orders in FR and export to a
Text File
*****************************************************************
‘Test Requirement: Capture Customer names from 1 to 10 orders
‘and export to text file
‘Test Flow:
‘Create an object in File system class
‘Open the text file in write mode using File sytem object
‘Login Operation
‘Form Loop to open 1 to 10 orders
‘capture the Customer names and write to external text file
‘*****************************************************************
Dim objFso, myFile
Set objFso=CreateObject(“scripting.FilesystemObject”)
10
Visit: www.gcreddy.com for QTP Information
Set myFile=objFso.OpenTextFile(“C:Documents and Settingsgcr.GCRC-
9A12FBD3D9Desktopabc.txt”,2)
myFile.WriteLine “Customer Names”
myFile.WriteLine “———”
If Not Window(“Flight Reservation”).Exist(3) Then
SystemUtil.Run “C:Program FilesHPQuickTest
Professionalsamplesflightappflight4a.exe”,”",”C:Program FilesHPQuickTest
Professionalsamplesflightapp”,”open”
Dialog(“Login”).Activate
Dialog(“Login”).WinEdit(“Agent Name:”).Set “hgghh”
Dialog(“Login”).WinEdit(“Password:”).SetSecure
“4c9e05a626f9b6471971fb15474e791b28cc1ed0″
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
wait 2
Customer_Name = Window(“Flight Reservation”).WinEdit(“Name:”).GetVisibleText()
myFile.WriteLine Customer_Name
Next
myFile.Close
Set objFso=Nothing
11
Visit: www.gcreddy.com for QTP Information
12