所以,我试图仅针对包含某些文本的某些行梳理大文本文件。想要将整行写入新的文本文件。这就是我想出来的,但是它完全清空了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
提前致谢!
更新:工作代码
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
答案 0 :(得分:1)
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
' do something with strLine
'if Instr(1, strLine, "tagId") > 0 'vbscript/VBA
if strLine Like "*tagId*" Then 'VBA
objOutFile.WriteLine strLine
end if
Loop
objOutFile.Close
ts.Close
objFS.DeleteFile strFile
objFS.MoveFile strTemp,strFile