Function to count number of lines in a text file

后端 未结 7 707
栀梦
栀梦 2020-12-29 08:04

Need a function that will accept a filename as parameter and then return the number of lines in that file.

Should be take under 30 seconds to get the count of a 10 m

相关标签:
7条回答
  • 2020-12-29 08:52

    I was looking for a faster way than what I already had to determine the number of lines in a text file. I searched the internet and came across 2 promising solution. One was a solution based on SQL thew other the solution I found here based on Fso by Kul-Tigin. I tested them and this is part of the result:

    Number of lines  Time elapsed  Variant
    --------------------------------------------------------
    110              00:00:00.70   SQL
    110              00:00:00.00   Vanilla VBA (my solution)
    110              00:00:00.16   FSO    
    --------------------------------------------------------
    1445014          00:00:17.25   SQL
    1445014          00:00:09.19   Vanilla VBA (my solution)
    1445014          00:00:17.73   FSO
    

    I ran this several times with large and small numbers. Time and again the vanilla VBA came out on top. I know this is far out of date, but for anyone still looking for the fastest way to determine the number of lines in a csv/text file, down here's the code I use.

    Public Function GetNumRecs(ASCFile As String) As Long
      Dim InStream As Long
      Dim Record As String
      InStream = FreeFile
      GetNumRecs = 0
      Open ASCFile For Input As #InStream
      Do While Not EOF(InStream)
        Line Input #InStream, Record
        GetNumRecs = GetNumRecs + 1
      Loop
      Close #InStream
    End Function
    
    0 讨论(0)
提交回复
热议问题