如何在 VB Script 中逐行读取文件? [英] How do I read a file line by line in VB Script?

查看:24
本文介绍了如何在 VB Script 中逐行读取文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

wscript.echo "BEGIN"

filePath = WScript.Arguments(0)
filePath = "C:Tempvblist.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:UsersEGrDocumentsScriptsVB>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:UsersEGrDocumentsScriptsVB	estloop.vbs(11, 1) Microsoft VBScript runti
me error: Type mismatch: 'EOF'

我以前没有在 VB 中编程,但我正在尝试找出循环,以便我可以修改我收到的 VB 脚本.我想逐行读取文件,并对每一行做一些事情.如果我将执行直到循环更改为 执行直到 StrData = 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:UsersEGrDocumentsScriptsVB>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:UsersEGrDocumentsScriptsVB	estloop.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.

推荐答案

如有疑问,请阅读 文档:

filename = "C:Tempvblist.txt"

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

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

f.Close

这篇关于如何在 VB Script 中逐行读取文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆