Read Number of lines in Large Text File VB6

后端 未结 3 1471
萌比男神i
萌比男神i 2021-01-15 01:16

I have text File of Size 230MB. I want to Count Number of Lines OF that File.

I tried \"Scripting.FileSystemOblect\" but it goes out Of

相关标签:
3条回答
  • 2021-01-15 01:49

    Normal Windows line breaks are CRLF, so you can count the LFs and add 1 to the count in cases where the last line of your files doesn't have one after it.

    In true VB (i.e. VB5, VB6, etc.) you can make use of the byte-oriented String operations to speed many tasks. If we can assume the text files contain ANSI then this is pretty fast:

    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 "big.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
    

    Given a 7,000,000 line file of 293MB it only takes 0.7 seconds here. But note that I had not rebooted to ensure that the file wasn't cached when I ran that test. Without caching (i.e. after a reboot) I'd expect it to take as long as 5 times that.

    Converting to handle Unicode text files is fairly simple. Just replace the B-functions by the non-B equivalents, make sure you set BUFSIZE to a multiple of 2, and search for vbLf instead of an ANSI LF byte.

    0 讨论(0)
  • 2021-01-15 02:05

    You can do it by reading each line into the same variable. There's no need to save all the lines:

    dim s as string
    dim n as integer
    
    open "filename.txt" for input as 1
    
    n = 0
    do while not eof(1)
      line input #1, s
      n = n + 1
      loop
    

    This has not been tested, and it's been a while since I've done any VB6, but it should be close.

    0 讨论(0)
  • 2021-01-15 02:07

    This takes about 6 seconds for me on a 480mb binary file with 1mil+ 0xD (vbcr)

    Dim buff() As Byte
    Dim hF As Integer
    Dim i As Long, n As Long
    
    hF = FreeFile(0)
    
    Open "c:\windows\Installer\2f91fd.msp" For Binary Access Read As #hF
    ReDim buff(LOF(hF) - 1)
    Get #hF, , buff()
    Close #hF
    
    For i = 0 To UBound(buff)
        If buff(i) = 13 Then n = n + 1
    Next
    
    MsgBox n
    
    0 讨论(0)
提交回复
热议问题