Sign in to follow this  
Followers 0
kiboost

fileclose() not closing

3 posts in this topic

#1 ·  Posted (edited)

Hi,

I have a problem with a txt file I read, open, write to, and close.

The script doesn't close the file. Later in the script I read it again with fileread() and close it, and the first time I do it, it doesn't show. second time, it show ok. Also in windows explorer I don't have visualisation of the file till I exit the script.

Basically, the script check the number of lines of the txt, if more than 50 lines it erase and rewrite the 30 first line (so fileopen(,2).

Here is the script ;

#Region ### Reset full log
    $fulllog = "\RFRlog.txt"
    $keeplines = 50
    $loglines = FileRead($fulllog, FileGetSize($fulllog))

    $loglines = StringSplit($loglines, @CRLF, 1)
    if $loglines[0] > $keeplines - 20 Then
        $tokeep = ""

        for $i = 1 to ($keeplines-0) Step 1
            $tokeep = $tokeep&$loglines[$i]&@CRLF
        Next

        FileOpen($fulllog, 2)
        FileWrite($fulllog, $tokeep)
        FileClose($fulllog)

    EndIf

#EndRegion ### Reset full log
;c

; later in full script :
$log = "\RFRlog.txt"
$bibi = FileRead($log, FileGetSize($log))
FileClose($log)

msgbox(0,"",$bibi)

$log = "\RFRlog.txt"
$bibi = FileRead($log, FileGetSize($log))
FileClose($log)

msgbox(0,"",$bibi)

First msgbox : nothing !

Second msgbox : I get it !

I've tried to put other fileclose later, some fileflush etc, can't debug this one :/

Kib

Edited by kiboost

Win7 pro x64. scripts compiled to x64. - Autoit v3.3.6.1 | Scite 1.79

Share this post


Link to post
Share on other sites



Re-read how fileopen is used. You need to use the handle that it returns for further interactions with the file, or ommit FileOpen completely, in which case the Open and Close actions are done implicitly during each file read/write action. This is not advisable when doing more than one action on a file as it will open and close for each action.

Here is an example of how to use the handle:

#Region ### Reset full log
    $fulllog = "\RFRlog.txt"
    $keeplines = 50
    $loglines = FileRead($fulllog, FileGetSize($fulllog))
;~  FileClose($fulllog) ; No point in this. If you pass fileread a location string it opens and closes automatically

    $loglines = StringSplit($loglines, @CRLF, 1)
    if $loglines[0] > $keeplines Then
    $tokeep = ""

    for $i = 1 to ($keeplines-20) Step 1
    $tokeep = $tokeep&$loglines[$i]&@CRLF
    Next

    $hFile = FileOpen($fulllog, 2) ;Store the FileOpen handle
    FileWrite($hFile, $tokeep) ;use the handle to write to the file
    FileClose($hFile) ;close the handle.

    EndIf

#EndRegion ### Reset full log
;c

$bibi = "done"
msgbox(0,"",$bibi)

Share this post


Link to post
Share on other sites

Thanks for these clarifications Tvern !! I will have to clean some fileclose elsewhere then ;-)

Indeed your solution works great !

Thanks again,

Kib


Win7 pro x64. scripts compiled to x64. - Autoit v3.3.6.1 | Scite 1.79

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