答案 0 :(得分:1)
没有证据表明您已尝试自己解决此问题,并且该问题缺少一些关键信息,因此我不确定这是否是您真正想做的事情。
但是,我只是碰巧要从“很大”的文本文件中删除特定字符,所以也许这段代码会有所帮助。
这会将文本文件快速读取到一个字节数组中,然后转储到第二个字节数组中,跳过特定字符,然后将该数组转换为字符串,然后以新名称保存文件。
Const fileIn = "x:\myPath\myInputFile.txt"
Const fileOut = "x:\myPath\myOututFile.txt"
Sub stripChars()
Dim bytesIn() As Byte, bytesOut() As Byte
Dim x As Long, y As Long, txtOut As String, fileSize As Long
Debug.Print fileIn & " : Reading Data, ";
Open fileIn For Binary Access Read As #1
fileSize = LOF(1) 'read bytes from file
ReDim bytesIn(fileSize - 1&)
Get #1, , bytesIn
Close #1
Debug.Print "Cleaning, ";
ReDim bytesOut(LBound(bytesIn) To UBound(bytesIn))
For x = LBound(bytesIn) To UBound(bytesIn)
Select Case bytesIn(x)
Case 9, 10, 13 To 126 'retain only specific ASCII characters
bytesOut(y) = bytesIn(x)
y = y + 1
Case Else
'do nothing (skip byte)
End Select
If x / 1000000 = x \ 1000000 Then
DoEvents 'update status bar, every 1m char
Application.StatusBar = "Cleaning: " & Format(x / fileSize, "0.0%")
End If
Next x
ReDim Preserve bytesOut(LBound(bytesOut) To y - 1) 'resize
txtOut = StrConv(bytesOut, vbUnicode) 'convert byte array to string
If Dir(fileOut) <> "" Then Kill fileOut 'delete output file if it exists
Debug.Print "Saving, ";
Open fileOut For Output As #2
Print #2, txtOut 'write to output file
Close #2
Application.StatusBar = "Finished! (Removed " & _
fileSize - FileLen(fileOut) & " bytes)"
Debug.Print "Done."
End Sub
或者,有几个工作表函数可用于清理测试。
请参阅“ Top 10 Ways To Clean Your Data。”