Jump to content

_filereadlastline UDF


Recommended Posts

My first udf, i needed this for a program and i thought, lets share it with you guys :)

#include <file.au3>
; Function Name:    _filereadlastline()
; Description:      Returns a certain line from a file by a 0-based index, starting at the end of the file
; Parameter(s):     $sFilePath - Path and filename of the file to be read
;                   $sline     - Line to read from the end of the file by a 0-based index       
; Requirement(s):   AutoIt3 Beta with COM support (post 3.1.1)
; Return Value(s):  On Success - Returns the contents of the specified line
;                   On Failure - Returns 0 and sets @ERROR = 1
; Author(s):        Frontmill
Func _filereadlastline($sFilePath, $iLine)
    $linecount = _FileCountLines($sFilePath)
    if @error then
        Return 0
        $line = $linecount - $iLine
        $string = FileReadLine($sFilePath, $line)
        Return $string
Edited by frontmill
Link to comment
Share on other sites

  • Moderators

Nice effort... I think I've done this somewhere before... maybe it's in Valuaters wrappers, but it was probably along the lines of:

Func _FileGetLastLine($hPath)
    Local $aSplit = StringSplit(StringStripCR(FileRead($hPath)), @LF)
    Return $aSplit[$aSplit[0]]

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites


If you have, say , a huge log file, it is quicker to use larry's binary read; use my "tailRW.au3" link in signature to see examples; 10s of millisecs instead of a number of secs, say, for a 80Mb file.

best, Randall

Link to comment
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...