[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Contiuously read from a file as it is appended to

Posted on 2004-11-01
6
Medium Priority
?
146 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

650 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