How do I combine multiple inputs that have the same output into a single If statement in the batch choice command?

So I've recently been experimenting with the choice command in batch, and I was wondering if there was a way I could have multiple inputs have the same output, without having an if statement for each one. For example, I'd like to have a simple Y/N choice but have Y and y do the same thing, and N and n do the same thing as well. I realize I could just do this:

IF %Errorlevel% == Y goto :choiceY
IF %Errorlevel% == y goto :choiceY
IF %Errorlevel% == N goto :choiceN
IF %Errorlevel% == n goto :choiceN

However, I was wondering if there was a way to simply combine both inputs into a single If statement, such as:

IF %Errorlevel% == Yy goto :choiceY
IF %Errorlevel% == Nn goto :choiceN

So, is there a way? Thank you in advance for any/all help.

Edit: Well I just figured out that the choice command doesnt seem to like lowercase letters anyway, as even when I put all 4 IF statements, as well as /C YyNn, it always seems to go straight to the default error level, without allowing for an input. Since that might be confusing, heres the Choice command im using:

CHOICE /n /T 15 /C YyNn /M "Please type Y to continue, or N to cancel.  In 15 seconds, the program will continue automatically." /D Y

Basically, it keeps going straight to the result for Y, without any chance for an input. Does lowercase just not work, or is there something wrong with my choice command?

To make responses easier, here is all the code in the batch file:

@echo off
:begin
echo.
echo.
echo This is a test file for batch coding.
echo.
echo.
pause
goto :choice

:choice
cls
CHOICE /n /T 15 /C YyNn /M "Please type Y to continue, or N to cancel.  In 15 seconds, the program will continue automatically." /D Y
IF %Errorlevel% == Y goto :start
IF %Errorlevel% == y goto :start
IF %Errorlevel% == N goto :end
IF %Errorlevel% == n goto :end

:start
cls
echo.
echo Your choice was Y.
timeout 10>nul
exit

:end
cls
echo.
echo Your choice was N.
timeout 10>nul
exit

1 answer

  • answered 2020-07-05 14:51 Compo

    Just FYI, this should perform the same functionality as your posted code.

    @Echo Off
    SetLocal EnableExtensions
    Echo(
    Echo(
    Echo This is a test file for batch coding.
    Echo(
    Echo(
    "%__AppDir__%timeout.exe" -1
    ClS
    Echo Please type Y to continue, or N to cancel.
    "%__AppDir__%choice.exe" /C YN /N /T 15 /D Y /M ^
     "In 15 seconds, the program will continue automatically."
    If Errorlevel 2 (ClS
        Echo(
        Echo Your choice was to cancel.
        "%__AppDir__%timeout.exe" 10 >NUL
        GoTo :EOF)
    ClS
    Echo(
    Echo You chose to continue.
    "%__AppDir__%timeout.exe" 10 >NUL
    

    Please note, if you know the code will only be used in a locale/language where Y and N are the default responses, you can omit the /C YN part. I have included them here because you used Y and N in the printed text, and you specified Y as the default, (that would be no good if [J,N]? were the locale/language defaults.).