How to write text file using VBScript

We can write text file using VBScript with the help of many functions provided by FileSystemObject.

Functions provided by FileSystemObject such as Write, Writeline, Writeblanklines help us to write data into the text file in many ways. Let’s see example code for how to use these functions.

FileSystemObject also provides methods to read text file, see – and write text file, see – https://automationscript.com/how-to-write-text-file-using-vbscript/

Write- How to write text file using VBScript

Write– This function is used for just plain and simple write functionality. The argument is the text which we want to write. See below how to use the function. This function keeps appending the text parsed in the argument in each subsequent use of the function.

Set fso=createobject("Scripting.FileSystemObject")
'Open the file “qtptest.txt” in writing mode.
Set qfile=fso.OpenTextFile("C:\qtptest.txt",2,True)
'write contents to the file into a single line
qfile.Write  "Welcome to the World of QTP"
qfile.Write "the file name is qtptest.txt"
'Open the file “qtptest.txt” in reading mode.
Set qfile=fso.OpenTextFile("C:\qtptest.txt",1,True)
'Read  the entire contents of  priously written file
Do while qfile.AtEndOfStream <> true
 'Output –> The file will contain the above written content  in  single line.
  Msgbox  qfile.ReadLine
Loop
'Close the files
qfile.Close
'Release the allocated objects
Set qfile=nothing
Set fso=nothing
Writeline – How to write data line by line using VBScript

Writeline – This function works the same as the Write function only difference is that it writes the data in a newline every time it is called in.

Set fso=createobject("Scripting.FileSystemObject")
'Open the file “qtptest.txt” in writing mode.
Set qfile=fso.OpenTextFile("C:\qtptest.txt",2,True)
'write contents to the file into two newlines
qfile.Writeline  "Welcome to the World of QTP"
qfile.Writeline  "the file name is qtptest.txt"
'Open the file “qtptest.txt” in reading mode.
Set qfile=fso.OpenTextFile("C:\qtptest.txt",1,True)
'Read  the entire contents of  priously written file
Do while qfile.AtEndOfStream <> true
 'Output –> The file will contain the above written content  line by line.
  Msgbox  qfile.ReadLine
Loop
'Close the files
qfile.Close
'Release the allocated objects
Set qfile=nothing
Set fso=nothing
WriteBlankLines – How to write blank lines using VBScript

WriteBlankLines– This function is used to print blank lines in the text file. The argument is the number of blank lines required. See below for an example of how to use this function.

Set fso=createobject("Scripting.FileSystemObject")
'Open the file “qtptest.txt” in writing mode.
Set qfile=fso.OpenTextFile("C:\qtptest.txt",2,True)
'write contents to the file into two newlines
qfile.Writeline  "Welcome to the World of QTP"
'will insert 4 new  blank lines
qfile.WriteBlankLines(4)
qfile.Writeline  "the file name is qtptest.txt"
'Open the file “qtptest.txt” in reading mode.
Set qfile=fso.OpenTextFile("C:\qtptest.txt",1,True)
'Read  the entire contents of  priously written file
Do while qfile.AtEndOfStream <> true
 'Output –> The file will be read file line by line.
  Msgbox  qfile.ReadLine
Loop
'Close the files
qfile.Close
'Release the allocated objects
Set qfile=nothing
Set fso=nothing

You may also read

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.