File system object in QTP
QTP provides various features and built-in functions to handle the folder structure. By using “filesystemobject ” you can easily work on folders and files.this is commonly used object for parameterization
there are several methods provided with fileobjectsystem in this post we will see some commonly used method with the example
Methods:
- Appends a name to an existing path. The BuildPath method inserts an
additional path separator between the existing path and the name, only if
necessary
Function GetBuildPath(path) Dim fso, newpath Set fso = CreateObject("Scripting.FileSystemObject") newpath = fso.BuildPath(path, "Sub Folder") GetBuildPath = newpath End Function
- Close Method
- This method is used to close the open TextStream file. if you open
any file for reading for writing purpose then you should close it once you
complete with the operation.
object.Close( );
Sub CreateAFile Dim fso, MyFile Set fso = CreateObject("Scripting.FileSystemObject") Set MyFile = fso.CreateTextFile("c:testfile.txt", True) MyFile.WriteLine("This is a test.") MyFile.Close End Sub
- Copy Method
- This is one of useful method used. it Copies a specified file or folder from
one location to another.
Dim fso, MyFile Set fso = CreateObject("Scripting.FileSystemObject") Set MyFile = fso.CreateTextFile("c:testfile.txt", True) MyFile.WriteLine("This is a test.") MyFile.Close Set MyFile = fso.GetFile("c:testfile.txt") MyFile.Copy ("c:windowsdesktoptest2.txt")
- CopyFile Method
- Copies one or more files from one location to another.
FileSystemObject.CopyFile "c:mydocumentsletters*.doc", "c:tempfolder"
this code will copy all docs file to desired location.
- CopyFolder Method
- Recursively copies a folder from one location to another.
FileSystemObject.CopyFolder "c:mydocumentsletters*", "c:tempfolder"
this code will copy all folders from letters folder to tempfolder.
- CreateFolder Method
- following code use to Creates a folder in desired location.
Function CreateFolderDemo Dim fso, f Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.CreateFolder("c:New Folder") CreateFolderDemo = f.Path End Function
- CreateTextFile Method
- Creates a specified file name and returns a TextStream object that
can be used to read from or write to the file.
Sub CreateAfile Dim fso, MyFile Set fso = CreateObject("Scripting.FileSystemObject") Set MyFile = fso.CreateTextFile("c:testfile.txt", True) MyFile.WriteLine("This is a test.") MyFile.Close End Sub
- Delete Method
- Deletes a specified file or folder.
Dim fso, MyFile Set fso = CreateObject("Scripting.FileSystemObject") Set MyFile = fso.CreateTextFile("c:testfile.txt", True) MyFile.WriteLine("This is a test.") MyFile.Close Set MyFile = fso.GetFile("c:testfile.txt") MyFile.Delete
- DeleteFile Method
- Deletes a specified file.
Sub DeleteAFile(filespec) Dim fso Set fso = CreateObject("Scripting.FileSystemObject") fso.DeleteFile(filespec) End Sub
- DeleteFolder Method
- Deletes a specified folder and its contents.
Sub DeleteAFolder(filespec) Dim fso Set fso = CreateObject("Scripting.FileSystemObject") fso.DeleteFolder(filespec) End Sub
- DriveExists Method
- Returns true if the specified drive exists; false if it does not.
Function ReportDriveStatus(drv) Dim fso, msg Set fso = CreateObject("Scripting.FileSystemObject") If fso.DriveExists(drv) Then msg = ("Drive " & UCase(drv) & " exists.") Else msg = ("Drive " & UCase(drv) & " doesn't exist.") End If ReportDriveStatus = msg End Function
- Exists Method
- Returns true if a specified key exists in the Dictionary object,
false if it does not.
Function KeyExistsDemo Dim d, msg ' Create some variables. Set d = CreateObject("Scripting.Dictionary") d.Add "a", "Athens" ' Add some keys and items. d.Add "b", "Belgrade" d.Add "c", "Cairo" If d.Exists("c") Then msg = "Specified key exists." Else msg = "Specified key doesn't exist." End If KeyExistsDemo = msg End Function
- FileExists Method
- Returns true if a specified file exists; false if it does not.
Function ReportFileStatus(filespec) Dim fso, msg Set fso = CreateObject("Scripting.FileSystemObject") If (fso.FileExists(filespec)) Then msg = filespec & " exists." Else msg = filespec & " doesn't exist." End If ReportFileStatus = msg End Function
- FolderExists Method
- Returns true if a specified folder exists; false if it does not.
Function ReportFolderStatus(fldr) Dim fso, msg Set fso = CreateObject("Scripting.FileSystemObject") If (fso.FolderExists(fldr)) Then msg = fldr & " exists." Else msg = fldr & " doesn't exist." End If ReportFolderStatus = msg End Function
- GetFile Method
- Returns a File object corresponding to the file in a specified path.
Function ShowFileAccessInfo(filespec) Dim fso, f, s Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.GetFile(filespec) s = f.Path & "<br>" s = s & "Created: " & f.DateCreated & "<br>" s = s & "Last Accessed: " & f.DateLastAccessed & "<br>" s = s & "Last Modified: " & f.DateLastModified ShowFileAccessInfo = s End Function
- GetFileName Method
- Returns the last component of specified path that is not part of the drive
specification.
Function GetAName(DriveSpec) Dim fso Set fso = CreateObject("Scripting.FileSystemObject") GetAName = fso.GetFileName(DriveSpec) End Function
- Returns a Folder object corresponding to the folder in a specified
path.
Sub AddNewFolder(path, folderName) Dim fso, f, fc, nf Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.GetFolder(path) Set fc = f.SubFolders If folderName <> "" Then Set nf = fc.Add(folderName) Else Set nf = fc.Add("New Folder") End If End Sub
- GetSpecialFolder Method
- Returns the special folder object specified.
Dim fso, tempfile Set fso = CreateObject("Scripting.FileSystemObject") Function CreateTempFile Dim tfolder, tname, tfile Const TemporaryFolder = 2 Set tfolder = fso.GetSpecialFolder(TemporaryFolder) tname = fso.GetTempName Set tfile = tfolder.CreateTextFile(tname) Set CreateTempFile = tfile End Function Set tempfile = CreateTempFile tempfile.WriteLine "Hello World" tempfile.Close
- GetTempName Method
- Returns a randomly generated temporary file or folder name that is useful
for performing operations that require a temporary file or folder.
Dim fso, tempfile Set fso = CreateObject("Scripting.FileSystemObject") Function CreateTempFile Dim tfolder, tname, tfile Const TemporaryFolder = 2 Set tfolder = fso.GetSpecialFolder(TemporaryFolder) tname = fso.GetTempName Set tfile = tfolder.CreateTextFile(tname) Set CreateTempFile = tfile End Function Set tempfile = CreateTempFile tempfile.WriteLine "Hello World" tempfile.Close
- OpenAsTextStream Method
- Opens a specified file and returns a TextStream object that can be
used to read from, write to, or append to the file.
The iomode argument can have any of the following settings:
Constant Value Description ForReading 1 Open a file for reading only. You can’t write to this file. ForWriting 2 Open a file for writing. If a file with the same name exists, its previous contents are overwritten. ForAppending 8 Open a file and write to the end of the file.
Constant Value Description TristateUseDefault -2 Opens the file using the system default. TristateTrue -1 Opens the file as Unicode. TristateFalse 0 Opens the file as ASCII.
Function TextStreamTest Const ForReading = 1, ForWriting = 2, ForAppending = 8 Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0 Dim fso, f, ts Set fso = CreateObject("Scripting.FileSystemObject") fso.CreateTextFile "test1.txt" ' Create a file. Set f = fso.GetFile("test1.txt") Set ts = f.OpenAsTextStream(ForWriting, TristateUseDefault) ts.Write "Hello World" ts.Close Set ts = f.OpenAsTextStream(ForReading, TristateUseDefault) TextStreamTest = ts.ReadLine ts.Close End Function
- OpenTextFile Method
- Opens a specified file and returns a TextStream object that can be
used to read from, write to, or append to the file.
Sub OpenTextFileTest Const ForReading = 1, ForWriting = 2, ForAppending = 8 Dim fso, f Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.OpenTextFile("c:testfile.txt", ForWriting, True) f.Write "Hello world!" f.Close End Sub
- Read Method
- Reads a specified number of characters from a TextStream file and
returns the resulting string.
Function ReadTextFileTest Const ForReading = 1, ForWriting = 2, ForAppending = 8 Dim fso, f, Msg Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.OpenTextFile("c:testfile.txt", ForWriting, True) f.Write "Hello world!" Set f = fso.OpenTextFile("c:testfile.txt", ForReading) ReadTextFileTest = f.Read(5) End Function
- ReadAll Method
- Reads an entire TextStream file and returns the resulting string.
Function ReadAllTextFile Const ForReading = 1, ForWriting = 2 Dim fso, f Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.OpenTextFile("c:testfile.txt", ForWriting, True) f.Write "Hello world!" Set f = fso.OpenTextFile("c:testfile.txt", ForReading) ReadAllTextFile = f.ReadAll End Function
- ReadLine Method
- Reads an entire line (up to, but not including, the newline character) from
a TextStream file and returns the resulting string.
Function ReadLineTextFile Const ForReading = 1, ForWriting = 2 Dim fso, MyFile Set fso = CreateObject("Scripting.FileSystemObject") Set MyFile = fso.OpenTextFile("c:testfile.txt", ForWriting, True) MyFile.WriteLine "Hello world!" MyFile.WriteLine "The quick brown fox" MyFile.Close Set MyFile = fso.OpenTextFile("c:testfile.txt", ForReading) ReadLineTextFile = MyFile.ReadLine ' Returns "Hello world!" End Function
- Write Method
- Writes a specified string to a TextStream file.
Function WriteToFile Const ForReading = 1, ForWriting = 2 Dim fso, f Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.OpenTextFile("c:testfile.txt", ForWriting, True) f.Write "Hello world!" Set f = fso.OpenTextFile("c:testfile.txt", ForReading) WriteToFile = f.ReadLine End Function
- WriteBlankLines Method
- Writes a specified number of newline characters to a TextStream
file.
Function WriteBlankLinesToFile Const ForReading = 1, ForWriting = 2 Dim fso, f Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.OpenTextFile("c:testfile.txt", ForWriting, True) f.WriteBlankLines 2 f.WriteLine "Hello World!" Set f = fso.OpenTextFile("c:testfile.txt", ForReading) WriteBlankLinesToFile = f.ReadAll End Function
- WriteLine Method
- Writes a specified string and newline character to a TextStream
file.
Function WriteLineToFile Const ForReading = 1, ForWriting = 2 Dim fso, f Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.OpenTextFile("c:testfile.txt", ForWriting, True) f.WriteLine "Hello world!" f.WriteLine "VBScript is fun!" Set f = fso.OpenTextFile("c:testfile.txt", ForReading) WriteLineToFile = f.ReadAll End Function
7/9/15
Webex notes:
-
Handling
files with QTP:
VB script concept – handle any files use
file System object.
1. Flat file(if a batch is producing, file
with delimeters in its content). Delimeter text file.
2. Fixed
Layout: Each record in a file follows a layout.
Nicely explained: As per the trainer)
To
access any methods of DLL, we need to create an object. So use set <object
name> for the FileSysteObject method.
Except
excel and XML we cannot use FileSystemObject method.
-> Readall does not work if the file
is very big, so go with ReadLine and
use loop and AtEndofStream to rech
end of line.
- 1- Read, 2 – Write, 8 – Append(append some data end of the file).
- For batch file and flat file, this is very usefull
- For Reading a file, we need the file to be opened, or use OpenTextfile before reading it.
- Exercise: Compare the contents of two files. And print the difference in the data table
- Exercise: Compare the two fixed length source target file
File
template(with no delimeter):
First name 10
Last name 10
Empid 6
Design 10
When you compare
the files, how will you check the difference in first, last, emp id etc….instead
of comparing everything.???
Ans:
Use the below for the above scenario:
Method: Read
Description: Reads a specified number of characters from a TextStream file and
returns the resulting string.
Syntax: strChars = objTso.Read(numCharacters)
#Returnsdifferenceintwostrings:
ReplyDeleteWhen you compare two strings, how will you get the difference b/w strings? I dont see any method for that or do we need to go through each and everyline of character in a file(even I dont know how to do it)
#Delete:
ReplyDeleteUnable to delete file using fobj.DeleteFile(<>) ---- it throws "Permission Denied" Error.