Function to count number of lines in a text file

后端 未结 7 706
栀梦
栀梦 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:33

    You could try some variation on this

    cnt = 0
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set theFile = fso.OpenTextFile(filespec, ForReading, False)
    Do While theFile.AtEndOfStream <> True
       theFile.SkipLine
       c = c + 1
    Loop
    theFile.Close
    WScript.Echo c,"lines"
    
    0 讨论(0)
  • 2020-12-29 08:33
    txt = "c:\YourTxtFile.txt"
    j = 0
    Dim read
    Open txt For Input As #1
      Do While Not EOF(1)
        Input #1, read
        j = j + 1
      Loop
    Close #1
    

    If it adds an empty last line the result is (j - 1).

    It works fine for one column in the txt file.

    0 讨论(0)
  • 2020-12-29 08:34

    If somebody still looking for faster way, here is the code:

    Set fso = CreateObject("Scripting.FileSystemObject") 
    Set theFile = fso.OpenTextFile("C:\textfile.txt", 8, True) 
    WScript.Echo theFile.Line 
    Set Fso = Nothing
    

    Of course, the processing time depend very much of the file size, not only of the lines number. Compared with the RegEx method TextStream.Line property is at least 3 times quicker.

    0 讨论(0)
  • 2020-12-29 08:38

    How to count all lines in the notepad Answers: => Below is the code -

    Set t1=createObject("Scripting.FileSystemObject")
    Set t2=t1.openTextFile ("C:\temp\temp1\temp2_VBSCode.txt",1)
    Do Until t2.AtEndOfStream
    strlinenumber = t2.Line
    strLine = t2.Readline
    Loop
    msgbox strlinenumber
    t2.Close
    
    0 讨论(0)
  • 2020-12-29 08:40

    The only alternative I see is to read the lines one by one (EDIT: or even just skip them one by one) instead of reading the whole file at once. Unfortunately I can't test which is faster right now. I imagine skipping is quicker.

    Dim objFSO, txsInput, strTemp, arrLines
    Const ForReading = 1
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    strTextFile = "sample.txt"
    txsInput = objFSO.OpenTextFile(strTextFile, ForReading)
    
    'Skip lines one by one 
    Do While txsInput.AtEndOfStream <> True
        txsInput.SkipLine ' or strTemp = txsInput.ReadLine
    Loop
    
    wscript.echo txsInput.Line-1 ' Returns the number of lines
    
    'Cleanup
    Set objFSO = Nothing
    

    Incidentally, I took the liberty of removing some of your 'comments. In terms of good practice, they were superfluous and didn't really add any explanatory value, especially when they basically repeated the method names themselves, e.g.

    'Create a File System Object
    ... CreateObject("Scripting.FileSystemObject")
    
    0 讨论(0)
  • 2020-12-29 08:42

    Too large files...
    The following is the fastest-effeciently way I know of:

    Dim oFso, oReg, sData, lCount
    Const ForReading = 1, sPath = "C:\file.txt"
    Set oReg = New RegExp
    Set oFso = CreateObject("Scripting.FileSystemObject")
    sData = oFso.OpenTextFile(sPath, ForReading).ReadAll
    With oReg
        .Global = True
        .Pattern = "\r\n" 'vbCrLf
        '.Pattern = "\n" ' vbLf, Unix style line-endings
        lCount = .Execute(sData).Count + 1
    End With
    WScript.Echo lCount
    Set oFso = Nothing
    Set oReg = Nothing
    
    0 讨论(0)
提交回复
热议问题