maloysius Posted April 1, 2020 Share Posted April 1, 2020 (edited) Hi all, I have been searching for like 2 hours and can't seem to quite find anything that suits my need, or that I am capable of understanding easily with my noobishness. I am trying to open a file for reading in AutoIt that constantly is growing by an undetermined amount daily. I want it to be able to open the file and read the last 10 lines every time, and then search those lines for a particular set of substrings. I was messing around with this example from another post for the initial file read, but it doesn't seem to do anything: $FilePath = "Test.txt" $sRead = FileRead($FilePath) $100Lines = StringTrimLeft($sRead, StringInStr($sRead, @CRLF, "", -10)+1) ConsoleWrite($100Lines) This is just a literal copy of another's code, I have been messing with it but I have never gotten it to even print anything at all. I just don't even know what to do to make this work Does anyone out there have any tips for me or a direction to point towards? Thank you! Edited April 1, 2020 by maloysius Link to comment Share on other sites More sharing options...
Danp2 Posted April 1, 2020 Share Posted April 1, 2020 Check out _FileCountLines in the help file. Use it to get the line count, subtract 10, then use FileReadLine in a loop. maloysius 1 Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
maloysius Posted April 1, 2020 Author Share Posted April 1, 2020 Danp2, you have helped me greatly sir! It's working now. Thanks a lot!! Link to comment Share on other sites More sharing options...
jchd Posted April 2, 2020 Share Posted April 2, 2020 A faster way, by not parsing the file twice: Local $s = FileRead("yourfile.txt") $s = StringRegExpReplace($s, "(?ms).*?((?:^\N*(?:\R|$)){10}\z)", "$1") ConsoleWrite($s) This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt) Link to comment Share on other sites More sharing options...
iamtheky Posted April 2, 2020 Share Posted April 2, 2020 (edited) #include<array.au3> msgbox(0, '' , _ArrayToString(FileReadToArray("test.txt") , @LF , @extended - 10)) this one only pulls the last 10 $fTest = fileopen("test.txt") $i = 10 $sOut = "" $total = ubound(filereadtoarray($ftest)) ;_filecountlines($ftest) do $sOut &= FileReadLine($fTest , $total - $i) & @LF $i -= 1 until $i = 0 fileclose($ftest) msgbox(0, '' , $sOut) Edited April 2, 2020 by iamtheky ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
Zedna Posted April 2, 2020 Share Posted April 2, 2020 (edited) You can use this my optimization FileReadLastChar() to read only part of file from end and parse only this part and not the whole (big?) file: Here is simplified version using native functions: msgbox(0, '' , FileReadLastChar('c:\test.txt' , 1024)) Func FileReadLastChar($file , $count) $hFile = FileOpen($file) $pos = FileSetPos($hFile , $count * -1 , 2) $sOut = FileRead($hFile) return $sOut EndFunc Edited April 2, 2020 by Zedna Resources UDF ResourcesEx UDF AutoIt Forum Search Link to comment Share on other sites More sharing options...
Nine Posted April 2, 2020 Share Posted April 2, 2020 And yet another way : #include <Constants.au3> $s = FileRead ("a.txt") MsgBox ($MB_SYSTEMMODAL,"",StringMid($s,StringInStr($s,@CRLF,$STR_CASESENSE,-10)+2)) “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Bitnugger Posted April 9, 2020 Share Posted April 9, 2020 On 4/2/2020 at 1:23 PM, Nine said: MsgBox ($MB_SYSTEMMODAL,"",StringMid($s,StringInStr($s,@CRLF,$STR_CASESENSE,-10)+2)) If the last line ends with @CRLF, you will only see the last 9 lines with text. With the solution from @jchd, 10 lines of text are still displayed and it also works if the line breaks consist only of @LF (Unix), which is not the case with your solution. Here's how it works better: #include <Constants.au3> $s = FileRead ("a.txt") $s = StringStripWS($s, $STR_STRIPTRAILING) $s = StringReplace($s, @CRLF, @LF) $s = StringReplace($s, @LF, @CRLF) ConsoleWrite(StringMid($s, StringInStr($s, @CRLF, $STR_CASESENSE, -10) + 2) & @CRLF) ... but I would rather take the solution from @jchd. Link to comment Share on other sites More sharing options...
Nine Posted April 9, 2020 Share Posted April 9, 2020 (edited) nvm - I reused the $s, still faster but not as much as I thought. #include <Constants.au3> $s = FileRead ("a.txt") $hTimer = TimerInit () $s = StringStripWS($s, $STR_STRIPTRAILING) $s = StringReplace($s, @CRLF, @LF) $s = StringReplace($s, @LF, @CRLF) ConsoleWrite(StringMid($s, StringInStr($s, @CRLF, $STR_CASESENSE, -10) + 2) & @CRLF) ConsoleWrite (TimerDiff ($hTimer) & @CRLF) $s = FileRead ("a.txt") $hTimer = TimerInit () $s = StringReplace(StringStripWS($s, $STR_STRIPTRAILING+$STR_STRIPSPACES),@LF,@CR) ConsoleWrite (StringMid($s,StringInStr($s,@CR,$STR_CASESENSE,-10)+1) & @CRLF) ConsoleWrite (TimerDiff ($hTimer) & @CRLF) $s = FileRead ("a.txt") $hTimer = TimerInit () $s = StringRegExpReplace($s, "(?ms).*?((?:^\N*(?:\R|$)){10}\z)", "$1") ConsoleWrite($s) ConsoleWrite (TimerDiff ($hTimer) & @CRLF) Edited April 9, 2020 by Nine “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
UEZ Posted April 9, 2020 Share Posted April 9, 2020 (edited) Try this: ConsoleWrite(FileReadLastNLines("C:\Temp\swtag.log") & @CRLF) Func FileReadLastNLines($sFile, $iLines = 9) Local $hFile = FileOpen($sFile) If @error Then Return SetError(1, 0, 0) Local $sResult = FileRead($hFile) FileClose($hFile) Local $iPos = FileGetSize($sFile) For $i = 1 To $iLines $iPos = StringInStr($sResult, @LF, 1, -1, $iPos) - 1 Next Return StringMid($sResult, $iPos + 2) ;assuming that @CRLF is the line break EndFunc Edited April 9, 2020 by UEZ added Zedna's suggestion Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
Zedna Posted April 9, 2020 Share Posted April 9, 2020 (edited) @all For speed optimization always use CaseSensitive flag (=1) in StringInStr() and StringReplace() when searching/replacing @LF or @CRLF (or any other special characters). The difference is really BIG, especially on BIG texts or inside loops with many repetitions! StringInStr($text, @CRLF) --> StringInStr($text, @CRLF, 1) StringReplace($text, @CRLF, @LF) --> StringReplace($text, @CRLF, @LF, 0, 1) Edited April 9, 2020 by Zedna Resources UDF ResourcesEx UDF AutoIt Forum Search Link to comment Share on other sites More sharing options...
MrCreatoR Posted April 9, 2020 Share Posted April 9, 2020 Without reading the whole file: #include <FileConstants.au3> $sFile = @ScriptDir & '\MyFile.txt' ConsoleWrite(_FileReadLinesFromEnd($sFile, 10) & @CRLF) Func _FileReadLinesFromEnd($sFile, $iLines) Local $iOffset = -2, $iCount = -1, $sRet = '', $sChar Local $hFile = FileOpen($sFile) FileSetPos($hFile, $iOffset, $FILE_END) For $i = FileGetPos($hFile) To 0 Step -1 $sChar = FileRead($hFile, 2) $sRet = $sChar & $sRet If StringInStr($sChar, @LF, 2) Then $iCount += 1 If $iCount = $iLines Then ExitLoop EndIf EndIf $iOffset -= 2 FileSetPos($hFile, $iOffset, $FILE_END) Next FileClose($hFile) Return StringStripWS($sRet, 3) EndFunc Spoiler Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1 AutoIt Russian Community My Work... Spoiler Projects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize ProgramUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF Examples: ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating ) * === My topics === * ================================================== ================================================== AutoIt is simple, subtle, elegant. © AutoIt Team Link to comment Share on other sites More sharing options...
Bitnugger Posted April 10, 2020 Share Posted April 10, 2020 (edited) Here is my last version ... copied from @UEZ ... so far the fastest version. #include <Constants.au3> $sFile = "f:\Eigene Dateien\TxT\Windows Message Codes.txt" $hTimer = TimerInit() ConsoleWrite(FileReadLastNLines($sFile, 10)) ConsoleWrite("> Time: " & TimerDiff($hTimer) & @CRLF) Func FileReadLastNLines($sFile, $iLines = 9) Local $sResult = FileRead($sFile), $iFileSize = @extended If @error Then Return SetError(@error, 0, "") $sResult = StringStripWS($sResult, $STR_STRIPTRAILING) Return StringMid($sResult, StringInStr($sResult, @LF, $STR_CASESENSE, - $iLines, $iFileSize) + 1) & @CRLF EndFunc > Time: 0.7427 Edited April 10, 2020 by Bitnugger Link to comment Share on other sites More sharing options...
Zedna Posted April 10, 2020 Share Posted April 10, 2020 Here is my modification using my previous mentioned FileReadLastChars() to avoid reading of whole content of file into memory which is important for efficiency on large files (megabytes) #include <Constants.au3> $sFile = "c:\test.txt" $hTimer = TimerInit() ConsoleWrite(FileReadLastNLines($sFile, 10)) ConsoleWrite("> Time: " & TimerDiff($hTimer) & @CRLF) Func FileReadLastNLines($sFile, $iLines = 10, $iMaxRowLen = 1000) Local $sResult = FileReadLastChars($sFile, $iLines * ($iMaxRowLen+2)) $sResult = StringStripWS($sResult, $STR_STRIPTRAILING) Return StringMid($sResult, StringInStr($sResult, @LF, $STR_CASESENSE, - $iLines) + 1) & @CRLF EndFunc Func FileReadLastChars($file, $count) $hFile = FileOpen($file) $pos = FileSetPos($hFile , -1 * $count, 2) $ret = FileRead($hFile) FileClose($hFile) Return $ret EndFunc Resources UDF ResourcesEx UDF AutoIt Forum Search Link to comment Share on other sites More sharing options...
UEZ Posted April 10, 2020 Share Posted April 10, 2020 I would suggest to use both ways depending on file size. Of course it depends also on the lines you want read. Best it to make some test when which function is faster... Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
MrCreatoR Posted April 10, 2020 Share Posted April 10, 2020 expandcollapse popup#include <FileConstants.au3> #include <StringConstants.au3> Global $sFile = @TempDir & '\LinesTest.tmp' If Not FileExists($sFile) Then $sLines = '' For $i = 1 To 10000 $sLines &= 'Line #' & $i & @CRLF Next $hFile = FileOpen($sFile, 2) FileWrite($hFile, $sLines) FileClose($hFile) EndIf ;Call the functions first to avoid first run difference _Test_FileReadLinesFromEnd() _Test_FileReadLastNLines() _Test_FileReadLastNLines2() _SpeedTest('_Test_FileReadLinesFromEnd', 'By MrCreatoR') _SpeedTest('_Test_FileReadLastNLines', 'By Zedna') _SpeedTest('_Test_FileReadLastNLines2', 'By Zedna') Func _SpeedTest($sTestFunc, $sTitle = 'Testing', $iRepeat_Test = 5) Local $aTests[$iRepeat_Test + 1] = [$iRepeat_Test] Local $iTotal_Tests = 0 ConsoleWrite(StringFormat('+ Tests for "Func %s" (%s) started...\n=--------------------=\n', $sTestFunc, $sTitle)) Local $iDuration_Timer = TimerInit() For $x = 1 To $iRepeat_Test $iTimer = TimerInit() ;=== CODE TEST ==== Call($sTestFunc) ;=== CODE TEST === $aTests[$x] = Round(TimerDiff($iTimer), 3) $iTotal_Tests += $aTests[$x] ConsoleWrite(StringFormat('- Test #%i: %.2f ms\n', $x, $aTests[$x])) Next Local $iAverage = Round($iTotal_Tests / $iRepeat_Test, 3) ConsoleWrite(StringFormat('======================\n! Tests average: %.2f ms\n+ Test Duration: %.2f ms\n\n', $iAverage, TimerDiff($iDuration_Timer))) EndFunc Func _Test_FileReadLinesFromEnd() _FileReadLinesFromEnd($sFile, 10) EndFunc Func _Test_FileReadLastNLines() FileReadLastNLines($sFile, 10, 2000) EndFunc Func _Test_FileReadLastNLines2() FileReadLastNLines2($sFile, 10) EndFunc Func _FileReadLinesFromEnd($sFile, $iLines) Local $iOffset = -2, $iCount = -1, $sRet = '', $sChar Local $hFile = FileOpen($sFile) FileSetPos($hFile, $iOffset, $FILE_END) For $i = FileGetPos($hFile) To 0 Step -1 $sChar = FileRead($hFile, 2) $sRet = $sChar & $sRet If StringInStr($sChar, @LF, 2) Then $iCount += 1 If $iCount = $iLines Then ExitLoop EndIf EndIf $iOffset -= 2 FileSetPos($hFile, $iOffset, $FILE_END) Next FileClose($hFile) Return StringStripWS($sRet, 3) EndFunc Func FileReadLastNLines($sFile, $iLines = 10, $iMaxRowLen = 1000) Local $sResult = FileReadLastChars($sFile, $iLines * ($iMaxRowLen+2)) $sResult = StringStripWS($sResult, $STR_STRIPTRAILING) Return StringMid($sResult, StringInStr($sResult, @LF, $STR_CASESENSE, - $iLines) + 1) & @CRLF EndFunc Func FileReadLastChars($file, $count) $hFile = FileOpen($file) $pos = FileSetPos($hFile , -1 * $count, 2) $ret = FileRead($hFile) FileClose($hFile) Return $ret EndFunc Func FileReadLastNLines2($sFile, $iLines = 9) Local $sResult = FileRead($sFile), $iFileSize = @extended If @error Then Return SetError(@error, 0, "") $sResult = StringStripWS($sResult, $STR_STRIPTRAILING) Return StringMid($sResult, StringInStr($sResult, @LF, $STR_CASESENSE, - $iLines, $iFileSize) + 1) & @CRLF EndFunc This speed test results shows (on my machine) that Zenda's FileReadLastNLines with $iMaxRowLen = 2000 even faster then the others. Spoiler Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1 AutoIt Russian Community My Work... Spoiler Projects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize ProgramUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF Examples: ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating ) * === My topics === * ================================================== ================================================== AutoIt is simple, subtle, elegant. © AutoIt Team Link to comment Share on other sites More sharing options...
UEZ Posted April 10, 2020 Share Posted April 10, 2020 I can confirm that Zedna's version is the best for small and large files! For large file reading takes too long. Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
MrCreatoR Posted April 10, 2020 Share Posted April 10, 2020 (edited) It's faster, but problematic if the line length is unknown. My version is not much slower but not depends on line length. Edited April 10, 2020 by MrCreatoR Spoiler Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1 AutoIt Russian Community My Work... Spoiler Projects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize ProgramUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF Examples: ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating ) * === My topics === * ================================================== ================================================== AutoIt is simple, subtle, elegant. © AutoIt Team Link to comment Share on other sites More sharing options...
MrCreatoR Posted April 10, 2020 Share Posted April 10, 2020 Ok, here is new version, twice faster! expandcollapse popup#include <FileConstants.au3> #include <StringConstants.au3> Global $sFile = @TempDir & '\LinesTest.tmp' If Not FileExists($sFile) Then $sLines = '' For $i = 1 To 10000 $sLines &= 'Line #' & $i & @CRLF Next $hFile = FileOpen($sFile, 2) FileWrite($hFile, $sLines) FileClose($hFile) EndIf ;Call the functions first to avoid first run difference _Test_FileReadLinesFromEnd() _Test_FileReadLastNLines() _SpeedTest('_Test_FileReadLinesFromEnd', 'By MrCreatoR') _SpeedTest('_Test_FileReadLastNLines', 'By Zedna') Func _SpeedTest($sTestFunc, $sTitle = 'Testing', $iRepeat_Test = 5) Local $aTests[$iRepeat_Test + 1] = [$iRepeat_Test] Local $iTotal_Tests = 0 ConsoleWrite(StringFormat('+ Tests for "Func %s" (%s) started...\n=--------------------=\n', $sTestFunc, $sTitle)) Local $iDuration_Timer = TimerInit() For $x = 1 To $iRepeat_Test $iTimer = TimerInit() ;=== CODE TEST ==== Call($sTestFunc) ;=== CODE TEST === $aTests[$x] = Round(TimerDiff($iTimer), 3) $iTotal_Tests += $aTests[$x] ConsoleWrite(StringFormat('- Test #%i: %.2f ms\n', $x, $aTests[$x])) Next Local $iAverage = Round($iTotal_Tests / $iRepeat_Test, 3) ConsoleWrite(StringFormat('======================\n! Tests average: %.2f ms\n+ Test Duration: %.2f ms\n\n', $iAverage, TimerDiff($iDuration_Timer))) EndFunc Func _Test_FileReadLinesFromEnd() _FileReadLinesFromEnd($sFile, 10) EndFunc Func _Test_FileReadLastNLines() FileReadLastNLines($sFile, 10, 2000) EndFunc Func _FileReadLinesFromEnd($sFile, $iLines) Local $iOffset = -500, $iCount = -1, $sRet = '', $sChar Local $hFile = FileOpen($sFile) FileSetPos($hFile, $iOffset, $FILE_END) For $i = FileGetPos($hFile) To 0 Step -1 $sChars = FileRead($hFile, 500) $sRet = $sChars & $sRet StringReplace($sChars, @LF, '', 0, $STR_CASESENSE) If @extended >= $iLines Then ExitLoop EndIf $iOffset -= 500 FileSetPos($hFile, $iOffset, $FILE_END) Next FileClose($hFile) $sRet = StringStripWS($sRet, $STR_STRIPTRAILING) Return StringMid($sRet, StringInStr($sRet, @LF, $STR_CASESENSE, - $iLines) + 1) EndFunc Func FileReadLastNLines($sFile, $iLines = 10, $iMaxRowLen = 1000) Local $sResult = FileReadLastChars($sFile, $iLines * ($iMaxRowLen+2)) $sResult = StringStripWS($sResult, $STR_STRIPTRAILING) Return StringMid($sResult, StringInStr($sResult, @LF, $STR_CASESENSE, - $iLines) + 1) & @CRLF EndFunc Func FileReadLastChars($file, $count) $hFile = FileOpen($file) $pos = FileSetPos($hFile , -1 * $count, 2) $ret = FileRead($hFile) FileClose($hFile) Return $ret EndFunc Spoiler Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1 AutoIt Russian Community My Work... Spoiler Projects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize ProgramUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF Examples: ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating ) * === My topics === * ================================================== ================================================== AutoIt is simple, subtle, elegant. © AutoIt Team Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now