22

I have the following to read a file line by line:

wscript.echo "BEGIN"

filePath = WScript.Arguments(0)
filePath = "C:\Temp\vblist.txt"
Set ObjFso = CreateObject("Scripting.FileSystemObject")
Set ObjFile = ObjFso.OpenTextFile(filePath)
StrData = ObjFile.ReadLine
wscript.echo "END OF FIRST PART"

Do Until StrData = EOF(ObjFile.ReadLine)
    wscript.echo StrData
    StrData = ObjFile.ReadLine
Loop

wscript.echo "END"

The EOF() function doesn't seem to work:

C:\Users\EGr\Documents\Scripts\VB>cscript testloop.vbs ArgVal
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

BEGIN
END OF FIRST PART
C:\Users\EGr\Documents\Scripts\VB\testloop.vbs(11, 1) Microsoft VBScript runti
me error: Type mismatch: 'EOF'

I haven't programmed in VB before, but I'm trying to figure out loops so that I can modify a VB script I've been handed. I want to read a file line by line, and do something with each line. If I change the Do Until loop to Do Until StrData = EOF, it works but throws an error when it gets to the end of the file:

C:\Users\EGr\Documents\Scripts\VB>cscript testloop.vbs ThisRANDOMValue
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

BEGIN
1
END OF FIRST PART
host1
host2
host3
C:\Users\EGr\Documents\Scripts\VB\testloop.vbs(13, 2) Microsoft VBScript runti
me error: Input past end of file

I feel like there is probably an easy solution, but I haven't been able to find it. I've tried a few other solutions I've found online, but haven't got as close as the above.

Mike Brind
  • 28,238
  • 6
  • 56
  • 88
EGr
  • 2,072
  • 10
  • 41
  • 61

2 Answers2

47

When in doubt, read the documentation:

filename = "C:\Temp\vblist.txt"

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(filename)

Do Until f.AtEndOfStream
  WScript.Echo f.ReadLine
Loop

f.Close
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Thanks! Out of curiosity, this is visual basic, correct? When I try to do something like `Dim TestString As String = "Look at these!"` It throws an error for "expected end of statement" – EGr Mar 21 '13 at 15:47
  • 4
    It's VBScript, not VB. The former doesn't support variable declarations of the form `Dim variable As type`. Just use `Dim variable` without a type for declaring variables in VBScript. – Ansgar Wiechers Mar 21 '13 at 16:23
  • Sorry for all the questions, but are both .vb and .vbs files VBScript? I have both filetypes. – EGr Mar 21 '13 at 17:53
  • 3
    They probably are not. However, an extension is basically a label: it tells what should be inside the file, but the actual content may still differ from what the label says. You have to look yourself. – Ansgar Wiechers Mar 21 '13 at 19:52
4

If anyone like me is searching to read only a specific line, example only line 18 here is the code:

filename = "C:\log.log"

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(filename)

For i = 1 to 17
    f.ReadLine
Next

strLine = f.ReadLine
Wscript.Echo strLine

f.Close
emirjonb
  • 203
  • 2
  • 12