Malkey Posted August 14, 2012 Posted August 14, 2012 (edited) Here are another couple of methods to return individual lines from the end of a file.expandcollapse popupLocal $sFile = "test.txt" ; http://www.autoitscript.com/forum/topic/143295-filereadline-can-you-read-second-last-line/page__st__20#entry1009431 ConsoleWrite(@LF & "===== File Name ======================================" & @LF) ConsoleWrite("The first line > " & _FileReadLine($sFile) & @LF) ; Returns first line. ConsoleWrite("The first line > " & _FileReadLine($sFile, "") & @LF) ; Returns first line (file handle not used). ConsoleWrite("The last line > " & _FileReadLine($sFile, -1) & @LF) ; Returns last line. ConsoleWrite("The penultimate line> " & _FileReadLine($sFile, -2) & @LF) ; Returns penultimate line. ConsoleWrite("The third last line > " & _FileReadLine($sFile, -3) & @LF) ; Returns third last line. ConsoleWrite(@LF & "===== File handle ==================================" & @LF) Local $file = FileOpen($sFile, 0) ConsoleWrite("The first line > " & _FileReadLine($file) & @LF) ; Returns first line. ConsoleWrite("The second line > " & _FileReadLine($file, "") & @LF) ; Returns first line (file handle not used). ConsoleWrite("The last line > " & _FileReadLine($file, -1) & @LF) ; Returns last line. ConsoleWrite("The penultimate line> " & _FileReadLine($file, -2) & @LF) ; Returns penultimate line. ConsoleWrite("The third last line > " & _FileReadLine($file, -3) & @LF) ; Returns third last line. FileClose($file) ConsoleWrite(@LF & "=== 2 StringInStrs =====================================" & @LF) ConsoleWrite("The last line > " & _FileReadEndLine($sFile, -1) & @LF) ; Returns last line. ConsoleWrite("The penultimate line> " & _FileReadEndLine($sFile, -2) & @LF) ; Returns penultimate line. ConsoleWrite("The third last line > " & _FileReadEndLine($sFile, -3) & @LF) ; Returns third last line. Local $s = FileRead($sFile) If StringRegExp($s, "v$") = 0 Then $s &= @LF ConsoleWrite(@LF & 'Three-liner RegExp returns the penultimate line> "' & _ StringRegExpReplace( $s, "^(V*R)+(V*)((V*R){2})", "2") & '"' & @LF) Func _FileReadLine($sFile, $iLine = "") If $iLine = "" Or $iLine = Default Then ; Successive calls with these parameters increments line number, only when used in conjunction with a file handle. Return FileReadLine($sFile) ElseIf $iLine < -1 Then FileSetPos($sFile, 0, 0);$FILE_BEGIN = 0 from FileConstants.au3 and NOT from Constants.au3 as help file says. Local $s = FileRead($sFile) If StringRegExp($s, "v$") = 0 Then $s &= @LF Return StringRegExpReplace($s, "^(V*R)+(V*)((V*R){" & (-1 * $iLine) & "})$", "2") Else Return FileReadLine($sFile, $iLine) EndIf EndFunc ;==>_FileReadLine Func _FileReadEndLine($sFile, $iLine = -1) Local $s = FileRead($sFile) If StringRegExp($s, "v$") = 0 Then $iLine += 1 Local $Fst = StringInStr($s, @LF, 2, $iLine - 1) + 1 Return StringMid($s, $Fst, (StringInStr($s, @LF, 2, $iLine)) - $Fst) EndFunc ;==>_FileReadEndLineEdit: Adjusted script so that correct results are returned when there is also no trailing linefeed in file. Edited August 14, 2012 by Malkey
guinness Posted August 14, 2012 Posted August 14, 2012 $FILE_BEGIN is in Constants.au3 as FileConstants.au3 is included. UDF List: _AdapterConnections() • _AlwaysRun() • _AppMon() • _AppMonEx() • _ArrayFilter/_ArrayReduce • _BinaryBin() • _CheckMsgBox() • _CmdLineRaw() • _ContextMenu() • _ConvertLHWebColor()/_ConvertSHWebColor() • _DesktopDimensions() • _DisplayPassword() • _DotNet_Load()/_DotNet_Unload() • _Fibonacci() • _FileCompare() • _FileCompareContents() • _FileNameByHandle() • _FilePrefix/SRE() • _FindInFile() • _GetBackgroundColor()/_SetBackgroundColor() • _GetConrolID() • _GetCtrlClass() • _GetDirectoryFormat() • _GetDriveMediaType() • _GetFilename()/_GetFilenameExt() • _GetHardwareID() • _GetIP() • _GetIP_Country() • _GetOSLanguage() • _GetSavedSource() • _GetStringSize() • _GetSystemPaths() • _GetURLImage() • _GIFImage() • _GoogleWeather() • _GUICtrlCreateGroup() • _GUICtrlListBox_CreateArray() • _GUICtrlListView_CreateArray() • _GUICtrlListView_SaveCSV() • _GUICtrlListView_SaveHTML() • _GUICtrlListView_SaveTxt() • _GUICtrlListView_SaveXML() • _GUICtrlMenu_Recent() • _GUICtrlMenu_SetItemImage() • _GUICtrlTreeView_CreateArray() • _GUIDisable() • _GUIImageList_SetIconFromHandle() • _GUIRegisterMsg() • _GUISetIcon() • _Icon_Clear()/_Icon_Set() • _IdleTime() • _InetGet() • _InetGetGUI() • _InetGetProgress() • _IPDetails() • _IsFileOlder() • _IsGUID() • _IsHex() • _IsPalindrome() • _IsRegKey() • _IsStringRegExp() • _IsSystemDrive() • _IsUPX() • _IsValidType() • _IsWebColor() • _Language() • _Log() • _MicrosoftInternetConnectivity() • _MSDNDataType() • _PathFull/GetRelative/Split() • _PathSplitEx() • _PrintFromArray() • _ProgressSetMarquee() • _ReDim() • _RockPaperScissors()/_RockPaperScissorsLizardSpock() • _ScrollingCredits • _SelfDelete() • _SelfRename() • _SelfUpdate() • _SendTo() • _ShellAll() • _ShellFile() • _ShellFolder() • _SingletonHWID() • _SingletonPID() • _Startup() • _StringCompact() • _StringIsValid() • _StringRegExpMetaCharacters() • _StringReplaceWholeWord() • _StringStripChars() • _Temperature() • _TrialPeriod() • _UKToUSDate()/_USToUKDate() • _WinAPI_Create_CTL_CODE() • _WinAPI_CreateGUID() • _WMIDateStringToDate()/_DateToWMIDateString() • Au3 script parsing • AutoIt Search • AutoIt3 Portable • AutoIt3WrapperToPragma • AutoItWinGetTitle()/AutoItWinSetTitle() • Coding • DirToHTML5 • FileInstallr • FileReadLastChars() • GeoIP database • GUI - Only Close Button • GUI Examples • GUICtrlDeleteImage() • GUICtrlGetBkColor() • GUICtrlGetStyle() • GUIEvents • GUIGetBkColor() • Int_Parse() & Int_TryParse() • IsISBN() • LockFile() • Mapping CtrlIDs • OOP in AutoIt • ParseHeadersToSciTE() • PasswordValid • PasteBin • Posts Per Day • PreExpand • Protect Globals • Queue() • Resource Update • ResourcesEx • SciTE Jump • Settings INI • SHELLHOOK • Shunting-Yard • Signature Creator • Stack() • Stopwatch() • StringAddLF()/StringStripLF() • StringEOLToCRLF() • VSCROLL • WM_COPYDATA • More Examples... Updated: 22/04/2018
AZJIO Posted August 14, 2012 Posted August 14, 2012 (edited) #include <FileConstants.au3> $Pos = 200 $hFile = FileOpen(@ScriptFullPath) FileSetPos($hFile, -$Pos, $FILE_END) $sText = FileRead($hFile) FileClose($hFile) While 1 $string = _ReadString($Pos) MsgBox(0, '', $string) ; ClipPut($string) If $Pos<1 Then ExitLoop WEnd Func _ReadString(ByRef $Pos) $TmpPos = StringInStr($sText, @CRLF, 0, -1, $Pos, $Pos) $string = StringMid($sText, $TmpPos + 2, $Pos - $TmpPos - 2) $Pos = $TmpPos Return $string EndFunc Edited May 8, 2013 by AZJIO My other projects or all
UEZ Posted August 14, 2012 Posted August 14, 2012 (edited) Here another one #include <File.au3> $sFile = @ScriptDir & "Testfile.txt" $hFile = FileOpen($sFile, 2) Global $sTxt For $i = 1 To $iLines $sTxt &= "Line number " & $i & @CRLF Next FileWrite($hFile, $sTxt) FileClose($hFile) MsgBox(0, "Test", GetLineContent($sFile, _FileCountLines($sFile) - 1)) Func GetLineContent($sFile, $iLineNum) Return StringRegExpReplace(FileRead($sFile), "^(.*v+?|$){" & $iLineNum & "}(?s).*", "$1") EndFunc Br, UEZ Edited August 14, 2012 by UEZ 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!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ
AZJIO Posted August 14, 2012 Posted August 14, 2012 Here another oneWhen row 30000 returns all of the text.My version is faster than StringSplit, if many rows. But it is impossible to obtain the specified string, only the follow- My other projects or all
UEZ Posted August 14, 2012 Posted August 14, 2012 (edited) I never tested it with huge lines!Anyway, when I run this I get an error: AutoIT3.exe ended.rc:-1073741571 (0xC00000FD).#include <File.au3> $sFile = @ScriptDir & "Testfile.txt" $iLines = 3957 ;3957 - 4096 is crashing the script and above 4096 the whole string will be displayed $hFile = FileOpen($sFile, 2) Global $sTxt For $i = 1 To $iLines $sTxt &= "Line number " & $i & @CRLF Next FileWrite($hFile, $sTxt) FileClose($hFile) $hFile = FileOpen($sFile) $sString = FileRead($hFile) FileClose($hFile) MsgBox(0, "Test", GetLineContent($sString, $iLines - 1)) Func GetLineContent($sString, $iLineNum) Return StringRegExpReplace($sString, "^(.*v+?|$){" & $iLineNum & "}(?s).*", "$1") EndFunc$iLines from 3957 up to 4096 is crashing the script and above 4096 returns the whole string. Strange.Seems to be a problem with StringRegExpReplace() and the iLineNum value. {x} seems to be limited to 3956.Edit: {x} has a limited recursion stack which runs out of stack and crashes when the limit is reached!Nice to have a one liner but not working properly for larger than 3956 lines So better forget this idea!Br,UEZ Edited August 14, 2012 by UEZ 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!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ
kiffab Posted August 16, 2012 Author Posted August 16, 2012 Hi GUys Thanks for all the great feedback. What i've done works but I also have all these options available to me. So... which is the best? ;-) Oh, and regarding my earlier question re event logs - I am still using my batch file method but with a string at the end of the file which must be present for my function to continue.
Biatu Posted January 29, 2017 Posted January 29, 2017 (edited) On 8/14/2012 at 10:29 AM, UEZ said: I never tested it with huge lines! Anyway, when I run this I get an error: AutoIT3.exe ended.rc:-1073741571 (0xC00000FD). #include <File.au3> $sFile = @ScriptDir & "Testfile.txt" $iLines = 3957 ;3957 - 4096 is crashing the script and above 4096 the whole string will be displayed $hFile = FileOpen($sFile, 2) Global $sTxt For $i = 1 To $iLines $sTxt &= "Line number " & $i & @CRLF Next FileWrite($hFile, $sTxt) FileClose($hFile) $hFile = FileOpen($sFile) $sString = FileRead($hFile) FileClose($hFile) MsgBox(0, "Test", GetLineContent($sString, $iLines - 1)) Func GetLineContent($sString, $iLineNum) Return StringRegExpReplace($sString, "^(.*v+?|$){" & $iLineNum & "}(?s).*", "$1") EndFunc $iLines from 3957 up to 4096 is crashing the script and above 4096 returns the whole string. Strange. Seems to be a problem with StringRegExpReplace() and the iLineNum value. {x} seems to be limited to 3956. Edit: {x} has a limited recursion stack which runs out of stack and crashes when the limit is reached! Nice to have a one liner but not working properly for larger than 3956 lines So better forget this idea! Br, UEZ Exactly, as in the first post, the best solution is for big files, sometimes exceeding the AutoIt memory limits. So anything that Reads the whole file is not perferred, such as ReadFile($hFile) or _FileReadToArray() On page 1 water already posted what looks like the best solution, and the fastest? Edited January 29, 2017 by Biatu What is what? What is what.
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