[loops] How do I read a file line by line in VB Script?

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.

This question is related to loops vbscript readfile

The answer is


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

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

Examples related to loops

How to increment a letter N times per iteration and store in an array? Angular 2 Cannot find control with unspecified name attribute on formArrays What is the difference between i = i + 1 and i += 1 in a 'for' loop? Prime numbers between 1 to 100 in C Programming Language Python Loop: List Index Out of Range JavaScript: Difference between .forEach() and .map() Why does using from __future__ import print_function breaks Python2-style print? Creating an array from a text file in Bash Iterate through dictionary values? C# Wait until condition is true

Examples related to vbscript

How to run VBScript from command line without Cscript/Wscript How to set recurring schedule for xlsm file using Windows Task Scheduler How to prevent 'query timeout expired'? (SQLNCLI11 error '80040e31') How do I rename a file using VBScript? How to get two or more commands together into a batch file How to run vbs as administrator from vbs? Find specific string in a text file with VBS script Getting current directory in VBScript Run Command Line & Command From VBS Permission denied on CopyFile in VBS

Examples related to readfile

How to read file with async/await properly? ReadFile in Base64 Nodejs How to refactor Node.js code that uses fs.readFileSync() into using fs.readFile()? How do I read a file line by line in VB Script? open read and close a file in 1 line of code Reading file using fscanf() in C Where to put a textfile I want to use in eclipse?