I'm creating a script that I want to be dual-purpose:
The script will use user-input options to execute commands to change some settings. This part is nearly done.
The script is collecting the user's choices into local environment variables via SETLOCAL
/ENDLOCAL
and SET /P
, and calling the variables later in the script for command execution.
After collecting the user options, there is a verification step that displays those options and asks the user for confirmation before proceeding to command execution.
The script will give the user an option to automatically create a new script with their options built-in for future convenience.
Here's my current plan concept to achieve objective #2, though I'm not exactly sure how to write it:
Build a template script that can be copied to start off creation of the customized scripts.
At the very start of the template script, SETLOCAL
will isolate environment variables created by the finalized script so they do not persist after the script is finished running.
Immediately after SETLOCAL
will be a GOTO
command which points to a label at the very end of the script. The label will be the start of the variable definitions section in completed scripts.
After the GOTO
command will be a label called STARTEXEC that will be jumped to after variable definitions are complete.
Just before the variables section label will be a labeled section with some command(s) to exit the script.
Into the current script:
CHOICE
during the verification step that asks the user if they'd like to create a static script. If yes, prompt the user for a filename via SET /P
then include the script building commands in this script's execution flow via GOTO
.Use some commands similar to these, to build the static script's variables section:
COPY ScriptTemplate.bat %UserFileName%.bat
ECHO SET VAR1=%UserVar1% >> %UserFileName%.bat
ECHO SET VAR2=%UserVar2% >> %UserFileName%.bat
Add one more line to make sure the static script jumps to command execution.
ECHO GOTO STARTEXEC >> %UserFileName%.bat
My questions here:
Are my methods fairly sound, or is there a better way to do this, still via batch script?
What should I use to exit the static script, before it comes back around to the variables section? I know I'll need an ENDLOCAL
. Will EXIT
work here, or is there something else more appropriate for this?
The template idea seems to be the easy method (not sure about short, but this is probably how I would've approached it.) Create a separate template based on the original, removing all of the SET /P commands.
EDIT:
Removed the old answer.
At the end of the template, write:
GOTO:EOF
:User_Defined_Variables
At the beginning of the file, write:
CALL:User_Defined_Variables
In your original script, after the user confirms creating a custom script, make a copy of the template file COPY /y template.bat userscript.bat
send all of the user's variables to it.
(ECHO var1 = %uservar1%
ECHO var2 = %uservar2%
Echo var3 = %uservar4%)>> userscript.bat
GOTO:EOF
So when the user runs the script, the CALL:User_Defined_Variables will set the user's script parameters and continue on with the rest of the script.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With