Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

read each line from a text file 2

Status
Not open for further replies.

smithy1981

IS-IT--Management
Sep 16, 2005
12
GB
Hi, I want to use a do...while loop, that will read from a text file and use each line as the variable.

IE. the text file has two lines:

Test
Test2

I want a variable to be set as the next line each time the loop passes and then stops when it reaches an empty line

Not sure if i have made myself clear but i hope so
 
Const ForReading = 1
Set objShell = WScript.CreateObject ("WScript.Shell")
Set objFSO = CreateObject ("Scripting.FileSystemObject")
Set fileObject = objFSO.OpenTextFile("File.txt", ForReading)

Do While Not fileObject.AtEndOfStream
strLine = fileObject.ReadLine
WScript.Echo strLine
Loop

Hope this helps
aco
 
if you want it to stop when it gets to an empty line (why?) then you will need

Do While Not fileObject.AtEndOfStream
strLine = ""
strLine = fileObject.ReadLine
'If strLine = "" Then
If Trim(strLine) = "" then
Exit Do
End If
Loop
fileObject.Close
Set fileObject = Nothing
 
If you know the file does not contain low values, then you can use the readall method and put your entire file into an array. It's much faster than reading each line individually...

Code:
Set objReadFile = objFileSys.OpenTextFile("filepath", ForReading)
aryRec = Split(objReadFile.ReadAll, vbCrlf)

For i = 0 to Ubound(aryRec)
    Msgbox aryRec(i)
Next

He who has knowledge spares his words, and a man of understanding is of a calm spirit. Even a fool is counted wise when he holds his peace; when he shuts his lips, he is considered perceptive. - King Solomon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top