Contiuously read from a file as it is appended to

Posted on 2004-11-01
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.

Question by:jfunderb
    LVL 35

    Expert Comment

    by:[ fanpages ]

    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?


    LVL 3

    Expert Comment

    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

    Author Comment

    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.
    LVL 16

    Accepted Solution

    >> 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...



    Author Comment

    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
    LVL 35

    Expert Comment

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



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    The Complete Ruby on Rails Developer Course

    Ruby on Rails is one of the most popular web development frameworks, and a useful tool used by both startups and more established companies to build strong graphic user interfaces, and responsive websites and apps.

    There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
    Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    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…

    931 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now