Jump to content
Sign in to follow this  
Theo

Importing variables from a file

Recommended Posts

Theo

Hey all, I need a bit of help here.

I have a Gui that writes a bunch of variables to a file. I then want the master script to import those variables. The variables are written one per line in the style: $variable = "value"

I first tried using a #include "GuiVars.au3", but it appears that autoit wants to read in all the includes at the beginning of the script no matter where they are located in the script. Then I moved to this:

Call( "_importVars" )   ;   Get the variables created.
    Call ( "_Rename", $hostname )   ;pass the new hostname to the rename function

Func _importVars()
    $file = FileOpen("C:\temp\2008_followUp\guiVars.au3", 0)

    ;   Check if file opened for reading OK
    If $file = -1 Then
        MsgBox(0, "Error", "Unable to open guivars.au3 file.")
        Exit
    EndIf

    ;   Read in lines of text until the EOF is reached
    While 1
        $line = FileReadLine($file)
        If @error = -1 Then ExitLoop
        Global $line    ;   Declare each var in GuiVars.aur as a global variable
        ; debug MsgBox(1, "Variable", "Current variable is: " & $line)
    Wend

    FileClose($file)
EndFunc

Unfortunaely, the line "Global $line" doesn not seem to be creating global variables because the next call function

call ( "_Rename", $hostname ) ;pass the new hostname to the rename function

errors out telling me I am using an undeclared variable. My debug statement says the variable should be getting declared correctly. Any thoughts? Is there a better way to import variables? -kp

Edited by Theo

Systems AdministratorSiemens: Windows Server SupportFly Fisher, Table Tennis Junkie, and Struggling Coder

Share this post


Link to post
Share on other sites
omikron48

Global $line

This declares a global variable, $line. I think what you're looking for is Assign or Eval functions. Check the help file.

Edited by omikron48

Share this post


Link to post
Share on other sites
FuryCell

This method works. It assumes the file is formatted without quotes however but that can be changed.

Call( "_importVars" )   ;   Get the variables created.

Func _importVars()
    $file = FileOpen("guivars.au3", 0)

    ;   Check if file opened for reading OK
    If $file = -1 Then
        MsgBox(0, "Error", "Unable to open guivars.au3 file.")
        Exit
    EndIf

    ;   Read in lines of text until the EOF is reached
    While 1
        $line = FileReadLine($file)
        If @error = -1 Then ExitLoop
        $Split=StringSplit($Line,"=")
        Assign(StringReplace($Split[1],"$",""),$Split[2],2)
    Wend
    FileClose($file)
EndFunc

And here is an example GUIVars.au3

$Var1=A
$Var2=B
Edited by P5ych0Gigabyte

HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.

Share this post


Link to post
Share on other sites
Theo

I see what is happening. I am declaring a Global variable called $line, rather than declaring Global <value of variable $line>

....how to fix...hmmmm


Systems AdministratorSiemens: Windows Server SupportFly Fisher, Table Tennis Junkie, and Struggling Coder

Share this post


Link to post
Share on other sites
FuryCell

I see what is happening. I am declaring a Global variable called $line, rather than declaring Global <value of variable $line>

....how to fix...hmmmm

Use assign or execute. Did you try my example?


HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.

Share this post


Link to post
Share on other sites
Theo

I can also just declare the variable Global first and then assign the value. sigh, sometimes I am just slow. thanks for your input as well. I have another place I can use your example so your time is not wasted!


Systems AdministratorSiemens: Windows Server SupportFly Fisher, Table Tennis Junkie, and Struggling Coder

Share this post


Link to post
Share on other sites
omikron48

Or if you don't store too much information, you can just use IniWrite and IniRead instead.

Edited by omikron48

Share this post


Link to post
Share on other sites
Theo

Thanks, I ended up using iniWrite, which with the addition of _StringEncrypt()for password data worked perfectly.


Systems AdministratorSiemens: Windows Server SupportFly Fisher, Table Tennis Junkie, and Struggling Coder

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.