Stuart McLachlan
stuart at lexacorp.com.pg
Mon Dec 1 19:39:27 CST 2008
I didn't have one five minutes ago, but I do now. Here you go:
Function LastLine(FileName As String) As String
Dim strFileContent As String
Dim strLastLine As String
' Get entire file into memory
Open FileName For Binary As #1
strFileContent = Space$(LOF(1))
Get #1, , strFileContent
Close #1
'Strip any trailing CrLf
If Right(strFileContent, 2) = Chr$(13) & Chr$(10) Then
strFileContent = Left$(strFileContent, Len(strFileContent) - 2)
End If
'Find Last CrLf and get everything after it
LastLine = Mid$(strFileContent, InStrRev(strFileContent, Chr$(13) &
Chr$(10)) + 1)
End Function
Cheers,
Stuart
On 2 Dec 2008 at 9:25, Darren D wrote:
> Does anyone have code to read the last line of a text file - the text file grows
> and shrinks so a fixed position read won't work.
>