Last line from external file

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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

Thanks in advance.
 
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top