CodeItBetter Programming Another VB Programming Blog

Read last line from a text file

Posted on June 25, 2011
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
'Text File Handling - Read last line from a text file
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
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.