Sign in to follow this  
Followers 0
dworld

Enhanched FileWriteLine?

9 posts in this topic

Hi Everybody,

after half day searching and other half day help reading i couldn't figure out how to write a line in a file and see it immediately... The problem with FileWriteLine func that only after all line has been written, can see the lines.. I hope i described clearly what i would like to achieve

I made a very simple example which is working as i wrote above:( :

;read a line from sys.log and write it to sys2.log 

$file = FileOpen("sys.log", 0)     
$fileout = FileOpen("sys2.log", 2) 

While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    sleep(50)       ; slow it down for testing..
    FileWriteLine($fileout, $line)
Wend

FileClose($fileout)
FileClose($file)

Until the script is running i'm trying to view the contents of the file, but it is empty till the script don't finish..

Thanks for any help in advance!


dworldI'm new in autoit, but I like it. My mind is open to the new things.

Share this post


Link to post
Share on other sites



How are you trying to "view" the contents of the file?

I can open either the source or the destination in notepad while the script is running and I will see contents.

For the destination file, "sys2.log", you will only get a snapshot to the last line written before opening notepad, but it proves viewing can be done.

Share this post


Link to post
Share on other sites

How are you trying to "view" the contents of the file?

I can open either the source or the destination in notepad while the script is running and I will see contents.

For the destination file, "sys2.log", you will only get a snapshot to the last line written before opening notepad, but it proves viewing can be done.

Well I checked it out, to this time I tried to view the sys2.log file with total commander viewer, now I opened with notepad, and the strange that for the 4th (the file contained already a lot of lines) opening I saw some lines, but by the first 3 opening it was with 0 length and was empty..

Well probably we should increase the sleep time to 200 to can see my problem


dworldI'm new in autoit, but I like it. My mind is open to the new things.

Share this post


Link to post
Share on other sites

You can't - yet. The next beta release of AutoIt will have a FileFlush() function which can be used to manually flush file contents to disk There is no ETA for the next beta.

Share this post


Link to post
Share on other sites

You can't - yet. The next beta release of AutoIt will have a FileFlush() function which can be used to manually flush file contents to disk There is no ETA for the next beta.

Thank you for the info Valik!

Actually i was wandering about this flush and today i tried to use _WinAPI_FlushFileBuffers($filehandle) without success (it caused the destination file stood empty)

Anyway thanks again for the fast replies :)


dworldI'm new in autoit, but I like it. My mind is open to the new things.

Share this post


Link to post
Share on other sites

Well I did it, I know it is not the best solution, but what can I do, I need this feature :)

;---------------------------------------------------------
#Include <File.au3>
;---------------------------------------------------------
$x = 1
$file = FileOpen("sys.log", 0)
$CountLines = _FileCountLines("sys.log")

While $x <> $CountLines
    _FileWriteLineEX($file, $x)
    Sleep(150)
    $x += 1
WEnd

FileClose($file)
;---------------------------------------------------------
Func _FileWriteLineEX($file, $x)
    
    $fileout = FileOpen("sys2.log", 1) 
    $line = FileReadLine($file, $x)

    ConsoleWrite("Line: " & $x & "Data: " & $line & @LF) 
    
    FileWriteLine($fileout, $line)  
    FileClose($fileout)
    
EndFunc
;---------------------------------------------------------

dworldI'm new in autoit, but I like it. My mind is open to the new things.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

You could have done the same thing by removing the FileOpen() and FileClose() for the FileWriteLine() (if you don't give it a handle it will automaticly open and close one, as the helpfile says)

edit: in case I wasn't clear enough, here is an example

$file = FileOpen("sys.log", 0)

While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    sleep(50)    ; slow it down for testing..
    FileWriteLine("sys2.log", $line)
Wend

FileClose($file)
Edited by AdmiralAlkex

Share this post


Link to post
Share on other sites

You could have done the same thing by removing the FileOpen() and FileClose() for the FileWriteLine() (if you don't give it a handle it will automaticly open and close one, as the helpfile says)

edit: in case I wasn't clear enough, here is an example

$file = FileOpen("sys.log", 0)

While 1
    $line = FileReadLine($file)
    If @error = -1 Then ExitLoop
    sleep(50)   ; slow it down for testing..
    FileWriteLine("sys2.log", $line)
Wend

FileClose($file)

wow, thx :):)


dworldI'm new in autoit, but I like it. My mind is open to the new things.

Share this post


Link to post
Share on other sites

wow, thx :):)

Yeah, the helpfile for AutoIt is really great, contains almost everything you need to know about how functions work etc.... (well except Sleep() but no one besides me seem ot care about it)

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  
Followers 0