Sign in to follow this  
Followers 0

_FileCountLinesEx


5 posts in this topic

Posted

This is just to demonstrate the latest feature of V3.3.9.4+ Beta (V3.3.9.3 had a problem so it's not recommended to use this.)

ConsoleWrite('Number of lines: ' & _FileCountLinesEx(@ScriptFullPath) & @CRLF)

; Works only with V3.3.9.4 and above. This is a different approach to _FileCountLines using the new feature StringSplit('Text, Text', ',')[1].
Func _FileCountLinesEx($sFilePath)
    Local $sData = StringStripWS(FileRead($sFilePath), 2)
    If StringInStr($sData, @LF) Then
        Return StringSplit(StringStripCR($sData), @LF)[0]
    ElseIf StringInStr($sData, @CR) Then
        Return StringSplit($sData, @CR)[0]
    Else
        If StringLen($sFilePath) Then
            Return 1
        Else
            Return SetError(1, 0, 0)
        EndIf
    EndIf
EndFunc   ;==>_FileCountLinesEx

Share this post


Link to post
Share on other sites



Posted (edited)

guinness, does it work for larger files (100+ MB, I suppose). I made something like this by modifying Zinthose's functions (and it took 38 seconds on my pc on a 140 MB file):

#include <WinAPI.au3>
$COUNT = 0
$start = default
While 1
$res = _HexSearch("G:\WIN7\sources\boot.wim", StringToBinary("" & @crlf), $start)
$start = $res + 2
$COUNT = $COUNT + 1
If $res = -1 then exitloop
Wend
msgbox(64, "", "Lines count: " & $COUNT)
Func _HexSearch($FilePath, $BinaryValue, $StartOffset = Default)  
        Local $Buffer, $ptr, $hFile, $Result, $Read, $SearchValue, $Pos, $BufferSize = 2048
            If $StartOffset = Default      Then $StartOffset = 0
            If Not FileExists($FilePath)    Then    Return SetError(1, @error, 0)
            $fLen = FileGetSize($FilePath)
            If $StartOffset > $fLen      Then   Return SetError(2, @error, 0)
            If Not IsBinary($BinaryValue)   Then    Return SetError(3, @error, 0)
            If Not IsNumber($StartOffset)   Then    Return SetError(4, @error, 0)
            $SearchValue = BinaryToString($BinaryValue)
            $Buffer = DllStructCreate("byte[" & $BufferSize & "]")
            $ptr = DllStructGetPtr($Buffer)
                $hFile = _WinAPI_CreateFile($FilePath, 2, 2, 1)
                If $hFile = 0 Then Return SetError(5, @error, 0)
            $Result = _WinAPI_SetFilePointer($hFile, $StartOffset)
            $err = @error
            If $Result = 0xFFFFFFFF Then
                _WinAPI_CloseHandle($hFile)
                Return SetError(5, $err, 0)
            EndIf
            $Pos = $StartOffset
            While True
                    $Read = 0
                    $Result = _WinAPI_ReadFile($hFile, $ptr, $BufferSize, $Read)
                    $err = @error
                    If Not $Result Then
                        _WinAPI_CloseHandle($hFile)
                        Return SetError(6, $err, 0)
                    EndIf
                    $Result = DllStructGetData($Buffer, 1)
                    $Result = BinaryToString($Result)
                    $Result = StringInStr($Result, $SearchValue)
                    If $Result > 0 Then ExitLoop
                    If $Read < $BufferSize Then
                        _WinAPI_CloseHandle($hFile)
                        Return -1
                    EndIf
                    $Pos += $Read
                  
            WEnd
            _WinAPI_CloseHandle($hFile)
            If Not $Result Then Return SetError(7, @error, 0)
            $Result = $Pos + $Result - 1
            Return $Result
    EndFunc

And yes, the latest beta does have improvements... thank you!!! ;)

Edited by MKISH

Share this post


Link to post
Share on other sites

Posted

Great, but my point was just to showcase the latest addition. I wanted to add this to the snippets section but it was discussed with that only code that works with stable version should be added. I think the function should be used on source/text files really.

Share this post


Link to post
Share on other sites

Posted

Thanks for your quick reply, friend. And yes, the example is OK. I just found another method so I figured I'll tell everyone about it.

Good day/Good night to all... I shall go to sleep now...

Share this post


Link to post
Share on other sites

Posted

Why not post it to the snippets section?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0