如果我运行它一次运行,第二次运行第二个txt文件是空的,所以脚本停止在这一行上运行:
strTxt = strTxt & txt.ReadAll
我如何解决它,如果文件是空的,它不会停止?
Sub MergeTxtFiles()
Dim fso As Object, txt As Object, strTxt As String
Dim strParentFldr As String, strFile As String
Dim iFreeFile As Integer, strOutPutFile As String
KillProperly "D:\users\gf05856\Documents\TEst\RVA002ALL.txt"
Call DeleteLine1
Call DeleteLine2
strOutPutFile = "D:\users\gf05856\Documents\TEst\RVA002ALL.txt"
strParentFldr = "D:\users\gf05856\Documents\TEst"
Set fso = CreateObject("Scripting.FileSystemObject")
strFile = Dir(strParentFldr & "\Register van aankomst 002*.txt")
If Len(strFile) > 0 Then
Do
Set txt = fso.OpenTextFile(strParentFldr & "\" & strFile)
strTxt = strTxt & txt.ReadAll
strFile = Dir
Loop Until Len(strFile) = 0
If Left(strTxt, 2) = vbCrLf Then strTxt = Mid(strTxt, 2)
End If
iFreeFile = FreeFile
Open strOutPutFile For Output As #iFreeFile
Print #iFreeFile, strTxt
Close #iFreeFile
Set txt = Nothing
Set fso = Nothing
End Sub
Public Sub KillProperly(Killfile As String)
If Len(Dir$(Killfile)) > 0 Then
SetAttr Killfile, vbNormal
Kill Killfile
End If
End Sub
Sub DeleteLine1()
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("D:\users\gf05856\Documents\TEst\Register van aankomst 002 - D (Histo).txt", ForReading)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
If InStr(strLine, "CLIFOUPAYCODE") = 0 Then
strNewContents = strNewContents & strLine & vbCrLf
End If
Loop
objFile.Close
Set objFile = objFSO.OpenTextFile("D:\users\gf05856\Documents\TEst\Register van aankomst 002 - D (Histo).txt", ForWriting)
objFile.Write strNewContents
objFile.Close
End Sub
Sub DeleteLine2()
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("D:\users\gf05856\Documents\TEst\Register van aankomst 002 - D.txt", ForReading)
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
If InStr(strLine, "CLIFOUPAYCODE") = 0 Then
strNewContents = strNewContents & strLine & vbCrLf
End If
Loop
objFile.Close
Set objFile = objFSO.OpenTextFile("D:\users\gf05856\Documents\TEst\Register van aankomst 002 - D.txt", ForWriting)
objFile.Write strNewContents
objFile.Close
End Sub
答案 0 :(得分:0)
.ReadAll()在空/零长度/大小文件上失败:
>> WScript.Echo oFS.GetFile("x").Size
>> Set f = oFS.OpenTextFile("x")
>> s = f.ReadAll()
>>
0
Error Number: 62
Error Description: Eingabe hinter Dateiende.
>>
因此,您应该检查.Size以确定是否应该打开该文件。
(顺便说一下:你试图解决的现实问题是什么?)