Jump to content

Importing variables from a file


Theo
 Share

Recommended Posts

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

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • 2 months later...

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

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...