0

I have this

@echo off
:1
cls
echo Navigation: W-Up, S-Down, E-Enter
echo _________________________________
echo -(Option 1)
echo   Option 2
echo   Option 3
choice /c WSE /n
if ERRORLEVEL 1 goto 3
if ERRORLEVEL 2 goto 2
if ERRORLEVEL 3 goto opt1
:2
cls
echo Navigation: W-Up, S-Down, E-Enter
echo _________________________________
echo   Option 1
echo -(Option 2)
echo   Option 3
choice /c WSE /n
if ERRORLEVEL 1 goto 1
if ERRORLEVEL 2 goto 2
if ERRORLEVEL 3 goto opt2
:3
cls
echo Navigation: W-Up, S-Down, E-Enter
echo _________________________________
echo   Option 1
echo   Option 2
echo -(Option 3)
choice /c WSE /n
if ERRORLEVEL 1 goto 2
if ERRORLEVEL 2 goto 1
if ERRORLEVEL 3 goto opt3
:opt1
cls
echo You chose Option 1
pause >nul
exit
:opt2
cls
echo You chose Option 2
pause >nul
exit
:opt3
cls
echo You chose Option 3
pause >nul
exit

What it is supposed to do is look like a selection menu, but for some reason it just constantly loops through ":1" from lines 2 to 9 it just loops over and over again, why is it doing this? How do I make it not do this?

dan1st
  • 12,568
  • 8
  • 34
  • 67
  • You have to reverse your if errorlevel, starting with the highest number. In fact `if ERRORLEVEL 1 goto 1` is interpreted as `if ERRORLEVEL is 1 or greater goto 1` so the other IFs aren't reached at all. The alternative is to compare `if %errorlevel%==1 ...` available with EnableExtensions (default) –  Aug 22 '17 at 22:47
  • Reading `help if` / `If /?` or [ss64.com/nt/if.html](http://ss64.com/nt/if.html) helps –  Aug 22 '17 at 22:50

1 Answers1

0

Read choice /?.


According to that help page, errorlevel(s) are supposed to be checked in reversed order.

choice /c WSE /n
if ERRORLEVEL 3 goto opt3
if ERRORLEVEL 2 goto 2
if ERRORLEVEL 1 goto 1

Why? Because if ERRORLEVEL 1 means if %errorlevel% geq 1. Alternatively, you can do:

if %errorlevel% equ 1 goto 1
if %errorlevel% equ 2 goto 2
if %errorlevel% equ 3 goto opt3

Or even crazier(but safer) with findstr.

set errlvl=%errorlevel:~-1%
echo "%errlvl%"| findstr /l /x /i """1""" && goto :1
echo "%errlvl%"| findstr /l /x /i """2""" && goto :2
echo "%errlvl%"| findstr /l /x /i """3""" && goto :opt3

We do not really need that set statement, but a precaution


Just another reminder, if the user pressed key(s) out of the "WSE", an annoyingly loud beep will occur. You may want to check out my poorly title question to find out methods of silencing them.