?
Solved

Contiuously read from a file as it is appended to

Posted on 2004-11-01
6
Medium Priority
?
151 Views
Last Modified: 2010-05-02
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
0
Comment
Question by:jfunderb
6 Comments
 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 12467719
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.
0
 
LVL 3

Expert Comment

by:sgayatri
ID: 12471368
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
0
 

Author Comment

by:jfunderb
ID: 12484063
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.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 16

Accepted Solution

by:
jimbobmcgee earned 1500 total points
ID: 12484474
>> 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...

HTH

J.
0
 

Author Comment

by:jfunderb
ID: 12485343
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
0
 
LVL 35

Expert Comment

by:[ fanpages ]
ID: 12487988
Glad you found a workable solution with/without my suggestion(s).

BFN,

fp.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

601 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question