【问题标题】:VBA Read Large Text File and Delete unwanted text linesVBA 读取大文本文件并删除不需要的文本行
【发布时间】:2018-01-03 16:54:25
【问题描述】:

我正在尝试清理一个大文本文件(重复的标题/拖尾)以导入到访问表中。 我引用了之前的帖子:Read Number of lines in Large Text File VB6

我想要做的是切换:打开 s_inp_file 以输入为 inp_file_num 以打开 s_inp_file 以进行二进制访问读取为 #inp_file_num 进行二进制读取以加快进程。输入法耗时过长。问题出在我的代码中,当以二进制形式打开时我无法使用行输入,并且出现运行时错误 #62 - 输入到达循环中文件的末尾。提前致谢。

代码:

Sub Scrub_TextLines(s_inp_file As String, s_out_file As String)
    Dim inp_file_num As Integer
    Dim out_file_num As Integer
    Dim text_line As String
    Dim file_content As String
    Dim buffer() As Byte
    Dim i As Long

    Const remove_text1 As String = "REPORTING SERVICE"
'    Const remove_text2 As String = "PRODUCT TYPE -"
'    Const remove_text3 As String = "ALL REPORT"
    Const remove_text4 As String = "CLIENT NAME"
    Const remove_text5 As String = "CLIENT ID"
    Const remove_text6 As String = "TY YEAR"
    Const remove_text7 As String = "CLIENT-ID2"
    Const remove_text8 As String = "----"

    inp_file_num = FreeFile
    Open s_inp_file For Binary Access Read As #inp_file_num
    ReDim buffer(LOF(inp_file_num) - 1)
    Get #inp_file_num, , buffer
        Do Until EOF(inp_file_num)
            Line Input #inp_file_num, text_line
            If text_line <> "" And _
                Left(text_line, 4) <> "    " And _
                    InStr(1, text_line, remove_text1, vbTextCompare) = 0 And _
                    InStr(1, text_line, remove_text4, vbTextCompare) = 0 And _
                    InStr(1, text_line, remove_text5, vbTextCompare) = 0 And _
                    InStr(1, text_line, remove_text6, vbTextCompare) = 0 And _
                    InStr(1, text_line, remove_text7, vbTextCompare) = 0 And _
                    InStr(1, text_line, remove_text8, vbTextCompare) = 0 Then

                file_content = file_content & text_line & vbCrLf
'                Debug.Print file_content
            End If
        Loop
    Close #inp_file_num

    out_file_num = FreeFile
    Open s_out_file For Output As out_file_num
        Print #out_file_num, file_content
    Close #out_file_num
End Sub

【问题讨论】:

  • 可能超过 32 千位的 INTEGER 限制?将变量更改为 LONG?
  • 我认为这是因为您的循环的结构。您的 do-until 循环在检查 EOF 之前评估正文,以确保您仍有要阅读的行。尝试将您的 Do ... Until 循环更改为 Do While ... Loop 循环。我相信Do While Not EOF(inp_file_num) 应该可以工作。
  • 感谢您的快速响应 - 我刚刚尝试更改循环并在“Line Input #inp_file_num, text_line”行中收到相同的错误。此外,整数参考文件分配(FreeFile),因此不会增加太多。
  • 如果不是在内存中构建“输出”文件,而是将每一行直接写到不同的文件中,那么您可能会继续使用原始的基于文本的逐行方法。或者至少使用“stringbuilder”方法来创建输出:codereview.stackexchange.com/questions/67596/…
  • 我刚刚阅读了代码,我将在明天尝试实现它,看看它是如何工作的 - 感谢您的建议,看起来很有趣!

标签: vba file ms-access text binary


【解决方案1】:

尝试改变:

Do Until EOF(inp_file_num)

收件人:

Do While Not EOF(inp_file_num)

【讨论】:

  • 不幸的是,同样的错误,从我读到的,当使用二进制方法时,你不能使用行输入,也许它严格用于字符串?我只是不确定修改它以使其工作的最佳方法。
【解决方案2】:

删除以下行:

Get #inp_file_num, , buffer

如果读取所有文件内容,则指针位于文件末尾,因此无法读取新行。

【讨论】:

    猜你喜欢
    • 2011-01-17
    • 2013-03-23
    • 1970-01-01
    • 2018-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多