Function to count number of lines in a text file

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

提交回复
热议问题