0

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?

Community
  • 1
  • 1
Linify
  • 227
  • 4
  • 13
  • j, k, l, and m are in fact used. The `for` loop separates the comma-separated items entered (see the `delims=, tokens=1-5") into up to 5 values (i, j, k, l, and m). Try running it, and then echoing %i, %j, %k, %l, and %m. – Ken White Jul 17 '14 at 17:26
  • @KenWhite sorry to say. I don't know why but it just doesn't have any value in it. i have tried echoing it. I also tried `@echo on` so that i can view how it works. but then after you insert the numbers. next would be `set i=op` and the rest would be like `set j=` – Linify Jul 17 '14 at 17:33
  • Yeah, in your version it's because "op" isn't being expanded (no %). However, I can't get it to work correctly using `('%op')`, `("%op")`, `(%op)`, or any variation thereof. – Ken White Jul 17 '14 at 17:46
  • @KenWhite Is there other ways to get the result I want? This coding is from the website I gave in the question. I just used it as reference. But I do not really understand and know how to implement it. Do you have any idea how? – Linify Jul 17 '14 at 17:48
  • No, sorry. (If I had, I would have posted an answer.) – Ken White Jul 17 '14 at 17:50
  • @KenWhite oh okay~ :) – Linify Jul 17 '14 at 17:53

2 Answers2

0

You wrote, you tried ('%op'), ("%op"), (%op) and variations.

It should be: ("%op%")

Only forvariables and Commandline-Parameters use <Percent><char> syntax.

op is a "normal" variable and it is used <Percent><name><Percent>: %op%

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Hi Stephan, it works now for the front part but the back part I still don't really understand the logic. Do you mind elaborating? Sorry to trouble you. – Linify Jul 17 '14 at 19:47
  • `if %i%X neq X `: "if %i% is empty". `set last=1b & goto %i%` : "remember where I am and goto Label". `goto %last%`: "go back to the remembered position". Not a very common way to do this. `call` (as Aacini showed) would be much better here. – Stephan Jul 18 '14 at 16:25
0

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:

@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

:getOptions
set /p "op=Type the numbers of the names you want to use (separated by commas OR spaces): "
if "%op%" equ "" goto getOptions

if %op% equ 5 set op=1,2,3,4
for %%a in (%op%) do (
   echo Process option %%a
   call :option-%%a
)
goto :EOF

:option-1
echo 1. Hello.txt
exit /B

:option-2
echo 2. Byebye.txt
exit /B

:option-3
echo 3. ThisIsText.txt
exit /B

:option-4
echo 4. MyBatchScript.txt
exit /B
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • Do you know how to check for parameters? Because option5 is for all. So if the user type "1,2,3,4,5" or "1,5" or "5,1" or "1,2,5,3", it will prompt the user to enter valid options – Linify Jul 17 '14 at 22:46
  • Hi Aacini, do you know how to change the coding to make it like if the user insert "1,2,3" then option 1,2,3 would run? – Linify Jul 18 '14 at 00:57