Jump to content

Variable Population From Text File?


Recommended Posts

If I have three global variables, let's say...

$varA

$varB

$varC

and I have a really long script (hour execution time) that I want to be able to fail gracefully to allow for immediate restarting after fixing the origin of the problem without having to hardcode a bunch of debugging/temporary code at the start.

I have error handling where if a catastrophic error occurs, I want the script to "dump" the global variable contents to a text file from that moment in time. Then, when I restart, I want the script to read in those values from the dump file and repopulate my global variables to what they were. Essentially, I suppose I'm attempting to create rudimentary "sessions" with AutoIt using some sort of Reflection like in Java. Yes, I know I'm asking a lot of AutoIt ;)

So, if my dump file contains...

varA:15

varB=ABC

varC=green

My script should look for a dump file and if found, populate the variables. I have the code written to do all of the above except I'm using a case statement (which isn't dynamic since it's all hardcoded) to populate each of the variables if found.

Any ideas/suggestions?

Link to comment
Share on other sites

Look at the IniRead and Assign functions that might be of some help. You'd need to change the format of your dump file slightly to have the sections that an ini file would need but that shouldn't be hard to do.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Thank you! Assign() was the missing piece! I did convert my dump file to be an ini file and the sections will lend themselves perfectly to the 40 or so scripts I will have instead of 40 different dump files. So, now is there is any way to iterate through the global variables to grab the variable name strings so that I can create the key/value pairs in the ini file?

Link to comment
Share on other sites

Well, instead of individual variables, you could use an array and save each value to the ini in a simple For/Next loop.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Individual or array, no difference, it's getting the list of keys that's the problem. I want to make this a dynamic function to save all currently declared Global variables. IsDeclared helps a bit but it seems I have to do a bunch of statements in the function - it's not pretty and I'd rather not have to trust other coders to make sure they add a list of all of their Global variables in my sample code below:

Local $pairs = ""

if isDeclared("varA") Then $pairs = $pairs & "varA=" & $varA & @LF

.... hundreds more if lines here...

IniWriteSection($fileName, $sectionName, $pairs)

I'd love to use an array for $pairs but I've looked and can't find a function that would extract the list of key/value pairs of Global variables from the in progress script.

Link to comment
Share on other sites

I don't get this at all. The code you posted is strange.Does the variable $pairs becoming a multi-line monster in the part where it says .. "hundreds more if lines here." ? You say you would love $pairs to be an array, but it's clearly a string, that's confusing. Why can't you use an array? Then you just would have one variable to worry about right? Also I wonder what is causing the errors.

Edited by czardas
Link to comment
Share on other sites

Why not use the Execute function instead? Your dump could be a list of all existing global variable names and values and when you restart it, you can use Execute to load them back in. Just make sure to format the dump like an .au3 file. And like czardas said, an array is your best bet for this. (Tip: Use the [0] entry to track how many variables you've got.)

Link to comment
Share on other sites

czardas, what errors? I never said I had any errors happening. Yes, indeed, it does become a multiline monster which is what I don't want to have to do. Don't blame me for the confusion for delimited string vs array, as here is the function definition from the documentation for iniWriteSection:

IniWriteSection ( "filename", "section", "data" [, index] )

data The data to write. The data can either be a string or an array. If the data is a string, then each key=value pair must be delimited by @LF. If the data is an array, the array must be 2-dimensional and the second dimension must be 2 elements.

Artisan, I'm not sure how Execute could help since that would still require a messy manually maintained list of the Global variables, just moved into an include file. See above for string vs array argument - I want an array. Going back to my last post's last line... "I can't find a function that would extract the list of key/value pairs of Global variables from the in progress script."

Link to comment
Share on other sites

Perhaps, you could write the Global variables to the ini file after they are used in your script but also checking to see if a value exists in the ini file before declaring them.

Yes you may have a lot of lines of code to add depending on how may Global vars you are using but I am following this thread to see if there really is a native function (or shared UDF) to pull all the declared Global vars into an array.

Link to comment
Share on other sites

MaritimeGirl, correct me if I'm wrong, but you're dealing with an unknown number of global variables that you want to be able to record to file (variable name & value) in the event of error handling. Do you have control over the variable names? If so, then it's a trivial matter to throw it all in a single array instead - you wouldn't even need the variable names then. You would only have to output values of the array, which you could load back in again later. If you have no control over the names of the variables, then I think you're stuck. At least I would be.

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...