Link to home
Start Free TrialLog in
Avatar of jfunderb
jfunderb

asked on

Contiuously read from a file as it is appended to

I would like to read in a file, pause for a second or so, and then looks to see if the file has been appended to.  If so, then read in just the new data.

Thanks,
Jacob
Avatar of [ fanpages ]
[ fanpages ]

Hi,

Are you looking for the complete code or just pointers?...

1) Define the "Sleep" API:
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

2) Read the file contents & store the LOF value.

3) Call the "Sleep" API for as many milliseconds as you need to wait, for example:
Sleep 5000

4) Compare stored LOF value & present LOF value (by re-opening file).

5) If the two differ (i.e. new LOF greater than stored LOF), re-read contents.

Is this sufficient detail, or do you need sample code?

BFN,

fp.
Use  end-of-file-marker say **

Whenever the file is created, use ** as the eof.

Search the file for ** -
Read the file after **  and till actual end of file

Now open your file for editing
search the file for **
remove it
go to end of file - add **

HOpe this would solve your problem
Avatar of jfunderb

ASKER

This is a log file and I don't have access to write to this file; only read.  It's almost a given that everytime I go to read the file there will be hundreds of new lines that I will want to parse through and act upon.  The issue is this:  Let's say the log file has grown to 10MB in size.  I don't want to read 10MB every few seconds to simple skip by until I get to those last hundred lines or so.  I want to be efficient and only read in the new data that I need.
ASKER CERTIFIED SOLUTION
Avatar of jimbobmcgee
jimbobmcgee
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks jimbob.  Didn't answer the question but you pointed me in the right direction (which is really all we can ask anyways).  Here's the code that is now doing what I wanted (case anyone else might need):

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Sub Form_Load()
Dim fso As New Scripting.FileSystemObject
Dim f As Scripting.File
Dim ts As Scripting.TextStream
dim temp as string

Set f = fso.GetFile("c:\Logs\2004-11-03.log")
Set ts = f.OpenAsTextStream(ForReading)
While 2 = 2 ' forever
    While Not ts.AtEndOfStream
        temp = ts.ReadLine
        ' perform parsing here
    Wend
    Sleep 2000 ' Pause for two seconds
Wend
ts.Close

End Sub
Glad you found a workable solution with/without my suggestion(s).

BFN,

fp.