Jump to content



Photo

_FileCountLinesEx

beta _FileCountLinesEx

  • Please log in to reply
4 replies to this topic

#1 guinness

guinness

    all-consuming swarm in inconspicuous disguise

  • Developers
  • 18,147 posts

Posted 20 May 2012 - 02:26 PM

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

UDF List:
Spoiler
Updated: 31/03/2015







#2 stormbreaker

stormbreaker

    More wits this time...

  • Active Members
  • PipPipPipPipPipPip
  • 547 posts

Posted 20 May 2012 - 04:34 PM

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

AutoIt         
#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, 20 May 2012 - 04:35 PM.

----------------------------------------

:bye: Hey there, was I helpful?

----------------------------------------

My Current OS: Win8 PRO (64-bit); Current AutoIt Version: v3.3.8.1


#3 guinness

guinness

    all-consuming swarm in inconspicuous disguise

  • Developers
  • 18,147 posts

Posted 20 May 2012 - 04:50 PM

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.
UDF List:
Spoiler
Updated: 31/03/2015

#4 stormbreaker

stormbreaker

    More wits this time...

  • Active Members
  • PipPipPipPipPipPip
  • 547 posts

Posted 20 May 2012 - 04:55 PM

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...

----------------------------------------

:bye: Hey there, was I helpful?

----------------------------------------

My Current OS: Win8 PRO (64-bit); Current AutoIt Version: v3.3.8.1


#5 guinness

guinness

    all-consuming swarm in inconspicuous disguise

  • Developers
  • 18,147 posts

Posted 20 May 2012 - 05:00 PM

Why not post it to the snippets section?
UDF List:
Spoiler
Updated: 31/03/2015




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users