您好我正在尝试在VB6.0中创建一个函数来读取cfd文件中的行,然后从该文件中删除空行?谁能帮帮我呢?
我想说的是。
/The file is already read through a ReadStream
/Once its read I would like to identify the blank rows
/ delete them.take the blank rows
我尝试了一堆不同的方法,这是最后一个:
Do Until Len(msLineRecord) = ReadStream.AtEndOfStream
msLineRecord = Replace(msLineRecord, vbNewLine & vbNewLine, vbNewLine)
msLineRecord = Len(msLineRecord)
Loop
另外
Do Until ReadStream.AtEndOfStream
If LenB(Trim$(strLine)) = 0 Then
If Not bFoundBlankLine Then
Print #2, strLine bFoundBlankLine = True
End If
Else
Print #2, strLine
End If
Loop
Close ff1
Close ff2
Kill "C:\temp\blank lines.txt"
Name "C:\temp\MyTemp.tmp" As "C:\temp\blank lines.txt"
答案 0 :(得分:1)
您似乎正在使用 FileSystemObject 打开并读取您的文件,因此我假设cfd文件是纯文本文件。我不包括错误处理。你应该能够做到这一点。
Dim fso As New FileSystemObject
Dim fsoSourceStream As TextStream
Dim fsoTempStream As TextStream
Dim fsoFile As File
Dim strLine As String
' Create a temporary text file, and return a reference to a TextStream
Set fsoTempStream = fso.CreateTextFile("some_path\temporary.txt", True) ' the True parameter overwrites the file if it already exists
' Open the source file for reading and return a reference to the TextStream
Set fsoFile = fso.GetFile("some_path\my_file.cfd")
Set fsoSourceStream = fsoFile.OpenAsTextStream(ForReading)
' Loop through the lines writing the lines that are not blank to the temp file
Do While Not fsoSourceStream.AtEndOfStream
strLine = fsoSourceStream.ReadLine
If Len(strLine) > 0 Then
fsoTempStream.WriteLine strLine
End If
Loop
fsoSourceStream.Close
fsoTempStream.Close
fso.DeleteFile("some_path\my_file.cfd") ' Delete the source file
' Rename the temporary file to the source file name
Set fsoFile = fso.GetFile("some_path\temporary.txt")
fsoFile.Name = "some_path\my_file.cfd"
' Clean up
Set fso = Nothing
Set fsoFile = Nothing
Set fsoSourceStream = Nothing
Set fsoTempStream = Nothing