Jump to content

Implementing FileSeek


Queue
 Share

Recommended Posts

What I want to accomplish:

Reading from the end of a log file that constantly gets data appended. This log file is too big for the standard methods of just loading it in its entirety into memory, doing a line count and then using FileReadLine, etc. AutoIt constantly checks the file's size and should only read in new data when the file size increases (if it decreases, it simply sets its last known size to whatever the new smaller size is).

What I'm looking for help with:

Is there a way to shift AutoIt's file pointer for the currently opened file to a given offset (so it thinks the beginning of the file is actually X bytes in)? Even if it's a fairly dirty hack (modifying AutoIt's own memory after a FileOpen command to shift the file pointer), it would be acceptable.

Something I've tried that doesn't work:

FileReadLine($file_handle, -2)

Or -3, etc. Only -1 works and that most recent line is often not enough (sometimes multiple lines get appended at once).

Queue

Edited by Queue
Link to comment
Share on other sites

In the current AutoIt-Version there is a function called FileSetPos :D

To read the new contents, you could just do this. Then, AutoIt reads everything that was added since the last FileRead-command:

$hFile = FileOpen($file, 0) ; open for reading
FileSetPos($hFile, 0, 2) ; move to end
OnAutoItExitRegister("_Close") ; close on Exit

While 1
    Sleep(500)
    $sNewLines = FileRead($hFile) ; read everything from current position to end
    If Not @error Then ; when something was read
        MsgBox(0, 'New Text', $sNewLines)
    EndIf
WEnd

Func _Close()
    FileClose($hFile)
EndFunc
Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

In the current AutoIt-Version there is a function called FileSetPos :D

Crud, that figures. I can't use versions beyond 3.2.12.X due to Win9x support being dropped. For any NT projects though, this information will be quite handy. Thank you.

Also, it looks like the _WinAPI_SetFilePointer, ReadFile, etc. calls will be acceptable. Funny how you only manage to find an answer AFTER you ask the question out loud.

Queue

Edited by Queue
Link to comment
Share on other sites

You could try to use KernelEx on your Win98-machines.

Or try to use the append-mode to open a file. Then the filepointer is at the end of the file, and when another app adds something, you should be able to read it.

$hFile = FileOpen($file, 1) ; open for append

While 1
    Sleep(500)
    $sNewLines = FileRead($hFile) ; read everything from current position to end
    If Not @error Then ; when something was read
        MsgBox(0, 'New Text', $sNewLines)
    EndIf
WEnd

Func OnAutoItExit()
    FileClose($hFile)
EndFunc
Edited by ProgAndy

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

I actually know of (and use) KernelEx, but I can't guarantee it will be available on each machine I intend this script for (nor have I personally tested a newer AutoIt on machines with KernelEx since doing so is pointless when I can't guarantee it will be on all mach... you get the point).

Thanks for the idea with append, also good to know that affects the file pointer.

Queue

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...