2014-02-26 73 views
0

嗨我想创建一个函数在VB6.0从cfd文件中读取行,然后从该文件中删除空行?任何人都可以帮助我吗?阅读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

打开一个临时文件,并且当您从源文件读取行时,如果它们不是空白,请将它们写入临时文件。完成后删除原始文件并将临时文件重命名为原始文件名。 – jac

+0

谢谢,你还可以告诉我代码。 – user3347312

+0

不,你还没有提供任何代码供我使用。 – jac

回答

0

看来你正在使用的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 
+0

谢谢。这很棒 – user3347312