View Single Post
  #10   Report Post  
Posted to microsoft.public.excel.programming
john john is offline
external usenet poster
 
Posts: 5
Default FileSystemObject to get last 10 lines of text file.

Hi Dana,

Your code is cracking stuff!

I'm getting strange return times, but it's always between fast and very
fast, so no problem!

If I don't find any other ways, this should do the trick.

Thank you.

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