This question already has an answer here:
- Multiple choices menu on batch file? 13 answers
I want to create a windows batch scripting whereby it allows the user to enter multiple choices at one go, and then after that the program will runs.
With reference to this website (Multiple choices menu on batch file?), I get to know it somehow works to allow multiple selection. However, this is in bash scripting. For example...
@echo off
setlocal enabledelayedexpansion
echo Which would you like to use?
echo 1. Hello.txt
echo 2. Byebye.txt
echo 3. ThisIsText.txt
echo 4. MyBatchScript.txt
echo 5. All
set /p op=Type the numbers of the names you want to use (separated by commas with no spaces. E.g: 1,3,2):
Until here, it works by prompting users to select one or more choices.
for /f "delims=, tokens=1-5" %%i in ("op") do (
set i=%%i
set j=%%j
set k=%%k
set l=%%l
set m=%%m
)
However, until here, I realised that the choices would be stored into a variable "op" and this would then be in i
. And basically, j
, k
, l
and m
are not used. I'm not sure if I interpreted it wrongly. Hopefully I did not interpret the coding wrongly.
So for what I want is that...
When the user selects only 1 options, It will insert the "Hello.txt" into a command (e.g.)
echo This is complicated > Hello.txt
But if the user selects more than 1 options (for example, user typed 1,2), then it will insert
echo This is complicated > Hello.txt
echo This is complicated > Byebye.txt
And if the user selects option '5', it cannot be entered along with other numbers (since it is ALL). Then it will echo the This is complicated > Byebye.txt
, Hello.txt
, etc
Is there anyway to do it using batch scripting?
Edit: Can anyone explain this to me? I tried finding different websites but I still don't get it. Sorry, I am new to writing batch scripts. So the understanding of it is still not deep. Disclaimer: This is the coding I got from the website I mentioned above.
if %i%X neq X set last=1b & goto %i%
:1b
if %j%X neq X set last=2b & goto %j%
:2b
if %k%X neq X set last=3b & goto %k%
:3b
if %l%X neq X set last=4b & goto %l%
:4b
if %m%X neq X set last=%m% & goto %m%
goto next
:1
::Put the code for doing the first option here
goto %last%
:2
::Put the code for doing the second option here
goto %last%
:3
::Put the code for doing the third option here
goto %last%
:4
::Put the code for doing the fourth option here
goto %last%
:5
::Put the code for doing the fifth option here
goto %last%
I do not get how this helps to run multiple command. If I input 1,2,3
into the field, how does it gets me to part where I can run it all together?
You may make good use of the fact that the standard separators for items in flat FOR command (no /F option) are spaces, commas, semicolons and equal-signs:
You wrote, you tried
('%op'), ("%op"), (%op)
and variations.It should be:
("%op%")
Only
for
variables and Commandline-Parameters use<Percent><char>
syntax.op
is a "normal" variable and it is used<Percent><name><Percent>
:%op%