0

试图弄清楚如何修改下面的代码以添加到文件末尾恰好有一个额外的 CRLF 的文本文件中。根据我放置 CHR(10) 的位置,我会得到令人困惑的结果。任何想法如何剥离 CRLF 或删除空行?我最终需要没有额外的 CRLF !!!

'如果 RandomCSV 文件不是 20 的倍数,此脚本将添加行。'如果文件已经是 20 的倍数,则不会发生任何事情。

dim filesys, readfile, contents, lines, remainder, LinesToAdd, StaticLine, Appendfile, Count  
dim field1, field2, field3, field4      
set filesys = CreateObject("Scripting.FileSystemObject")  
Set readfile = filesys.OpenTextFile("C:\RandomCSV.txt", 1, false)  
contents = readfile.ReadAll  
Lines = readfile.line  
readfile.close  
    MsgBox "The file contains this many lines " & Lines  
remainder = lines mod 20  
LinesToAdd = (20 - remainder)  
    MsgBox "Adding this many lines " & LinesToAdd  
If LinesToAdd <> 20 then  
   Set Appendfile = filesys.OpenTextFile("C:\RandomCSV.txt", 8, false)  
For Count = 1 to LinesToAdd  
    Appendfile.write Chr(34) & "Field1" & Chr(34) & Chr(44) & Chr(34) & "Field2" & Chr(34) & Chr(44) & Chr(34) & "Field3" & Chr(34) & Chr(44) & Chr(34) & "Field4" & Chr(10)  
Next  
appendfile.close  
End If  
4

1 回答 1

0

这就是我最终为摆脱文件末尾的 CRLF 而做的事情。似乎工作正常:
'=============================
'摆脱文件末尾的空白行 Dim strEnd

常量 ForReading = 1 '常量 ForWriting = 2

设置 objFSO = CreateObject("Scripting.FileSystemObject") 设置 objFile = objFSO.OpenTextFile("C:\RandomCSV.txt", ForReading) strFile = objFile.ReadAll objFile.Close

intLength = Len(strFile) strEnd = Right(strFile, 2)

If strEnd = vbCrLf Then
strFile = Left(strFile, intLength - 2)
Set objFile = objFSO.OpenTextFile("C:randomCSV.txt", ForWriting)
objFile.Write strFile
objFile.Close
End If
strFile = ""

于 2010-12-06T03:27:27.037 回答