Jump to content
Sign in to follow this  
Turranius

Question on FileRead

Recommended Posts

Turranius

Greetings,

First time on the forums, but I've been using Autoit for a few years. Mostly for smaller stuff.

Anyway, I have a file I'm reading. My first question is this. Consider this code.

$file = FileOpen( "c:\Slask\execmgr.log", 0)

While 1
    $crap = FileRead($file)
    msgbox(1,"testing3", $crap )
WEnd

FileClose($file)

Not that hard, right? Open a file. Go into a loop, read the file in each loop and show it in a msgbox.

In the first loop, it shows the file fine. But then it stops that and $crap is just empty for the following loops. Why?

Here is what I am trying to do, and its working right now.

1: I have a loop that is checking a logfile for 2 different keywords on the last line. It checks every 250ms.

2: If finds the keywords, it shows a message in the traybar (basically, "installation started" and "Installation ended".

3: The lines I am looking for are "Raised Program Started Event for" and "Execution is complete for program".

As I said earlier, the above works fine.

The problem though, is that I only check the last line of the file: $line = FileReadLine($file,-1)

Sometimes, after "Execution is complete for program" the log spits out another line so fast that my script does not have time to pick it up since it only loops every 250 ms. The line I want is always above the new line.

In that case, I thought I read the number of lines for the file. Subtract 1 and read that line. Then I have the line I want.

I used the _FileCountLines to do so: $LineToRead = ( _FileCountLines($FileToRead) -1 )

The problem in this is that _FileCountLines tries to open the file (its already open) and then closes it. So my file is now closed and the

script starts taking up 50% CPU as soon as I use the _FileCountLines function.

Ok, so I stole the _FileCountLines into my own script and rewrote it to work on my already opened filed without closing it.

The sad part is that it uses : $sFileContent = StringStripWS(FileRead($hFile), 2)

And there we are back in my original question. It uses FileRead which only seems to work once inside a loop.

Basically, I can solve it if only FileRead worked inside my loop.

Same thing with this one:

$file = FileOpen( "c:\Slask\execmgr.log", 0)

$crap = "nada"

func test()
    $crap = FileRead($file)
EndFunc

While 1
    test()
    msgbox(1,"testing3", $crap )
WEnd

FileClose($file)

Sorry for the long read and thanks for any advice.

Edit: Oh, if there is an easier way to read the second to last line in a textfile, that would be even easier.

I tried -2 of course instead of -1 (for last line) but that was a no go =)

Edited by Turranius

Share this post


Link to post
Share on other sites
Turranius

Hi again,

I managed to solve it, but the initial question about the loops still remain.

It works with a filename directly, but not if you first open the file.

While 1
    $crap = FileRead("c:\Slask\execmgr.log")
    msgbox(1,"testing3", $crap )
WEnd

works fine =)

Share this post


Link to post
Share on other sites
Tvern

When you use FileOpen/FileClose you can loop FileRead to read pieces of the file one after the other.

So if you run "FileRead($hFile,5)" twice, it will first read character 1-5 and then character 6-10.

When the end of the file is reached it will return nothing and set @error.

As you read the entire file the first time there is nothing left to read after that.

Share this post


Link to post
Share on other sites
Turranius

Ahh thats brilliant and explains why _FileCountLines opens and closes the file. Thank you!

I simply modified _FileCountLines to not open and close the file and instead use a "hardcoded" filename so its opened and closed automatically. Then it did not interfear with my previous FileOpen on the same file.

(and it only uses _FileCountLines very sparse, so its not creating a CPU problem doing that =) )

Edited by Turranius

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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.