0

以下のコードを変更して、たまたまファイルの末尾に余分な CRLF があるテキスト ファイルに追加する方法を見つけようとしています。CHR(10) をどこに置くかによって、混乱する結果が得られます。CRLF を削除する方法や空白行を削除する方法はありますか? 余分な CRLF をなくす必要があります!!!

'このスクリプトは、20 の倍数でない場合、RandomCSV ファイルに行を追加します。'ファイルが既に 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

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 = ""

于 2010-12-06T03:27:27.037 に答える