Sign in to follow this  
Followers 0
lingo124

Script that checks a file keeps returning same data even if file was changed?

4 posts in this topic

I have a script that opens up a text file that contains information about a target hour, minute, and second. When I first run the script, the time showed is the time in the text file. However, when I put it in a loop and have it reload my file data, even if I change the txt file, the script still returns the old value.

#include <file.au3>
#include <array.au3>

global $target_min, $target_sec

while 1
    rereadconfig()
WEnd

Func rereadconfig()
    $file = "Z:\Documents\config.txt"
    $filehandle=FileOpen($file, 0)
    if ($filehandle=-1) Then
        MsgBox(0,"Missing Config File", "'config.txt' not present in this programs directory. Program will quit.")
        Exit
    EndIf

    local $config[2]
    For $i = 1 to _FileCountLines($file) step 3
        $config[($i/3)] = FileReadLine($file, ($i+1))
    Next
    FileClose($file)

    msgbox(0,"",$config[0]) 

    $target_min = $config[0]
    $target_sec = $config[1]

EndFunc

And my text file is 

target minute
54

target second
0

The for loop I used a step of 3 to only read in the 2nd and 5th lines.

Here is what happens when I run this script:

Run

msgbox pops up reading 54

at that point i change the 54 in the text file to say 25 and press save

then i press ok on the message box

msgbox pops up reading 54 again, even though the text file reads 25.

Is there something I'm missing? I closed the file each time it queries the text file, unless autoit stores a copy of the text file in the buffer.

Any help would be appreciated. Thanks!

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

lingo124,

Try it like this...see comment in code.  I might also recommend an .INI file instead of the .TXT file. 

#include <file.au3>
#include <array.au3>

global $target_min, $target_sec

while 1
    rereadconfig()
    sleep(250)
WEnd

Func rereadconfig()
    ;$file = "Z:\Documents\config.txt"
    $file = @scriptdir & '\config.txt'      ;   so it runs on my PC
    $filehandle=FileOpen($file, 0)
    if ($filehandle=-1) Then
        MsgBox(0,"Missing Config File", "'config.txt' not present in this programs directory. Program will quit.")
        Exit
    EndIf

    local $config[2]
    For $i = 1 to _FileCountLines($file) step 3
        $config[($i/3)] = FileReadLine($file, ($i+1))
    Next
    ;FileClose($file)
    FileClose($filehandle)  ; <<<------------  close the handle, not the file name

    msgbox(0,"",$config[0])

    $target_min = $config[0]
    $target_sec = $config[1]

EndFunc

kylomas

edir: I also added a sleep in the main loop to slow it down a bit

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

lingo124,

Try it like this...see comment in code.  I might also recommend an .INI file instead of the .TXT file. 

#include <file.au3>
#include <array.au3>

global $target_min, $target_sec

while 1
    rereadconfig()
    sleep(250)
WEnd

Func rereadconfig()
    ;$file = "Z:\Documents\config.txt"
    $file = @scriptdir & '\config.txt'      ;   so it runs on my PC
    $filehandle=FileOpen($file, 0)
    if ($filehandle=-1) Then
        MsgBox(0,"Missing Config File", "'config.txt' not present in this programs directory. Program will quit.")
        Exit
    EndIf

    local $config[2]
    For $i = 1 to _FileCountLines($file) step 3
        $config[($i/3)] = FileReadLine($file, ($i+1))
    Next
    ;FileClose($file)
    FileClose($filehandle)  ; <<<------------  close the handle, not the file name

    msgbox(0,"",$config[0])

    $target_min = $config[0]
    $target_sec = $config[1]

EndFunc

kylomas

edir: I also added a sleep in the main loop to slow it down a bit

Hmm, you're right?!Do you know why referring to it by handle and referring to it by name results in different results?

Also why an ini file as opposed to a txt?

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

lingo124,

Do you know why referring to it by handle and referring to it by name results in different results?

 

Technically, no.  However, I can speculate that when you open a handle to a resource all subsequent access is through the handle.  Whereas, accessing the file using the file name (with no previous open) does an open/close for each access.  I don't know the access method or control blocks for this architecture so can't offer more than that. 

As an example, this would also have worked...

#include <file.au3>
#include <array.au3>

global $target_min, $target_sec

while 1
    rereadconfig()
    sleep(250)
WEnd

Func rereadconfig()
    $file = @scriptdir & '\config.txt'      ;   so it runs on my PC
    local $config[2]
    For $i = 1 to _FileCountLines($file) step 3
        $config[($i/3)] = FileReadLine($file, ($i+1))
    Next
    msgbox(0,"",$config[0])

    $target_min = $config[0]
    $target_sec = $config[1]

EndFunc

The Help file is pretty specific about file* behaviors.

 

Also why an ini file as opposed to a txt?

 

Using a fixed line within a file for "control" type info limits flexibility and is prone to errors.  As opposed to a .INI type file which can be accessed by

section(s) and/or keyword.

Using an .INI file your code might look like this...

; ini file looks like this
;       [timing]
;       minutes = 15
;       seconds  = 13

local $target_min, $target_sec

while 1
    rereadconfig()
    sleep(250)
WEnd

Func rereadconfig()

    $target_min = iniread(@scriptdir & '\config.ini','timing','minutes','Not Found')
    $target_sec = iniread(@scriptdir & '\config.ini','timing','seconds','Not Found')
    msgbox(0,'Timing',$target_min & ' minutes and ' & $target_sec & ' seconds left till meltdown')


EndFunc

kylomas

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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