Last line from external file

A

Angus

How can I read the last line from an external txt file and assign this to a
string veriable?

Thanks in advance.
 
D

Dirk Goldgar

Angus said:
How can I read the last line from an external txt file and assign
this to a string veriable?

Thanks in advance.

One way, at least, would be to open the file, read through till you come
to the last line, assign that to a variable, and then close the file.
Basic code:

Dim iFileNo As Integer
Dim sLine As String

iFileNo = FreeFile()

Open "C:\My Path\MyFile.txt" For Input As #iFileNo

Do Until EOF(iFileNo)
Line Input #iFileNo, sLine
Loop

Close iFileNo

' at this point the last line of the file is in sLine.

I'm sure it's possible to open the file for random access, position to
near the end, read what's left, and parse the last line out of that.
For a large file, that would be faster. But the above is simple and
easy.
 
Top