读取 .txt 文件时跳过一行
Skipping one line while reading a .txt file
我正在尝试通读一个 .txt 文件,上面写着:
"If you are"
"Reading this"
"It worked!"
我试图只显示 "If you are" 和 "It worked!",跳过中间行 "Reading this"。
我的代码退出循环,只显示 "If you are"。我该如何更改?
'Declare variables
Dim objFSO, objReadFile, contents
'Set Objects
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objReadFile = objFSO.OpenTextFile("M:\vbscripts\folder\read.txt")
'Read file contents
Do While objReadFile.AtEndOfStream <> True
contents = objReadFile.ReadLine
If contents = "Reading this" then
Exit Do
End If
WScript.Echo contents
Loop
'Quit script
WScript.Quit()
遇到要跳过的行时不要退出循环。相反,只有当它们不包含有问题的文本时才输出行:
Do Until objReadFile.AtEndOfStream
contents = objReadFile.ReadLine
If Not InStr(contents, "Reading this") > 0 then
WScript.Echo contents
End If
Loop
我正在尝试通读一个 .txt 文件,上面写着:
"If you are" "Reading this" "It worked!"
我试图只显示 "If you are" 和 "It worked!",跳过中间行 "Reading this"。 我的代码退出循环,只显示 "If you are"。我该如何更改?
'Declare variables
Dim objFSO, objReadFile, contents
'Set Objects
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objReadFile = objFSO.OpenTextFile("M:\vbscripts\folder\read.txt")
'Read file contents
Do While objReadFile.AtEndOfStream <> True
contents = objReadFile.ReadLine
If contents = "Reading this" then
Exit Do
End If
WScript.Echo contents
Loop
'Quit script
WScript.Quit()
遇到要跳过的行时不要退出循环。相反,只有当它们不包含有问题的文本时才输出行:
Do Until objReadFile.AtEndOfStream
contents = objReadFile.ReadLine
If Not InStr(contents, "Reading this") > 0 then
WScript.Echo contents
End If
Loop