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

  10. thanks for sharing wonderful blog and keep updating more useful information like this. and you are doing great article.
    qtp training in chennai

  11. Your style is very unique compared to other people I've read stuff from. Thanks for posting when you've got the opportunity, Guess I'll just bookmark this web site. gmail login

  12. Really it was an awesome article...very interesting to read..You have provided an nice article....Thanks for sharing..
    Android Training in Chennai
    Ios Training in Chennai

  13. Existing without the answers to the difficulties you’ve sorted out through this guide is a critical case, as well as the kind which could have badly affected my entire career if I had not discovered your website.
    dotnet training in bangalore

  14. I wish to show thanks to you just for bailing me out of this particular trouble.As a result of checking through the net and meeting techniques that were not productive, I thought my life was done.

    oracle training in Bangalore

  15. Thanks The information which you provided is very much useful and easy to learn as well... keep rocking and updating... looking further..
    Austere Technologies |Internet Of Things

  16. Really great blog, it's very helpful and has great knowledgeable information.

    Best Cloud Solutions | Austere Technology Solutions

  17. Great article, really very helpful content you made. Thank you, keep sharing.

    Best Software Security Services | Austere Technology Solutions

  18. AWS Training in Bangalore - Live Online & Classroom
    myTectra Amazon Web Services (AWS) certification training helps you to gain real time hands on experience on AWS. myTectra offers AWS training in Bangalore using classroom and AWS Online Training globally. AWS Training at myTectra delivered by the experienced professional who has atleast 4 years of relavent AWS experince and overall 8-15 years of IT experience. myTectra Offers AWS Training since 2013 and retained the positions of Top AWS Training Company in Bangalore and India.

    IOT Training in Bangalore - Live Online & Classroom
    IOT Training course observes iot as the platform for networking of different devices on the internet and their inter related communication. Reading data through the sensors and processing it with applications sitting in the cloud and thereafter passing the processed data to generate different kind of output is the motive of the complete curricula. Students are made to understand the type of input devices and communications among the devices in a wireless media.