VBScript Text Files: Read, Write, Append

email me

To Write
Set objFileToWrite = CreateObject(“Scripting.FileSystemObject”).OpenTextFile(“C:\file.txt”,2,true)
objFileToWrite.WriteLine(data)
objFileToWrite.Close
Set objFileToWrite = Nothing

To Read the entire file
Set objFileToRead = CreateObject(“Scripting.FileSystemObject”).OpenTextFile(“C:\file.txt”,1)
strFileText = objFileToRead.ReadAll()
objFileToRead.Close
Set objFileToRead = Nothing

To Read line by line
Set objFileToRead = CreateObject(“Scripting.FileSystemObject”).OpenTextFile(“C:\file.txt”,1)
Dim strLine
do while not objFileToRead.AtEndOfStream
strLine = objFileToRead.ReadLine()
‘Do something with the line
loop
objFileToRead.Close
Set objFileToRead = Nothing

 

Notes

OpenTextFile parameters:
IOMode
1=Read
2=write
8=Append

Create (true,false)
Format (-2=System Default,-1=Unicode,0=ASCII)