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.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

[ fanpages ]IT Services ConsultantCommented:

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?


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
jfunderbAuthor Commented:
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.
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

>> I want to be efficient and only read in the new data that I need.

Unfortunately, when you open a text file, the file marker starts at the beginning of the file.  That means you have to read in all lines, ignoring them until you get to the ones you need.

If the log file uses a standard carriage return & line feed to denote the end of a record, you could always use the SkipLine method of the TextStream object you use to read your file in.

I would poll the LOF of the file, every few seconds, as fanpages suggests, or the Size property of the File object you have assigned as your file.

Look up info on the FileSystemObject, for more details...



Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jfunderbAuthor Commented:
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
    Sleep 2000 ' Pause for two seconds

End Sub
[ fanpages ]IT Services ConsultantCommented:
Glad you found a workable solution with/without my suggestion(s).


It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.