udfFileGetLastLine
str udfFileGetLastLine (str)
;------------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfFileGetLastLine (strFilename)
intFilesize = FileSize (strFilename)
If !intFilesize Then Return ""
strLine = ""
intBBsize = 8192 ; Assumption: 8 kByte buffer, will cover some lines.
hdlBB = BinaryAlloc (intBBsize)
If BinaryReadEx (hdlBB, 0, strFilename, Max (0, intFilesize - intBBsize), intBBsize)
   BinaryReplace (hdlBB, @CRLF, @LF, @TRUE)
   BinaryReplace (hdlBB, @CR, "", @TRUE)
   intEod = BinaryEodGet (hdlBB) - 1
   intPosLF = BinaryIndexEx (hdlBB, intEod, @LF, @BACKSCAN, @TRUE)
   Switch @TRUE
   Case intPosLF < 0 ; Not a valid textfile or try a larger buffer.
      Break
   Case intPosLF < intEod ; Last Line without trailing @LF.
      strLine = BinaryPeekStr (hdlBB, intPosLF + 1, intEod)
      Break
   Case @TRUE ; Valid Line.
      intEod = intEod - 1
      intPosLF = BinaryIndexEx (hdlBB, intEod, @LF, @BACKSCAN, @TRUE)
      strLine = BinaryPeekStr (hdlBB, intPosLF + 1, intEod - intPosLF)
      Break
   EndSwitch
EndIf
hdlBB = BinaryFree (hdlBB)
Return strLine
;..........................................................................................................................................
; This UDF "udfFileReadLastLine" returns the last line of a textfile.
;
; Detlev Dalitz.20030107.20090428.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------


; Test.

strFilename = IntControl (1004, 0, 0, 0, 0) ; We use this script as test input file.
strMsgTitle = "Demo: udfFileGetLastLine (strFilename)"
strMsgText  = StrCat ('Filename = "', strFilename, '"', @LF, 'Last Line = "', udfFileGetLastLine (strFilename), '"')
Message (strMsgTitle, strMsgText)

Exit
;------------------------------------------------------------------------------------------------------------------------------------------