excel 如何快速统计多个文本文件的行数?

k4emjkb1  于 2023-04-22  发布在  其他
关注(0)|答案(4)|浏览(346)

我有100多个文本文件,我必须计算每个文件的行数。Column A列出了文件名,位于E1中指定的文件夹中。有几个文件超过100万行,导致脚本运行时间长得可怕。

Sub counter()
    Dim fso As New FileSystemObject
    Dim ts As TextStream
    Dim longtext As String
    Dim lines As Variant
    Dim GoToNum As Integer
    Dim Start As Integer
    GoToNum = 2
    Start = 3

    Do Until IsEmpty(Cells(Start, 1))
        GoToNum = GoToNum + 1
        Start = Start + 1
    Loop

    For i = 3 To GoToNum
        If Cells(i, 2).Value <= Cells(2, 5).Value Then
            ConOrg = Cells(1, 4).Value & "\" & Cells(i, 1).Value

            Set ts = fso.OpenTextFile(ConOrg, ForReading, False)
            longtext = ts.ReadAll

            ts.Close
            lines = Split(longtext, vbLf)
            Cells(i, 3) = UBound(lines) - LBound(lines) - 1

        End If
    Next i
End Sub

如何获取最后一行的编号(从文本文件),避免逐行计数?

7eumitmz

7eumitmz1#

* 如何使用VBA计算文本文件的行数:*

最快的方法取决于您正在处理的文件的大小:
| | 行数|文件大小|Open语句|FileSystemObject|
| --------------|--------------|--------------|--------------|--------------|
|一个GIANT文件|一百七十万|40mb × 1|25.2秒|✔️2.1秒|
|多个s文件|六|14b × 10,000|✔️1.3秒|18.9秒|
| | | |更适合s文件|更适合 * 大 * 文件|

→更适合小文件:

Function countLF(fName As String) As Long
  Dim st As String
  Open fName For Input As #1: st = Input(LOF(1), 1): Close #1
  countLF = Len(st) - Len(Replace(st, vbLf, "")) + 1
End Function

用法示例:

Debug.Print countLF("c:\test.txt")

→更适合大型文件:

Function countLines(fName As String) As Long
  countLines=CreateObject("Scripting.FileSystemObject").OpenTextFile(fName, 8, True).Line
End Function

用法示例:

Debug.Print countLines("c:\test.txt")

其他测试变体的更多基准测试(2500个小文本文件)
二进制访问/获取(4.32s)Kill=1.17s . . . Open F `对于二进制访问Read As #1:ReDim... Get #1,,bytes
Line Input/LineInput(4.44s)Kill=1.11s ... Open F For Input As #iFile...Line Input #1,st
早期绑定/ReuseObj(5.25s)Del=1.12s . . . Set o=New Scripting. FileSystemObject ':st=o.OpenTextFile(F).ReadAll()
Early Bind/FreshObj(11.98s)Del=1.35s ... Set o=New Scripting. FileSystemObject':st=o.OpenTextFile(F).ReadAll()
LateBind/ReuseObj(6.25s)Del=1.47s ... Set o=CreateObject(“Scripting.FileSystemObject”)
LateBind/FreshObj(13.59s)Del=2.29s ... With CreateObject(“Scripting.FileSystemObject”)

mlnl4t2r

mlnl4t2r2#

给予这个函数。它使用FileSystemObject。应该比读取整个文件并将其拆分为单行快。灵感来自Hey, Scripting guy

Function countLines(fName As String) As Long

    Const ForReading = 1
    Dim objFSO  As Object, objTextFile As Object
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objTextFile = objFSO.OpenTextFile(fName, ForReading)
    objTextFile.ReadAll
    countLines = objTextFile.Line
End Function
x9ybnkn6

x9ybnkn63#

另一种方法是使用Power Query(获取和转换数据):

let
    Source = Folder.Files("C:\Users\me\MyFolder"),
    #"Filtered Rows" = Table.SelectRows(Source, each [Extension] = ".txt"),
    #"Added Row Count" = Table.AddColumn(#"Filtered Rows", "Rows In File", each Table.RowCount(Table.FromColumns({Lines.FromBinary([Content])})), Int64.Type),
    #"Removed Columns" = Table.SelectColumns(#"Added Row Count",{"Name", "Rows In File"})
in
    #"Removed Columns"

这个动作很快

slhcrj9b

slhcrj9b4#

我不认为你可以只用一种方法来读最后一行。

Do While fso.AtEndOfStream <> True
    fso.SkipLine
Loop

lines = fso.Line-1

这样的东西不会更快吗

相关问题