Hello All! In this post, we going to see about File handling in QTP. The term file handling is seem to be confusing rite? Don't get confused, it's just a text file or in other words flat files.
In order to handle text files using QTP, we need to create below object. Without creating object of "Scripting.FileSystemObject" we cannot read/write text files.
Set FSO = CreateObject("Scripting.FileSystemObject")
Creating Text File:
Syntax : FSO.CreateTextFile (FileName, [In, Optional] Overwrite, [In, Optional] Unicode)
FSO.CreateTextFile ("C:\abc.txt",False,False)
The file "abc.txt" will be created in "C:\" drive when executing the above line. But make sure the file does not exists in the directory already. Make the Overwrite parameter as True, if you want to create a file by overwritting the already exiting file.
Open Text File:
Syntax : FSO.OpenTextFile (FileName,[in, optional] IOMode, [in,optional] Create, [in,optional] Format)
FileName Required. The name of the file to open.
IOMode Optional. How to open the file
1=ForReading - Open a file for reading. You cannot write to this file.
2=ForWriting - Open a file for writing.
8=ForAppending - Open a file and write to the end of the file.
Create Optional. Sets whether a new file can be created if the filename does not exist. True indicates that a new file can be created, and False indicates that a new file will not be created. False is default
Format Optional. The format of the file
0=TristateFalse - Open the file as ASCII. This is default.
-1=TristateTrue - Open the file as Unicode.
-2=TristateUseDefault - Open the file using the system default.
Set file1 = FSO.OpenTextFile("C:\abc.txt", 1, True)
The file 'abc.txt' will be opened in 'Read' mode if it exist. If the file does not exist then a new file will be created and the created file will be opened in the read mode.
Check if File Exists:
Syntax:
FSO.FileExists(FileSpec)
Msgbox FSO.FileExists("C:\abc.txt")
This will return True if the file exists and False if does not.
In order to handle text files using QTP, we need to create below object. Without creating object of "Scripting.FileSystemObject" we cannot read/write text files.
Set FSO = CreateObject("Scripting.FileSystemObject")
Creating Text File:
Syntax : FSO.CreateTextFile (FileName, [In, Optional] Overwrite, [In, Optional] Unicode)
FSO.CreateTextFile ("C:\abc.txt",False,False)
The file "abc.txt" will be created in "C:\" drive when executing the above line. But make sure the file does not exists in the directory already. Make the Overwrite parameter as True, if you want to create a file by overwritting the already exiting file.
Open Text File:
Syntax : FSO.OpenTextFile (FileName,[in, optional] IOMode, [in,optional] Create, [in,optional] Format)
FileName Required. The name of the file to open.
IOMode Optional. How to open the file
1=ForReading - Open a file for reading. You cannot write to this file.
2=ForWriting - Open a file for writing.
8=ForAppending - Open a file and write to the end of the file.
Create Optional. Sets whether a new file can be created if the filename does not exist. True indicates that a new file can be created, and False indicates that a new file will not be created. False is default
Format Optional. The format of the file
0=TristateFalse - Open the file as ASCII. This is default.
-1=TristateTrue - Open the file as Unicode.
-2=TristateUseDefault - Open the file using the system default.
Set file1 = FSO.OpenTextFile("C:\abc.txt", 1, True)
The file 'abc.txt' will be opened in 'Read' mode if it exist. If the file does not exist then a new file will be created and the created file will be opened in the read mode.
Check if File Exists:
Syntax:
FSO.FileExists(FileSpec)
Msgbox FSO.FileExists("C:\abc.txt")
This will return True if the file exists and False if does not.
No comments:
Post a Comment