Many a times you may need to interact with text files using QTP. Interaction can be(but not limited to) in the form of reading input from a file, writing output to a file. This post describe in detail "File handling using QTP".
We use FSO object to do this.
What is FSO?
FSO stands for File System Object. This is used to support text file creation and manipulation through the TextStream object and is contained in the Scripting type library (Scrrun.dll)
The FSO Object Model has a rich set of properties, methods and events to process folders and files.
How to create a file?
We first create a FSO object using CreateObject and then create a text file using CreateTextFile.
For Example: Suppose you want to create a file called "test.txt" located in C:
Dim fso, file, file_locationWe would use the same example for the rest of this post.
file_location = "C:\file_location"
Set fso = CreateObject(“Scripting.FileSystemObject”)
Set file = fso.CreateTextFile(file_location, True) // True--> file is to be overwritten if it already exists else false
How to open a file?
Set file= fso.OpenTextFile("C:\file_location", ForWriting, True)How to read content from a file?
//2nd argument can be ForReading, ForWriting, ForAppending
//3rd argument is "True" if new file has to be created if the specified file doesn’t exist else false, blank signify false.
Use ReadLine() method
For example:
Set file= fso.OpenTextFile("C:\file_location", ForReading, True) //2nd argument should always be "ForReading" in order to read contents from a fileHow to write content to a file?
Do while file.AtEndofStream <> True
data = file.ReadLine()
msgbox data
Loop
You can use Write() or WriteLine() Methods to write text into a file. The difference between the Write() and WriteLine() Method is that the latter automatically inserts a new line character while the former doesn’t insert a new line character.
For example:
Set file= fso.OpenTextFile("C:\file_location", ForWriting, True) //2nd argument should always be "ForWriting" in order to write contents to a fileHow to delete content?
file.Write("This is a place to get all your qtp")
file.Write("questions and answers solved.")
//Output will be:
This is a place to get all your qtp questions and answers solved.
while
file.WriteLine("This is a place to get all your qtp")
file.Write("questions and answers solved.")
//Output will be:
This is a place to get all your qtp
questions and answers solved.
Use DeleteFile() method to delete a file from a particular location
Foe Example:
file_location = "C:\file_location"
Set fso = CreateObject(“Scripting.FileSystemObject”)
fso.DeleteFile(file_location)
If you want to keep track of further articles on QTP. I recommend you to subscribe via RSS Feed. You can also subscribe by Email and have new QTP articles sent directly to your inbox.

18 comments:
Hi Ankur..this is very good information provided and in very organised manner..
Hi Ankur,
this information is really valuable and organised!!!
Hi, Ankur
The info regards FSO is very much understandable. Explnation which you have given for each functions are harmony...
thanks Brother.
---- ganesh
Can i read and write in one shot means reading from one file and writing it in another file by creating the other new file
very helpful solution to handle files using QTP
i have problem reading content from file.
We have a S/W product which generates a log file in txt format and stores by default at 'location 'C:\Documents and Settings\All Users\Application Data\STRAQ.Director\Logs'. The name of the log file is something like xxxxx.xxxxx.xxxx.txt.
I have used FSO and also SystemUtil.Run methods to open the file and read the content of the file. But, could not . i tried after renaming the file with a simple name (xxxx.txt), it did not work.
It doesn't work, when i change the location of the file
It works only when i copy the content of the file to any other txt file.
Please suggest any solution
very helpful solution to handle files using QTP
i have problem read a txt file, which is generated as a log file for our S/W produce. The default location of the file is location 'C:\Documents and Settings\All Users\Application Data\STRAQ.Director\Logs'. The default name of the file is xxxx.xxxx.xxxx.txt
i have tried reading the file after renaming the file ( say xxxx.txt), it did not work
i have tried changing the location of the file, it did not work
It works only when i copy the content of the file to any other txt file
Please suggest any solution
hi ankur,
i have another prob in fso..
1) how to create sub folder under folder
D:\folder\folder1\folder2
D:\folder\file
2) how to access those sub folders.or files
kindly reply fast..
Thanks..
Hi you can create subfolder within folder by using following code:
Set fso = CreateObject("Scripting.FileSystemObject")
fso.createfolder("C:\Folder") 'create a folder
Set fso1 = fso.createfolder("C:\Folder\Folder1") ' Create subfolder within main folder
For more information please fo through http://msdn.microsoft.com/en-us/library/ebkhfaaz(VS.85).aspx
Cheers
hey
this is what I get when I try to run the first step :
Dim fso, file1, file_location
file_location = "C:\test.txt"
Set fso = CreateObject(“Scripting.FileSystemObject”)
Set file1 = fso.CreateTextFile(file_location, True)
in QTP 10:
The test run cannot continue due to a syntax error.
Invalid character
Line (1): "Set fso = CreateObject(“Scripting.FileSystemObject”)".
it would be great if someone could point out whats going wrong
hey
its working now .. i was copy pasting the stuff from the browser n QTP wasnt accepting the double quotes properly (")
thanks
Hello,
Very nice explanation.
Just one question: how to paste data into a text file ?
hi all,
I have a requirement where log file is generated and from that log file i need to get the content and save in another logfile through file object how can it be done?
Hi All,
I want to replace a complete line
after comparing the line with subsection of line
any solution pplease mail it to teja431@gmail.com
Hi Ankur,
1. how can I compare two text file
2. can I open an excel file using filesystemobject
I am getting following error
Invalid procedure call or argument
Line (12): "Set file= fso.OpenTextFile("C:\text.txt", ForReading, True)".
Do I need to add Scrrun.dll lidrary. If yes then please guide me how to do that..?
hello .. i m new to QTP anywy..i saw the article.. nice one ...i can extract it as..
File handling with QTP
Create filesystem object:
Set fso = CreateObject(“Scripting.FileSystemObject”)
//Way to create a file.. override it "True", not "False"
Set file = fso.CreateTextFile(file_location, True)
//Open text file for writing
Set file= fso.OpenTextFile("C:\file_location", ForWriting, True)
//open text file for reading
Set file= fso.OpenTextFile("C:\file_location", ForReading, True)
//Read file data
Do while file.AtEndofStream <> True
data = file.ReadLine()
msgbox data
Loop
//Write data to file
file.Write("questions and answers solved.")
//Delete file
fso.DeleteFile(file_location)
//Create a folder
Set fso1 = fso.createfolder("C:\Folder\Folder1")
Here we are not closing the file ..like we does it in c#....
is it expected ???
Problem :-
I am getting following error
Invalid procedure call or argument
Line (12): "Set file= fso.OpenTextFile("C:\text.txt", ForReading, True)".
Do I need to add Scrrun.dll lidrary. If yes then please guide me how to do that..?
Solution:
just write this code
Set file= fso.OpenTextFile("C:\text.txt")
it will work.
Hi All
i have one question in my mind
QTP uses Compiler or Interpreter ?
if it uses Compiler, then what its name?
Post a Comment