开发者

Function to count number of lines in a text file

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 million line file.

Currently have something along the lines of - but it is too slow with large files:

Dim objFSO, strTextFile, strData, arrLines, LineCount
CONST ForReading = 1

'name of the text file
strTextFile = "sample.txt"

'Create a File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Open the text file - strData now contains the whole file
strData = objFSO.OpenTextFile(strTextFile,ForReading).ReadAll

'Split by lines, put into开发者_如何学Go an array
arrLines = Split(strData,vbCrLf)

'Use UBound to count the lines
LineCount = UBound(arrLines) + 1

wscript.echo LineCount

'Cleanup
Set objFSO = Nothing


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

Const ForAppending = 8
Set fso = CreateObject("Scripting.FileSystemObject") 
Set theFile = fso.OpenTextFile("C:\textfile.txt", ForAppending, Create:=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.


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")


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


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"


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.


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


I was looking for a faster way than what I already had to determine the number of lines in a text file. I searched the internet and came across 2 promising solution. One was a solution based on SQL thew other the solution I found here based on Fso by Kul-Tigin. I tested them and this is part of the result:

Number of lines  Time elapsed  Variant
--------------------------------------------------------
110              00:00:00.70   SQL
110              00:00:00.00   Vanilla VBA (my solution)
110              00:00:00.16   FSO    
--------------------------------------------------------
1445014          00:00:17.25   SQL
1445014          00:00:09.19   Vanilla VBA (my solution)
1445014          00:00:17.73   FSO

I ran this several times with large and small numbers. Time and again the vanilla VBA came out on top. I know this is far out of date, but for anyone still looking for the fastest way to determine the number of lines in a csv/text file, down here's the code I use.

Public Function GetNumRecs(ASCFile As String) As Long
  Dim InStream As Long
  Dim Record As String
  InStream = FreeFile
  GetNumRecs = 0
  Open ASCFile For Input As #InStream
  Do While Not EOF(InStream)
    Line Input #InStream, Record
    GetNumRecs = GetNumRecs + 1
  Loop
  Close #InStream
End Function
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜