0

In Windows batch, I'm asking the user whether he wants to use the program's internal default or wants to set his own parameters, but no matter what the user sets as an answer, the program always jumps straight to the main routine using the internal defaults. This is my code:

@echo off
setlocal EnableDelayedExpansion

choice /C:yn /M "Use internal defaults? "
if errorlevel==1 goto yes
if errorlevel==2 goto no

rem use default
:yes 
set "MYNUMBER=5"
goto run

rem let user define another number
:no
set /P MYNUMBER="Please set a number: "
goto run

rem main routine
:run
echo %MYNUMBER%
pause

What am I missing?

And since we're at it: how can I force the program to wait for the user to hit "Enter" after typing his choice? Right now, it starts directly after typing "y" or "n".

4
  • 1
    Using your syntax you have to reverse the order, if errorlevel==1 implies if errorlevel equal 1 or greater. A different thing is using the environment variable %errorlevel% as long the var isn't altered (by another error) you can check for a distinct value. SeeHelp ifor view onlne Commented Apr 1, 2019 at 15:21
  • 1
    if errorlevel==1 is completely wrong syntax. The correct syntax can be read by opening a cmd window and run the command if /? which outputs the help of command IF explaining on first help page the correct syntax if [not] errorlevel number command. There is no string comparison operator ==. And this condition is true if exit code of previous command or application is greater or equal the number. So use first if errorlevel 2 goto no and no more if is needed because next command lines to execute on exit code being not greater or equal 2 are the lines for user choice y. Commented Apr 1, 2019 at 17:27
  • 2
    To await {Enter}, use set /P instead of choice; but then also account for invalid user input... Commented Apr 1, 2019 at 20:39
  • The errorlevel was not obvious to me. Now, using set /P USER_CHOICE="Use defaults? (Y/N)" and then checking it using if does the job. Commented Apr 2, 2019 at 6:06

1 Answer 1

1

Thanks to the commentators, that helped a lot! Now its working like this:

@echo off
setlocal EnableDelayedExpansion

:ask
set /P USER_CHOICE="Use internal defaults? (Y/N) "

if /i "%USER_CHOICE%"=="y" (
    goto yes
    ) else (
    if /i "%USER_CHOICE%"=="n" (
        goto no
        ) else (
        echo "Wrong input! Please choose Y or N!"
        goto ask
    )
)

rem use default
:yes 
set "MYNUMBER=5"
goto run

rem let user define another number
:no
set /P MYNUMBER="Please set a number: "
goto run

rem main routine
:run
echo %MYNUMBER%
pause
Sign up to request clarification or add additional context in comments.

2 Comments

why not set USER_CHOICE=5, set /p USER_CHOICE=Please set a number (empty for default [%USER_CHOICE%])
For that very basic example, this would work as well, for sure. But in my real case, I have multiple values that can be set by the user or he can just leave them at their defaults, that's why I chose this way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.