Sunday, June 15, 2008

QTP Tutorials 12 - 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:


Drive
|
Folder
|
File
|
Textstream

or some people make it like this:

Drive
|
Folder
|
Folders
|
File
|
Files
|
Textstream

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 on reading guyz!!!

2 comments:

Unknown said...

hey this sort of example is interesting ...will get to knw hw to start with manual scripting...as in most of the institutes like this examples are not given ..

Anonymous said...

nice example