QTP : VBScript to create a file
File System Object model is:
Drive
|
Folder
|
File
|
Textstream
or
Drive
|
Folder
|
Folders
|
File
|
Files
|
Textstream
These objects have methods and properties. With these 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.