I found the production version of _FileCountLines() improperly handles blank lines at the end of a file. It rarely reports the same number of lines shown in notepad or SciTE.
This modified function is 60-70% faster, uses half the memory (allowing it to read larger files and less likely to spit out "memory allocation" errors), and it properly handles blanks lines at the end of a file.
Anyone see anything wrong with the following replacement candidate?
Thank you.
Func _FileCountLines($sFilePath)
Local $hFile = FileOpen($sFilePath, $FO_READ)
If $hFile = -1 Then Return SetError(1, 0, 0)
Local $sFileContent = FileRead($hFile), $aTerminator[2] = ["n", "r"] ; linefeed, carriage return
FileClose($hFile)
For $x = 0 to 1
StringRegExpReplace($sFileContent, $aTerminator[$x], $aTerminator[$x])
If @extended Then
Local $count = @extended
If StringRight($sFileContent, 1) <> $aTerminator[$x] Then $count += 1
ExitLoop
EndIf
Next
If Not $count Then
If StringLen($sFileContent) Then
$count = 1 ; single-line file
Else
Return SetError(2, 0, 0) ; 0-byte file
EndIf
EndIf
Return $count
EndFunc ;==>_FileCountLines
Edit: I do see one might save the result of the StringRight() statement, and test that for the single-line/0-byte condition, eliminating the need for the StringLen() statement.