Wednesday, September 5, 2007

QTP Tutorials 14 - Script to create file

Now lets do some kind of processing with file system e.g. working with text, excel, word etc files from within the QTP.

For this tutorial you need to know VBScript FSO (File System Object).

The main purpose of the FSO is to access the file system of the computer.

File System Object model is:

or some people make it like this:

These above objects have methods and properties. With these above objects you can obtain and work with the information about drives, folders, files etc like creating a new file, opening a file, writing into a file and much more.

Here we will see a very simple example how to create a text file from within QTP.

Dim fso, new_file
Set fso = createobject("scripting.filesystemobject") // an instance of filesystemobject is being created here. After an instance (fso) is created then we can use the methods (like createtextfile, CreateFolder etc) with that objects instance.
Set new_file = fso.createtextfile("c:\testfile.txt", True) //createtextfile is a
method to create a file and after creating a file it returns a TextStream object
that can be used to read from or write to the file.
new_file.writeline("hello world!")
new_file.close

Just write the above text in the expert view of a new blank test and run it. A new text file (testfile.txt) will be created with "hello world!" written in it.

Try to change the extention of testfile.txt to testfile.doc and see what happens.

Much more to come, keep glued.