0

I suppose you could do:

@echo off
echo 1 - Start
echo 2 - Stop
choice /c 12 /n
if %errorlevel% == 1 (set /a newvariable=1)
if %errorlevel% == 2 (set /a newvariable=2)
if %newvariable% == 1 (
     ::execute code
)
if %newvariable% == 2 (
     ::execute code
)

But is there an simpler, more compact way of doing this?

Also incase you're wonering why I want to do this is because I have the code resembling this:

:function1
if %errorlevel% == 1 (
    if %variable1% GEQ 1 (
        cls
        echo Are you sure?
        echo.
        echo [1] Yes
        echo [2] No
        echo.
        choice /c 12 /n /m "--"
    )
)
if %errorlevel% == 1 (
    if %variable1% GEQ 1 (
        if %errorlevel% == 1 goto function2
        if %errorlevel% == 2 goto function1
    )
    goto function2
)

if %errorlevel% == 2 (
    if %variable1% GEQ 1 (
        cls
        echo Are you sure?
        echo.
        echo [1] Yes
        echo [2] No
        echo.
        choice /c 12 /n /m "--"
    )
)
if %errorlevel% == 2 (
    if %variable1% GEQ 1 (
        if %errorlevel% == 1 goto function3
        if %errorlevel% == 2 goto function1
    )
    goto function3
)

The second time you do choice, it sets the errorlevel to something else, and it won't read the second set of code. So a solution I had was to make it different variables.

  • 2
    you can't change the name of the `errorlevel` variable, but you can copy it into another variable: `set newvariable=%errorlevel%`. Your second code sample confuses me.... – Stephan Dec 02 '20 at 19:53
  • @Stephan Thank you. I'm sorry it was confusing, it's not the full code clearly, that's why there are missing parts. I wanted to clarify why I needed this information but I think it was unnecessary. – Victor Chavez Dec 02 '20 at 19:59
  • 1
    @VictorChavez I recommend to read the lower half of my answer [How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?](https://stackoverflow.com/a/49834019/3074564) Then you get an impression how to evaluate the exit code of `choice` better than done currently by you. You can remove a lot of `if` conditions and so avoid lots of command blocks on using a better structure of the code with `goto Option%Errorlevel% 2>nul || exit /B`. – Mofi Dec 02 '20 at 20:10
  • See also the DosTips forum topic: [ECHO. FAILS to give text or blank line - Instead use ECHO/](https://www.dostips.com/forum/viewtopic.php?f=3&t=774) On using multiple nested choices use, for example, `goto OptionA%Errorlevel% 2>nul || exit /B` for the first `choice`, `goto OptionB%Errorlevel% 2>nul || exit /B` for the second choice, `goto OptionC%Errorlevel% 2>nul || exit /B` for the third choice, or something similar. In other words use batches in a batch file and not nested condition trees. – Mofi Dec 02 '20 at 20:12
  • and judging by your obvious tendency to nest things, you should read about [delayed expansion](https://stackoverflow.com/questions/30282784/variables-are-not-behaving-as-expected/30284028#30284028) – Stephan Dec 02 '20 at 20:19

1 Answers1

0

rather than use errorlevel, you can use a for /f loop over the choice commmand to return the literal keypress in the for metavariable.

 @Echo off
 Setlocal EnableDelayedExpansion
 Set  "o.Q=quit" & Set "o.B=back" & Set "o.N=next"
 Echo/[Q]uit [B]ack [N]ext
rem /* Default prompt must be supressed with /N ; /M switch cannot be used. */
 For /F "delims=" %%G in ('Choice /N /C:qbn')Do Echo/you selected %%G !o.%%G!
T3RR0R
  • 2,747
  • 3
  • 10
  • 25