Wednesday, February 9, 2011

Script to read text from file

'Read Text File
On Error Resume Next
dim objFSO
dim objFile

Const ForReading=1
strFile="C:\file.txt"

set objFSO=CreateObject("Scripting.FileSystemObject")
set objFile=objFSO.OpenTextFile(strFile,ForReading)

'read in each line of data until you reach the end of the file
do While objFile.AtEndOfStream<>True
      strEntry=objFile.ReadLine
      'you can now do what ever you want with the line
      'as referenced with the strEntry variable such as
      'echoing it back (e.g. wscript.Echo strEntry) or passing it
      'as a variable to a function of subroutine (e.g. MyFunction strEntry)
     wscript.Echo strFile
loop

'close file
objFile.Close

set objFSO=Nothing
set objFile=Nothing

No comments: