|
|
|
|
|
You are here...
VBScript - Writing Data into a File
We can write data into a file in two ways i. You can write data continuously into the file (using the 'Write' function)
ii. You can write data into the file line by line (Using the 'WriteLine' function.)
The following piece of code demonstrates how to write data into a file.
Writing a Data into a File (Using the 'Write' function)
Dim ObjFso
Dim StrFileName
Dim ObjFile
StrFileName = "C:\TestFile.txt"
Set ObjFso = CreateObject("Scripting.FileSystemObject")
'Creating a file for writing data
Set ObjFile = ObjFso.CreateTextFile(StrFileName)
'Writing a string into the file
ObjFile.Write("This is a sample string.")
ObjFile.Write("This string will be written continuosly after the first string.")
'Closing the file
ObjFile.Close
While writing data into the file this way, if you want to insert a new line character in between two strings you can
do it by 'ObjFile.Write(VbCrLf)'. This will write a new line character into the file and what ever you write after the
new line character will come in the next line.
Writing Data into a File Line by Line (Using the 'WriteLine' Function)
Dim ObjFso
Dim StrFileName
Dim ObjFile
StrFileName = "C:\TestFile.txt"
Set ObjFso = CreateObject("Scripting.FileSystemObject")
'Creating a file for writing data
Set ObjFile = ObjFso.CreateTextFile(StrFileName)
'Writing a string into the file
ObjFile.WriteLine("This string will be in first line.")
ObjFile.WriteLine("This string will be in second line.")
'Closing the file
ObjFile.Close
Here the speciality of the 'WriteLine' function is that, after writing the string which you give as the argument, it will
automatically write a new line character also at the end of the string. So if you write a string again, it will come in
the next line.
Writing Blank Lines into a File (Using the 'WriteBlankLines' Function)
Dim ObjFso
Dim StrFileName
Dim ObjFile
StrFileName = "C:\TestFile.txt"
Set ObjFso = CreateObject("Scripting.FileSystemObject")
'Creating a file for writing data
Set ObjFile = ObjFso.CreateTextFile(StrFileName)
'Writing a string into the file
ObjFile.WriteLine("This string will be before the blank lines.")
'Writing 10 blank lines
ObjFile.WriteBlankLInes(10)
ObjFile.WriteLine("This string will be after the blank lines.")
'Closing the file
ObjFile.Close
|
|