1

how do you allow the user to type multiple numbers into batch scripting if prompted?

if it prompted 5 options to the user. And the user wanted to choose 1,2,3 but does not want 4,5, how to do it in batch scripting?

I refered to Allowing multiple choice selections for users using batch file and Multiple choices menu on batch file?

There is relevant questions in stackoverflow but there is no answer to it because the answer is not right to the point.

Any help would be greatly appreciated.

Community
  • 1
  • 1
Steven
  • 107
  • 2
  • 14
  • What's provoking the question (what do you really want to accomplish)? – Bill_Stewart Jul 26 '14 at 15:41
  • Because I also wanted to know the answer. I searched in the internet for the answer but to no avail. Plus I am new to batch scripting too. – Steven Jul 26 '14 at 15:47
  • 1
    Why do the other answers not answer your question? – Bill_Stewart Jul 26 '14 at 15:49
  • because the other answers can only insert one number. and what i wanted is that the answers can insert multiple numbers then from the user input, it will redirect it to the option 1, option 2 , etc.. – Steven Jul 26 '14 at 15:55
  • If the user enters multiple choices, what should the script do (how to express in script logic)? I would advise that it would be helpful to read through the other answers and learn how to walk before trying to run, so to speak. – Bill_Stewart Jul 26 '14 at 15:57
  • I have read the other answers, that's why I said I did not get what i wanted. i saw from one of the answers in http://stackoverflow.com/questions/14529246/multiple-choices-menu-on-batch-file . It assigns every number that the user input into a variable. But then I do not know how to write the condition statement whereby it will direct it to the `:option-1`, `:option-2`, etc. There is a condition statement provided but then it does not work. I tried to modify the condition statement accordingly but to no avail. It's been at least 3 weeks but I wouldn't figure it out. – Steven Jul 26 '14 at 16:02
  • Start small and get basic things to work first. After understanding the basics, you will start to understand why what you want to do isn't as simple as you might think. Mastery of the basics will also help you to ask a better question than "I don't know how to do X." – Bill_Stewart Jul 26 '14 at 16:08
  • @Steven Please show us your code with the menu. There are dozens of ways to implement an answer for a given question, and to get the right answer you need to provide details. – foxidrive Jul 26 '14 at 16:17
  • Actually... I have asked this question in [question i asked](http://stackoverflow.com/questions/24948004/allowing-users-to-choose-multiple-choices-using-batch-file-and-java) but maybe because my question was a bit long that no one actually read what I was asking. So... I wanted to try if it helps if i did not type long. – Steven Jul 26 '14 at 16:28
  • 1
    I have read all of you questions and their answers. They had answers, that exactly met your questions. If they are "not to the point", deleting them and ask with FEWER information won't help. – Stephan Jul 26 '14 at 16:38
  • @Stephan REALLY?! But I tried but it did not work. Oh my. – Steven Jul 26 '14 at 16:40
  • if it "did not work", track down at which line/command there are problems and what it should do and what it does instead. Note any errormessages. – Stephan Jul 26 '14 at 16:47
  • @Stephan so there is this `for %%a in (%choices%) do ( echo Process option %%a call :option-%%a )` and from my understanding, whatever is in %choices% it will call the option. But when i run, and type **1,2**. But it says **2 was unexpected at this time** – Steven Jul 26 '14 at 16:53
  • @Steven You want to have a multiple choice question and be able to enter any of the choices, separated by a comma, right? And then execute the sections by calling the labels. Is that it? – foxidrive Jul 26 '14 at 17:20
  • @foxidrive YES exactly. i tried but it couldn't work. – Steven Jul 26 '14 at 17:23

2 Answers2

2

This answer also refers to this question: How to integrate batch script multiple selections into JAVA GUI?

You might like to add a line at the top to delete family.txt if you want a new file each time.

@echo off
echo.
echo Selection time!
echo.
echo 1. My father is Joe
echo 2. My mother is Audrey
echo 3. My brother is Jerry
echo 4. My elder sister is June
echo 5. My youngest sister is Awy
echo 6. Include All
echo.

:getOptions
set "choices="
set /p "choices=Type your choices without spacing (e.g. 1,2,3): "

if not defined choices ( 
    echo Please enter a valid option
    goto getOptions
    )

for %%a in (%choices%) do if %%a EQU 6 set choices=1,2,3,4,5
for %%i in (%choices%) do call :option-%%i

echo.
echo Done
pause
exit

:option-1
>> Family.txt echo My father is Joe
exit /B

:option-2
>> Family.txt echo My mother is Audrey
exit /B

:option-3
>> Family.txt echo My brother is Jerry
exit /B

:option-4
>> Family.txt echo My elder sister is June
exit /B

:option-5
>> Family.txt echo My youngest sister is Awy
exit /B
Community
  • 1
  • 1
foxidrive
  • 40,353
  • 10
  • 53
  • 68
  • But I still get the `2 was unexpected at this time` error if I typed **1,2** – Steven Jul 26 '14 at 17:41
  • for `for %%i in (%choices%) do call :option-%%i` , it is able to run if only 1 number is inserted. But if **1,2** is inserted, there would be error – Steven Jul 26 '14 at 17:44
  • No, there is no error. Call your batch file `Mychoices.bat` and try it. It works fine here with `1,2,3` or `4,5,6` or `6` – foxidrive Jul 26 '14 at 17:45
1

Like this :

@echo off

set /p "$choix=Enter you(s) Choice (1,2,...): "
for  %%a in (%$choix%) do call:choix%%a
exit/b


:choix1
echo Choice 1
exit/b

:choix2
echo Choice 2
exit/b

:choix3
echo Choice 3
exit/b
SachaDee
  • 9,245
  • 3
  • 23
  • 33