Blog to understand automation concepts in QTP, Selenium Webdriver and Manual Testing concepts

How to write to Text Files using fileSystemObject in QTP

Using FileSystemObject we can write data to a text file. Below are the steps to write to a text file.


1. Create an instance of the FileSystemObject

 This is done through
Set objFSO = CreateObject("Scripting.FileSystemObject")

2. Use OpenTextFile to open an existing file or CreateTextFile to create a new text file to write data into.

Set otf = objFSO.CreateTextFile(FilePath,[OverWrite])
where FilePath is path of file to be created.OverWrite – Flag for overwriting content 

OR

 Set otf = objFSO.OpenTextFile(FilePath,ioMode)

where FilePath is path of file to be opened.ioMode – Define mode in which file to be opened. Use 1 to overwrite data and 8 to append data. 


3. Use below methods to write to the file.


a. Write(strText) - Write specified string in the text file.

Otf.Write(strText)

b. Writeline(strText) - writes a specified string and newline character.

otf.writeline(strText)

c. WriteblankLines(intLine) - Writes the specified number of blank lines in the text file

otf.WriteblankLines(4)

4. Close the text file.

otf.close This will close the file. It will close the text file. Note file is saved automatically when we write to a text file.


5. Set all objects as nothing


No comments:

Post a Comment