Jump to content
DeltaRocked

FileRead - continuously updated file

Recommended Posts

DeltaRocked
Posted (edited)

Hi,

I have been trying to read a file which is being continuously written by another process. At some point, the FileReadLine reaches EOF and the Second Process appends the file .

When using Filereadline and FileSetPos / FilegetPos , I am unable to go past the last FileReadLine. I have to always close the file and reopen it , set the file pointer position and then continue reading till EOF.

 

#include <FileConstants.au3>
Local $hFile, $sPos, $neof
$hFile = FileOpen('abc.txt', 0)
While 1
    $line = FileReadLine($hFile)
    If (@error = -1) Then
        $sPos = FileGetPos($hFile)
        ConsoleWrite(" ---- " & $sPos & ' ---- ' & @CRLF)
        FileClose($hFile)
        $hFile = FileOpen('abc.txt', 0)
        FileSetPos($hFile, $sPos, $FILE_BEGIN)
        $neof = 1
    Else
        $neof = 0
    EndIf
    If ($neof = 0) Then
        ConsoleWrite($line & @CRLF)
    EndIf
WEnd
looping.bat
rem -------------------
dir /s >> abc.txt
rem -------------------

At Command Prompt : looping.bat

How do I ensure that I can read the file as and when it gets updated.

[UPDATE]

I believe its done. but may require some fine tuning.

#include <FileConstants.au3>
Local $hFile, $sPos, $neof, $sFilePreviousPos_1, $sFileCurrentPos_1, $sFilePreviousPos, $error

$hFile = FileOpen('abc.txt', 0)
$sFileCurrentPos = FileGetPos($hFile)
While 1
    $line = FileReadLine($hFile)
    $error = @error
    $sFilePreviousPos = $sFileCurrentPos
    $sFileCurrentPos = FileGetPos($hFile)
    If ($error = -1) Then
        FileSetPos($hFile, $sFilePreviousPos, $FILE_BEGIN)
        $neof = 1
    Else
        $neof = 0
    EndIf
    If ($neof = 0) Then
            ConsoleWrite($line & @CRLF)
    EndIf
WEnd

 

 

Thanks in Advance.

 

Edited by DeltaRocked

Share this post


Link to post
Share on other sites
bernd670
Posted (edited)

Hello,

not so complicated, FileGetPos and FileSetPos is only needed if the same handle wants to read and write the file. Batch and AutoIt use different handles.

Try this:

While Not FileExists("abc.txt")
    Sleep(1000)
WEnd

$hFile = FileOpen("abc.txt", 0)

While 1
    $Line = FileReadLine($hFile)
    If Not @error Then ConsoleWrite($Line & @CRLF)
WEnd

 

Edited by bernd670

greetings
bernd


I hacked 127.0.0.1 -> pcfred6.gif

Share this post


Link to post
Share on other sites
jchd

Regular files offer zero guarantee for ACID properties, unless locks are set/released so that file integrity is maintained among non-cooperating threads/processes.


This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×