我有以下内容逐行读取文件:
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
我觉得可能有一个简单的解决方案,但我找不到它。我已经尝试了一些我在网上找到的其他解决方案,但还没有达到与上述相近的程度。
答案 0 :(得分:30)
如有疑问,请阅读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
答案 1 :(得分:2)
如果像我这样的人正在搜索只读一个特定的行,那么这里的第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