VBS在没有空格的情况下向文本文件添加行

时间:2010-12-05 22:09:35

标签: vbscript add lines

试图找出如何修改下面的代码以添加到恰好在文件末尾有额外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  

1 个答案:

答案 0 :(得分:0)

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

Const ForReading = 1 'Const 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)

如果strEnd = vbCrLf那么
strFile = Left(strFile,intLength - 2)
设置objFile = objFSO.OpenTextFile(“C:randomCSV.txt”,ForWriting)
objFile.Write strFile
objFile.Close
结束如果
strFile =“”