Jump to content

Running entries/variables in a file as commands


 Share

Recommended Posts

This seems like it should be simple, but I can't seem to figure it out.

Let's say I have multiple configuration files with varying values for common variables. For example, File_A might look like:

g_Variable1 = 100
g_Variable2 = 50

While File_B looks like:

g_Variable1 = 75
g_Variable2 = 75

To summarize this portion, multiple files all contain the exact same variables, but each one has different values.

What I want to do is read a file, line by line, and turn each line into a command as though it were written within the script itself.

For example, my program would read line 1 of FILE_A and see: g_Variable1 = 100

It would then process that as a command, altering the value of the Global Variable g_Variable1 (as contained and used within the script itself) so that it equals 100.

I realize I could read a file, process and explode each line, check as to which variable is being addressed, and then modify the value of that variable within the script, but it seems needlessly long.

Psuedocode that I want to make possible:

FileOpen($File_A)

While 1
Local $Line = FileReadLine($File_A)
If @error = -1 Then ExitLoop
$Line
WEnd
Link to comment
Share on other sites

You'd probably want to read the line, StringSplit it on the "=" and Assign the value on the right to the variable name on the left, although I'm not sure how that affects Global variables with the same name. Personally, I think you shouldn't do it this way because it's WAY over complicating things. Turn the file into an INI file, and use the INI functions to read the entries.

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

You'd probably want to read the line, StringSplit it on the "=" and Assign the value on the right to the variable name on the left, although I'm not sure how that affects Global variables with the same name. Personally, I think you shouldn't do it this way because it's WAY over complicating things. Turn the file into an INI file, and use the INI functions to read the entries.

I was hoping this would be a way to simplify things, but perhaps I was mistaken. I have about 150 variables that need to be set (and 16 different configurations of those variables, meaning a possible 2400 assignments), and would much rather loop/parse through a file, assigning each variable line by line, rather than having 150 variable assignments within my script itself. The overall idea was to take those 150 variables and move them outside of the code, keeping the script file itself much more compact (just a personal preference).

With an ini file, my script would look like:

var1 = iniread(get line/var 1 value)
var2 = iniread(get line/var 2 value)
...
var150 = iniread(get line/var 150 value)

As opposed to 5 lines of Loop code. It's not really a functionality thing, more like a personal 'keep the code concise because I'm ocd" thing.

Edited by JSands
Link to comment
Share on other sites

You can always turn it into an Include if these values are consistent throughout the script and not changed by this method on the fly.

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

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