VBS向文本文件添加不带空格的行

发布于 2024-10-06 04:03:03 字数 1130 浏览 0 评论 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  

trying to figure out how to modify the code below to add to a text file that happens to have an extra CRLF at the end of the file. I get confusing results depending on where I put the CHR(10). Any ideas how to strip the CRLF or remove the blank line? I need to end up with no extra CRLF's !!!

'This script will add lines to the RandomCSV file if it is not in a multiple of 20.
'If the file is already a mulitiple of 20, nothing should happen.

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  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

━╋う一瞬間旳綻放 2024-10-13 04:03:03

这就是我最终删除文件末尾的 CRLF 的方法。似乎工作正常:
'==============================
'去掉文件末尾的空行
Dim strEnd

Const ForReading = 1
'Const ForWriting = 2

Set 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.关闭
结束如果
字符串文件=“”

Here's what I ended up doing to get rid of the CRLF at the end of the file. Seems to work fine:
'============================
'Get rid of blank Line at End of file
Dim strEnd

Const ForReading = 1
'Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set 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 = ""

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文