Home > How-To Library > Text File Handling

Read last line from a text file

**************************************************************** * © 2007 CodeItBetter http://www.codeitbetter.com * * This notice MUST stay intact for legal use * ****************************************************************
Public Function fnReadLastLine(strFileName As String) 'You can call this function as: 'Call fnReadLastLine("C:\1234f1r.txt") Dim fPos As Long, LastLine As String, aByte As Byte Dim testCRLF As String * 2 Const LF = 10 On Error GoTo fnReadLastLine_Error Open strFileName For Binary As #1 fPos = LOF(1) + 1 Do fPos = fPos - 2 Seek #1, fPos Get #1, , testCRLF Loop While testCRLF = vbCrLf Do fPos = fPos - 1 Seek #1, fPos Get #1, , aByte Loop Until aByte = LF Line Input #1, LastLine Close #1 MsgBox Trim$(LastLine) ExitHere: On Error GoTo 0 Exit Function fnReadLastLine_Error: MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure fnReadLastLine of Module Module1" End Function

If you would like to submit your code here please us. Do not forget to mention your name. We are always thankful to each and everyone of you who submitted their code here.