所以,我试图梳理通过一个大的文本文件只包含某些文本的某些行。想要将整行写入新的文本文件。这是我想出来的,但它完全清空了taget文本文件,并给了我错误“输入文件末尾”。阅读文本文件,并只写行与某些文字
Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "C:\Users\Choti\Desktop\collect\collect.L"
strTemp = "C:\Users\Choti\Desktop\collect\temp.txt"
Set objFile = objFS.GetFile(strFile)
Set objOutFile = objFS.CreateTextFile(strTemp,True)
Set ts = objFile.OpenAsTextStream(1,-2)
Do Until ts.AtEndOfStream = false
strLine = ts.ReadLine
' do something with strLine
if strLine like "tagId" Then
objOutFile.Write(strLine)
end if
ts.AtEndOfStream = false
Loop
objOutFile.Close
ts.Close
objFS.DeleteFile(strFile)
objFS.MoveFile strTemp,strFile
在此先感谢!
UPDATE:工作守则
Set objFS = CreateObject("Scripting.FileSystemObject")
strFile = "C:\Users\Choti\Desktop\collect\collect.L"
strTemp = "C:\Users\Choti\Desktop\collect\temp.txt"
Set objFile = objFS.GetFile(strFile)
Set objOutFile = objFS.CreateTextFile(strTemp,True)
Set ts = objFile.OpenAsTextStream(1,-2)
Do While Not ts.AtEndOfStream
strLine = ts.ReadLine
if Instr(1, strLine, "tagId") > 0 Then
objOutFile.WriteLine strLine
end if
Loop
objOutFile.Close
ts.Close
你不检查,看是否有行*包括*“标签识别”,但行,而不是是否等于“标签识别”。使用'Instr()'或'Like'来检查一个字符串是否包含在另一个字符串中。你在计数/计数器上做什么? –
我刚刚意识到并更新了它,但现在即时获取输入文件结束。它也删除目标文本文件中的所有内容 – duckfeet23
这是VBA还是VBScript?一个有'Like'操作符,另一个没有。 –