1

I have a program I want to execute with a line like this:

gams.exe program.gms user-defined-variable1 user-defined-variable2 ..........

Currently I use a batch file which prompts the user for input strings which are the user-defined variables, for example:

  • code:

    set /P scen_num="(What scenario number would you like to simulate?) "
    
  • output:

    (What scenario number would you like to simulate?)

  • the user types: 1

I use this batch file very often and there are multiple parameters. I would like to write the most-recent parameters to a text file, so that the next time I run the batch (or Powershell) file, the previously-used setting will be there on the prompt:

(What scenario number would you like to simulate?) 1

So I can spam ENTER if I want to execute exactly the same thing as the previous time.

Ideally I want to be able to do this in batch, though Powershell is acceptable if batch cannot do this.

  1. How do I output to / read from a text file?
  2. How do I make the prompt already contain a specified value before I type anything?
wOxxOm
  • 65,848
  • 11
  • 132
  • 136
Anton
  • 1,458
  • 1
  • 14
  • 28
  • Can we close this as a duplicate of [Batch file include external file for variables](http://stackoverflow.com/q/2763875) (1) or [Is there anyway to have preset data for user input in a batch file?](http://stackoverflow.com/q/23549048) (2) ? – wOxxOm Oct 27 '15 at 16:34
  • You might also be interested in [ini.bat](http://stackoverflow.com/a/15413717/1683264). – rojo Oct 27 '15 at 17:02

1 Answers1

2

Another solution would be making a config batch, to save variables and call to set. You could just add something like this after all the variables are set.

Program.bat

:[ First variable acts to wipe file with single `>` ]
echo set var1=%var1% > config.bat
echo set var2=%var2% >> config.bat
echo set var3=%var3% >> config.bat
:[ etc ]

This will create a batch like so:

config.bat

:[ setting variables to random data for examples sake.
set var1=Hello
set var2=I am
set var3=text.

You can add the option to either manually set your variables, or just

call config.bat

And if you wish to have multiple default settings, you can just add these as seperate batch scripts, by adjusting Program.bat to something like:

Program2.bat

echo set var1=%var1% > %name%.bat
echo set var2=%var2% >> %name%.bat
echo set var3=%var3% >> %name%.bat

Setting %name% to whatever you wish if you opt to save your settings into a batch, and then just replace

call config.bat
:[ with ]
call %name%.bat

You did mention the ability to just tap enter for a variable you wanted to set default, that could be done with something like:

:[ Have the variables saved in config.bat set to retrievable variables ]
call config.bat
set 1=%var1%
set 2=%var2%
set 3=%var3%

:[ Set the variables, if `enter` pressed set to retrievable variable ]

echo Enter var1
set /p var1=
 if not defined var1 (
   set var1=%1%
)

echo Enter var2
set /p var2=
 if not defined var2 (
   set var2=%2%
)

echo Enter var3
set /p var3=
 if not defined var3 (
   set var3=%3%
)
Bloodied
  • 1,004
  • 7
  • 20
  • What is `set 1=var1` and how is this answer better than the possible duplicates listed in my comment? Unless your answer is better than the already existing ones, there's no need to provide duplicate answers for a question of the user who didn't search for existing solutions. – wOxxOm Oct 27 '15 at 16:49
  • Thank you. This was useful for me. I did search for existing solutions but not in their component parts. It did not occur to me to echo an existing variable from the config file to the user and combine it with a default value if user does not define the value, for example. – Anton Oct 27 '15 at 17:21
  • `Set 1=%var1` is used to counteract the `set /p var1` so that a variable is not marked empty, and not all are set at once. I was unsure of the conditions he wanted set, so the intention of my answer was to provide a simple quick method to set a variable to a default value if not defined. – Bloodied Oct 27 '15 at 17:57
  • Nevermind wOxxOm, i see what you meant. i fixxed and added `%` around the variables. – Bloodied Oct 28 '15 at 14:13