Results 1 to 4 of 4

Thread: VB6 - Quickly estimate the total number of lines in a file

  1. #1

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359

    VB6 - Quickly estimate the total number of lines in a file

    If you want to find out the total number of lines in a file, you can simply do something like this :

    VB Code:
    1. Private Function getLineCount(ByVal strFilePath As String) As Long
    2.     Open strFilePath For Binary As #1
    3.         Dim strBuff As String: strBuff = Space(Lof(1))
    4.         Get #1, , strBuff
    5.         getLineCount = UBound(Split(strBuff, vbCrLf))
    6.     Close #1
    7. End Function

    But if you're dealing with very large text files, then something like that could take a very long time to complete.
    So why not get an estimate on the number of lines ?

    VB Code:
    1. Private Function getLineCount(ByVal strFilePath As String) As Long
    2.     Dim i As Long, lngTotalLengthSoFar As Long, lngAverageLength As Long
    3.     Open strFilePath For Input As #1
    4.         Dim strBuff As String:  strBuff = Space(LOF(1))
    5.         Dim strBuff2 As String: strBuff2 = vbNullString
    6.         For i = 0 To 49
    7.             Line Input #1, strBuff2
    8.             lngTotalLengthSoFar = lngTotalLengthSoFar + Len(strBuff2)
    9.         Next
    10.         lngAverageLength = lngTotalLengthSoFar / 50
    11.         getLineCount = LOF(1) / lngAverageLength
    12.     Close #1
    13. End Function

    The above code will take the first 50 lines, and estimate from that, the total number of lines in the file.
    As with all statistical or analysis operations, the larger the source dataset the more accurate your results will be, so increasing the sample size from 50 to 100 or 200 would, on average, provide you with a more accurate result.

    At the same time, if you don't require brilliant accuracy, or know that each line is a very similar length and want to get the job done quick, then one could reduce the sample from 50 to 10 or 20 etc.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  2. #2
    So Unbanned DiGiTaIErRoR's Avatar
    Join Date
    Apr 1999
    Location
    /dev/null
    Posts
    4,111
    Reading the file in multiple parts(buffering) will speed up the calculation of lines.

    That way you can have speed and accuracy.

  3. #3

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    * 21-October-2004 - Moved to CodeBank *
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  4. #4
    New Member
    Join Date
    Nov 2006
    Posts
    1

    Re: VB6 - Quickly estimate the total number of lines in a file

    This method is faster!!!
    Initially i was reading the file line by line and using a counter to get the number of lines which was taking long time for big files

    Thanks.
    Last edited by Satgurunathan; Nov 10th, 2006 at 02:39 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width