File System Operations
File System Operations
File System Operations
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”)
ii) Check if the Folder Exist or not? If not create the Folder
Set objFso=CreateObject(“scripting.FileSystemObject”)
objFso.CreateFolder (myFolder)
End If
1
Visit: www.gcreddy.com for QTP Information
Set objFso=CreateObject(“scripting.FileSystemObject”)
objFso.CopyFolder myFolder,”E:abcd”
Set objFso=CreateObject(“scripting.FileSystemObject”)
objFso.DeleteFolder( myFolder)
2nd
Set objFso=CreateObject(“scripting.FileSystemObject”)
If objFso.FolderExists(myFolder) Then
objFso.DeleteFolder( myFolder)
End If
Set objFso=CreateObject(“scripting.FileSystemObject”)
Set colDrives=objFso.Drives
Msgbox oDrive
Next
2
Visit: www.gcreddy.com for QTP Information
Dim objFso
Set objFso=CreateObject(“scripting.FileSystemObject”)
Set myDrive=objFso.GetDrive(“D:”)
Dim objFso
Set objFso=CreateObject(“scripting.FileSystemObject”)
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
Set objFso=CreateObject(“scripting.FileSystemObject”)
objFso.CreateTextFile (myFile1)
End If
objFso.CreateTextFile (myFile2)
End If
3
Visit: www.gcreddy.com for QTP Information
objFso.CreateTextFile (myFile3)
End If
objFso.CreateTextFile (myFile4)
End If
Set objFso=CreateObject(“scripting.FileSystemObject”)
Do Until myFile.AtEndOfStream=True
myChar=myFile.Read(1)
Msgbox myChar
Loop
myFile.Close
Set objFso=Nothing
Set objFso=CreateObject(“scripting.FileSystemObject”)
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
‘Pre-requasites:
‘Test Flow:
‘Open the file with Read mode and store reference into a variable
‘Login Operation
‘*******************************************************************
******************
Set objFso=CreateObject(“scripting.FileSystemObject”)
myFile.SkipLine
myLine=myFile.ReadLine
5
Visit: www.gcreddy.com for QTP Information
myField=Split(myLine,”,”)
Dialog(“text:=Login”).Activate
Wait 2
Dialog(“text:=Login”).WinButton(“text:=OK”).Click
Window(“text:=Flight Reservation”).Close
Loop
myFile.Close
Set objFso=Nothing
a=10: b=20
Result=a+b
Set objFso=CreateObject(“scripting.FileSystemObject”)
myFile.Close
Set objFso=Nothing
Dim objFso
Set objFso=CreateObject(“scripting.FileSystemObject”)
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”)
End If
Set objFso=Nothing
Dim objFso
Set objFso=CreateObject(“scripting.FileSystemObject”)
Set objFso=Nothing
Option Explicit
Set objFso=CreateObject(“scripting.FileSystemObject”)
7
Visit: www.gcreddy.com for QTP Information
Else
End If
Set File_First=objFso.OpenTextFile(File1)
Set File_Second=objFso.OpenTextFile(File2)
myFile1=File_First.ReadAll
myFile2=File_Second.ReadAll
‘Msgbox myFile1
Else
End If
Else
End If
Set objFso=Nothing
8
Visit: www.gcreddy.com for QTP Information
Option Explicit
Set objFso=CreateObject(“scripting.FileSystemObject”)
Set myFile=objFso.OpenTextFile(File1)
myData=myFile.ReadAll
myWord=”QTP”
TotMatches=MatchesFound.Count
Set objFso=Nothing
xviii) Capture all Button Names from Login dialog Box and Export to a Text
File
Option Explicit
Set objFso=CreateObject(“scripting.FileSystemObject”)
Set myFile=objFso.OpenTextFile(FilePath,2)
myFile.WriteLine “————”
Set oButton=Description.Create
9
Visit: www.gcreddy.com for QTP Information
oButton(“micclass”).value=”WinButton”
Set Buttons=Dialog(“text:=Login”).ChildObjects(oButton)
TotButtons=Buttons.Count
myButton=Buttons(i).GetRoProperty(“text”)
myFile.WriteLine myButton
Next
myFile.Close
Set objFso=Nothing
*****************************************************************
‘Test Flow:
‘Open the text file in write mode using File sytem object
‘Login Operation
‘*****************************************************************
Set objFso=CreateObject(“scripting.FilesystemObject”)
10
Visit: www.gcreddy.com for QTP Information
myFile.WriteLine “———”
Dialog(“Login”).Activate
Dialog(“Login”).WinEdit(“Password:”).SetSecure
“4c9e05a626f9b6471971fb15474e791b28cc1ed0″
Dialog(“Login”).WinButton(“OK”).Click
End If
Window(“Flight Reservation”).Activate
Window(“Flight Reservation”).WinButton(“Button”).Click
wait 2
myFile.WriteLine Customer_Name
Next
myFile.Close
Set objFso=Nothing
11
Visit: www.gcreddy.com for QTP Information
12