Results 1 to 3 of 3

Thread: HUGE BIG txt file

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Italy-Napoli
    Posts
    2,022

    HUGE BIG txt file

    Code:
    Option Explicit
    Private Sub Main()
    
        Const BUFSIZE As Long = 100000
        Dim T0 As Single
        Dim LfAnsi As String
        Dim F As Integer
        Dim FileBytes As Long
        Dim BytesLeft As Long
        Dim Buffer() As Byte
        Dim strBuffer As String
        Dim BufPos As Long
        Dim LineCount As Long
    
        T0 = Timer()
        LfAnsi = StrConv(vbLf, vbFromUnicode)
        F = FreeFile(0)
        Open "C:\TEST\2014.TXT" For Binary Access Read As #F
        FileBytes = LOF(F)
        ReDim Buffer(BUFSIZE - 1)
        BytesLeft = FileBytes
        Do Until BytesLeft = 0
            If BufPos = 0 Then
                If BytesLeft < BUFSIZE Then ReDim Buffer(BytesLeft - 1)
                Get #F, , Buffer
                strBuffer = Buffer    'Binary copy of bytes.
                BytesLeft = BytesLeft - LenB(strBuffer)
                BufPos = 1
            End If
            Do Until BufPos = 0
                BufPos = InStrB(BufPos, strBuffer, LfAnsi)
                If BufPos > 0 Then
                    LineCount = LineCount + 1
                    BufPos = BufPos + 1
                End If
            Loop
        Loop
        
        Close #F
        'Add 1 to LineCount if last line of your files do not
        'have a trailing CrLf.
        
        MsgBox "Counted " & Format$(LineCount, "#,##0") & " lines in" & vbNewLine _
               & Format$(FileBytes, "#,##0") & " bytes of text." & vbNewLine _
               & Format$(Timer() - T0, "0.0#") & " seconds."
               
    End Sub
    Googling ... found this code.
    To understand, to the end of code, i can loop the result of array, line by line?

    Note:
    my file have approx 912.xxx lines and have 45 MB of dimension
    All lines are 132 fixed lenght
    Last edited by luca90; Mar 14th, 2015 at 04:17 PM.

  2. #2
    PowerPoster
    Join Date
    Jun 2013
    Posts
    4,930

    Re: HUGE BIG txt file

    Quote Originally Posted by luca90 View Post
    my file have approx 912.xxx lines and have 45 MB of dimension
    All lines are 132 cheracter fixed
    In case you only want to find out the amount of lines in the file (as the posted code did)
    and since in your case, you have a fixed amount of "chars per line", is it not enough to do:

    Code:
    Function GetLineCount(FileName, Optional CharsPerLine=132, Optional LF = vbCrLf)
      GetLineCount= (FileLen(FileName) + Len(LF)) \  (CharsPerLine + Len(LF))
    End Function
    Olaf

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,965

    Re: HUGE BIG txt file

    to use the above code and loop through the lines, you should probably change the buffer size to multiple of line length + 2 (or 1 as appropriate for end of line characters) else it will break the file mid line
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

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