Monday, March 16

File Handling in QTP - Part 1

Hello Friends!

In this post, we'll see how to handle files using vbscript in QTP.
We'll cover -

  • How to create new file and write data
  • How to open existing file and read data
  • How to open existing file and append data
  • How to delete an existing file

Okie... so lets talk about File System Object (FSO) first. Actually all these things will be done using FSO only!

What is File System Object (FSO)?

File System Object is used to -

1.  creation, manipulation and deletion of text files
2. create, move and Delete folders on hard disk

FSO is the short name for  File System Object.  The FSO Object Model has a rich set of properties, methods and events to process folders and files.

How to create a file?

yeah.. here we go!

Suppose you want to create a file called "myFile.txt" on C:\ drive

Dim objFSO, file_path
file_path = "C:\myFile.txt"

Function CreateFile(file_path)
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set CreateFile = objFSO.CreateTextFile(file_path, True)
End Function

Call CreateFile("C:\myFile.txt")

And it's done! Hunt your C:\ drive.... myFile.txt will be there!


Now, look at this part..

objFSO.CreateTextFile(file_path, True)
objFSO - is an object of type FileSystemObject
CreateTextFile - is a method of FSO
file_path - is the path/location of you file
True - true means - if the file is already there, it will be overwritten.

You can write False also. In that case if file is already there, it'll not be overwritten, and it'll give you following error.

To test this, follow these steps.... n picture'll be clear to you!
  • run the above code
  • open the file and write your name, save and close
  • run the above code again (same)
  • open the file and see- your name will not be there because it's overwritten
  • now change this line in above code and run again.
  • Set CreateFile = objFSO.CreateTextFile(file_path, False
  • this time you will get the error because file is already there and because of False, it will not be overwritten.


No worries!! In next part of this post, we will reveal all the methods of FSO.

In case of any queries, please post your comments.


  1. Abhikansh ...Thanks a Ton for sharing your knowledge...with us...i have learned a lot..


  2. hi this is very useful...

  3. Function CreateFile(file_path)
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set CreateFile = objFSO.CreateTextFile(file_path, True)
    End Function

    Can you please let me know why did you use Set before "CreateFile = objFSO.CreateTextFile(file_path, True)"

  4. what is the meaning testcomplete in automation testing? how can i give the answer to interviewer

  5. TestComplete is a Regression/Functional Testing tool like QTP.

  6. I tried this code to create a file ,file is not created and Testscript doesnt fail it shows done. wht should i do ?

    1. do you have trial download to try?

  7. @Aanvi s - make sure you have rights to create a file on given path. try to create a file manually first.

  8. thanks for sharing wonderful blog and keep updating more useful information like this. and you are doing great article.
    Software Testing Training in Chennai


  9. All are saying the same thing repeatedly, but in your blog I had a chance to get some useful and unique information, I love your writing style very much, I would like to suggest your blog in my dude circle, so keep on updates.

    SAP training in Chennai