Home > Automation Testing, QTP > File Handling Using QTP

File Handling Using QTP

QTP File Handling

Many times we have 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 or writing output to a file.

We can use FSO (FileSystemObject) 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.

Secondly we 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_actual_location
file_ actual_location = “C:\file_ actual _location”
Set fso = CreateObject(“Scripting.FileSystemObject”)
Set file = fso.CreateTextFile(file_ actual _location, True)

How to open a file?

Set file= fso.OpenTextFile(“C:\file_ actual_location”, ForWriting, True)
//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.

How to read content from a file?
Use ReadLine() method
For example:

Set file= fso.OpenTextFile(“C:\file_ actual_location”, ForReading, True) //2nd argument should always be “ForReading” in order to read contents from a file
Do while file.AtEndofStream <> True
data = file.ReadLine()
msgbox data
Loop

How to write content to a file?
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_ actual_location”, ForWriting, True) //2nd argument should always be “ForWriting” in order to write contents to a file
file.Write(“This is test is”)
file.Write(“file created.”)
//Output will be:
This is test is file created.
while
file.WriteLine(“This is test is “)
file.Write(“file created.”)
//Output will be:
This is test is
file created.

How to delete file?
Use DeleteFile() method to delete a file from a particular location
Foe Example:

file_location = “C:\file_actual_location”
Set fso = CreateObject(“Scripting.FileSystemObject”)
fso.DeleteFile(file_ actual_location)

Advertisements
  1. Albert Gareev
    July 15, 2009 at 6:18 pm

    Hi Thiyagarajan,

    Your post is a good summary on FileSystemObject’s methods for text files, just as QTP help.
    However I’m wondering why do you go with C-ish naming convention and even use C++ comments??
    “//” won’t work as a comment with QTP.
    You should use “REM” or ” ‘ ” statements.

    Thank you,
    Albert Gareev

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: