View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 27,285
Default FileSystemObject to get last 10 lines of text file.

If you look at Dave's link, the code by Gurgen Alaverdian is doing that.

allrecords = RegFile.Read(x.Size)

reads all the bytes into the variable allrecords as a huge text string.

this steps back through the string:

Y = 1
Do Until Y = nLines + 2
m = InStrRev(allrecords, vbCrLf, m - 1)
Y = Y + 1
Loop

It uses instrrev which is only available in VBA6 (xl2000 and later).

----------
Since you say it is appending to a single file, I would just keep a record
of the file size the last time you read it, then go to that point and start
reading. I don't think the filesystemobject offers any advantage for doing
that if you are doing it from Excel.

This is a good reference for fileio in Excel/VBA.
http://support.microsoft.com/support...eio/fileio.asp
File Access with Visual Basic® for Applications



Regards,
Tom Ogilvy



john wrote in message
...
Hi Tom,

Thank you for the insight.

I expect a Sequential read, would be most appropriate to the file, as it

has
random bits throughout and a header line... (the file is at work, that's

why
no sample supplied sorry)

When you say "you can read the 1 to n bytes into a string and then loop
backwards from the eof" ...would you mind explaining how would this be
achieved?

Tom Ogilvy wrote in message
...
What do you know about the file. Are all lines exactly the same length?

There is no magic to reading a file - it is a sequential read in most

cases.
A file is one to n bytes. The concept of a line is defined in Windows

by
encountering a crlf combination. Anything that would be faster would
involve calculating where to put the file pointer and if your lines

aren't
equal length, the alternative is to read the data and count the lines.

That
said, you can read the 1 to n bytes into a string and then loop

backwards
from the eof.

Look at the objects/properties/methods of textstream in the scripting
runtime and you will see there aren't any special methods beyond what I

have
described.

Regards,
Tom Ogilvy

john wrote in message
...
Hi there,

I am interested in finding out how to read the last 10 lines (or so)

of
a
text file using the FileSystemObject function, rather than looping

through
a
text file line-by-line until the end is reached then working

backwards.

I may be looking at reading the last 10 lines of a large text file

almost
every second, and would be interested in the fastest method available.

If anyone can explain how this could be done, I'd be very grateful!

Thank you.