20

我有以下内容逐行读取文件:

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"

EOF()功能似乎不起作用:

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'

我以前没有在 VB 中编程,但我正在尝试找出循环,以便我可以修改我收到的 VB 脚本。我想逐行读取文件,并对每一行做一些事情。如果我将 Do Until 循环更改为Do Until StrData = EOF,它可以工作,但在到达文件末尾时会引发错误:

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

我觉得可能有一个简单的解决方案,但我一直无法找到它。我尝试了一些我在网上找到的其他解决方案,但没有像上面那样接近。

4

2 回答 2

42

如有疑问,请阅读文档

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
于 2013-03-20T19:49:58.703 回答
3

如果像我这样的人正在搜索仅读取特定行,则仅示例第 18 行是代码:

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
于 2014-11-24T10:05:59.693 回答