Loops 如何在VB脚本中逐行读取文件?
我有以下内容逐行读取文件:Loops 如何在VB脚本中逐行读取文件?,loops,vbscript,readfile,Loops,Vbscript,Readfile,我有以下内容逐行读取文件: 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 FI
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
我觉得可能有一个简单的解决办法,但我一直没能找到。我在网上找到了一些其他的解决方案,但没有上面提到的那么好。如果有疑问,请阅读:
如果像我这样的人正在搜索仅读取特定行,示例第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
谢谢出于好奇,这是visual basic,对吗?当我尝试执行类似于
Dim TestString As String=“看看这些!”
的操作时,它会为“预期语句结束”抛出一个错误,这是VBScript,而不是VB。前者不支持形式为Dim variable As type
的变量声明。只需在VBScript中使用Dim variable
而不使用声明变量的类型。对于所有问题,很抱歉,但是.vb和.vbs文件都是VBScript吗?我有两种文件类型。它们可能不是。然而,扩展名基本上是一个标签:它告诉文件中应该包含什么,但是实际内容可能仍然与标签上所说的不同。你得看看你自己。
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