problem with choice command

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
gunitinug
Posts: 9
Joined: 09 Nov 2017 20:04

problem with choice command

#1 Post by gunitinug » 09 Nov 2017 20:06

Code: Select all

@echo off
CHOICE /C YNO /T 10 /D Y /M "Are you feeling awesome?"
IF ERRORLEVEL 1 goto yes
IF ERRORLEVEL 2 goto no
IF ERRORLEVEL 3 goto other

:yes
echo you are awesome!
goto end

:no
echo bad luck.
goto end

:other
echo unique ;)
goto end

:end
For every choice returns "you are awesome!". I'm expecting different responses with each choice. THX

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: problem with choice command

#2 Post by Squashman » 09 Nov 2017 20:26

From the help file for the IF command.

Code: Select all

ERRORLEVEL number Specifies a true condition if the last program run
                    returned an exit code equal to or greater than the number
                    specified.
And from the help for the CHOICE command.

Code: Select all

When you use ERRORLEVEL parameters in a batch program, list
   them in decreasing order.
In other words. Reverse your IF statements.

Your other option is to use the %errorlevel% variable.

Code: Select all

IF "%errorlevel%"=="1" goto yes

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: problem with choice command

#3 Post by Aacini » 10 Nov 2017 08:14

... or completely eliminate all those IF commands! 8)

Code: Select all

@echo off
CHOICE /C YNO /T 10 /D Y /M "Are you feeling awesome?"
goto option-%errorlevel%

:option-1 yes
echo you are awesome!
goto end

:option-2 no
echo bad luck.
goto end

:option-3 other
echo unique ;)
goto end

:end
Antonio

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: problem with choice command

#4 Post by penpen » 12 Nov 2017 05:09

You may want to cover the possibility of CTRL+C as a choice-input:

Code: Select all

::...

:option-0 CTRL+C
echo aborted.
goto end

:option-1 yes
: ...
penpen

Post Reply