View Single Post
  #8   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.

Using a Reg Dump file like in the cited thread:

353958 records

8.30018749999726 Seconds!

But it looked like a good idea.

Regards,
Tom Ogilvy


"Dana DeLouis" wrote in message
...
Hi Dave. If interested in making it faster, you need a special trick.
This took 1.4715 Seconds! with 200,000 lines. HTH.

Sub Read_Last_10_Records()
'// = = = = = = = = = = = = = = = = =
'// By Dana DeLouis
'// = = = = = = = = = = = = = = = = =
Dim fso
Dim f
Dim t As Double 'Timer
Dim j As Long 'Loop counter
Dim s As String 'Dummy String
Dim NumberOfRecords As Long

Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const nLines = 10
Const File = "C:\Temp.tp"

t = Timer
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(File, ForAppending)
NumberOfRecords = f.Line - 1
f.Close

Set f = fso.OpenTextFile(File, ForReading)
For j = 1 To NumberOfRecords - nLines
s = f.SkipLine
Next

'You could put the rest into an Array
'v = Split((f.readall), vbLf)

For j = 1 To nLines
Debug.Print f.ReadLine
Next

f.Close
Set f = Nothing
Set fso = Nothing

Debug.Print
Debug.Print Timer - t & " Seconds!"
End Sub

The above code returned:

This is line 199991
This is line 199992
This is line 199993
This is line 199994
This is line 199995
This is line 199996
This is line 199997
This is line 199998
This is line 199999
This is line 200000

1.47153124999932 Seconds!

--
Dana DeLouis
Windows XP & Office XP
= = = = = = = = = = = = = = = = =


"Dave Peterson" wrote in message
...
I searched google in the *scripting* newsgroup and found this:

http://groups.google.com/groups?thre...%40tkmsftngp10

The last post in the thread contained this:

This code takes 4 seconds to get last 10 lines out of 221,000 Lines

regdmp
File.



john wrote:

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.


--

Dave Peterson