0

I tried to convey my intentions in the title, but basically this is "part" of my batch command:

set /p "choice=Enter an option: "
if %choice% == 1 GOTO Redo
if %choice% == 2 GOTO Remove
if %choice% == 3 GOTO Notice
if %choice% == 4 GOTO Cancel
if %choice% == 5 GOTO Exit

And any input that doesn't contain 1, 2, 3, 4, or 5 goto :error

I will also need the same function for inputs that aren't numbers, so using EQU, NEQ, GTR, LSS, etc. won't work for:

set /p "Option=Would you like to do ..., y/n?: "
if %Option% == n GOTO MainMenu
if %Option% == y GOTO Reroute

If not n or y, goto error.

I tested with if %errorlevel% NEQ 0 goto :error among others, to no avail.

What ends up happening is that it would just skip the next section of the batch, rather than going to :error

Thanks in advance for the help.

  • [Could it be that you are searching for this?](http://stackoverflow.com/questions/18423443/switch-statement-equivalent-in-windows-batch-file) – Thehx Sep 10 '15 at 03:28
  • I can use something like this `IF %UserInput% GTR 2 goto :error` I could get something working for number choices, but how could get that to work for non-number inputs. like "y" or "n" – Mathieu Beaulieu Sep 10 '15 at 05:13

4 Answers4

0

After searching around a bit more and educating myself further on the subject, I found out that you could use multiple "if" statements in a single command line.

So for:

set /p "Option=Would you like to do ..., yes or no?: "
if %Option%==no GOTO MainMenu
if %Option%==yes GOTO Reroute

I used:

if not %Option%==no if not %Option%==yes GOTO :Error2

It worked like a charm. I've still got a ways to go, but I learned something. Thanks for pointing me in the right direction.

0

You can use "choice" than "if", like this:

choice /C YN /N /M "Would you like to do ..., y/n?: "
set ERR=%ERRORLEVEL%
if %ERR%== 1 goto Reroute
if %ERR%== 2 goto MainMenu
IZB
  • 151
  • 5
0

You don't have to check, any input that doesn't contain 1, 2, 3, 4, or 5, as all valid choices are already handled and will never hit the REM line

set /p "choice=Enter an option: "
if %choice% == 1 GOTO Redo
if %choice% == 2 GOTO Remove
if %choice% == 3 GOTO Notice
if %choice% == 4 GOTO Cancel
if %choice% == 5 GOTO Exit
REM any other value for %choice%:
GOTO Error

Of Course this works only with GOTO, but not with CALL

Like IZB, I would prefer the choice command, because it won't let you choose invalid Inputs.

Stephan
  • 53,940
  • 10
  • 58
  • 91
0

I am afraid there is a small confusion here. It seems that you did not conveyed your intentions, nor in the title neither in the example.

  • If you want to choose an option in order to execute a certain part, the simpler solution is to use choice command as IZB indicated:

.

choice /C RDNCE /M "Redo Delete Notice Cancel Exit: "
goto option-%errorlevel%

:option-1 Redo
...

:option-2 Delete
...

. . .

:option-0 Ctrl-C
:option-5 Exit
goto :EOF
  • However, if you want to know if an input is included in a list of valid options, then you may use this method:

.

setlocal EnableDelayedExpansion

set "options=/Redo/Remove/Notice/Cancel/Exit/"

set /P "choice=Enter an option: "
if "!options:/%choice%/=!" equ "%options%" goto Error2

rem Here we know that the choice is valid:
goto %choice%

If the choice is one of the words in options variable then the replacement modify that value, so the result would be different than the original. If the choice is not included in the list of options, the replacement fail and the value is the same than the original, so goto Error2 in this case.

This method have the advantage that don't matters if there are many options; the test of all of they is achieved in a single simple if command.

Aacini
  • 65,180
  • 12
  • 72
  • 108