duper
duper

Reputation: 243

Run/Execute statements only on a chosen menu option in Windows Batch script

I have this menu in batch script:

ECHO *1* - A
ECHO *2* - B
ECHO *3* - C
ECHO *4* - D
ECHO *5* - runAll
ECHO *6* - Quit

Right now, what I do with it is if the user enter 1, then it runs the 1st one and loops back to the menu, 2 for the 2nd, etc.
If the user enters 5 then my script goes and runs 1 through 4 step by step in order.

My question is: "Is it possible to run/execute certain ones that you choose to execute?"
Let's say I want to execute 1 & 4 and loop back to the main menu. I enter "1(space)4" and the script takes those arguments/parameters and goes and finds the corresponding option and executes it. Does it make sense? Is it possible?

I do have the option of running each one of those options separately, but this idea, the one I'm talking about, makes it more efficient and faster.

Upvotes: 1

Views: 152

Answers (1)

npocmaka
npocmaka

Reputation: 57252

:begin
ECHO *1* - A
ECHO *2* - B
ECHO *3* - C
ECHO *4* - D
ECHO *5* - runAll
ECHO *6* - Quit

set /p CH=choose
for /f  "tokens=* delims= " %%A in ("%CH%") DO (
    if *%%A* EQU *1* call A
    ....
    if *%%A* EQU *4* call D
    if *%%A* EQU *5* call :callAll
    if *%%A* EQU *6* exit
)
goto :begin

:callAll
    call A
    ...
    call D
goto :eof

edit:

:begin
ECHO *1* - A
ECHO *2* - B
ECHO *3* - C
ECHO *4* - D
ECHO *5* - runAll
ECHO *6* - Quit

set /p CH=choose
echo nul >temp.bat
set line=title "temp script"
for /f  "tokens=* delims=;, " %%A in ("%CH%") DO (
    if *%%A* EQU *1* set line=%line% &call A 
    ....
    if *%%A* EQU *4* set line=%line% &call D 
    if *%%A* EQU *5* call :callAll
    if *%%A* EQU *6* exit
)
echo %line% >>temp.bat
call temp.bat
del /s /q /f temp.bat
goto :begin

:callAll
    line=%line% &call A 
    ...
    line=%line% &call D 
goto :eof

this will create (and delete after execution) a temp script which will be executed after entered options.You can create self edited script also but the code will be more hard for maintenance . the for loop process the data entered by the "customer" and executes all numbers separated by space.in "delims=" you can add commas, semicolon and what you want.

Upvotes: 3

Related Questions