1

So, I am asking the user to make an selection. The point of this question is to catch if the user hit just entered without any input. My plan is to find out if the user entered nothing and just hit entered. If so, then display that it is not a valid option and go back to :START. The following code does not work - especially the empty input and the input with just space character. If the user did this invalid thing then I just want to display the message saying it is empty. Any suggestions on how to handle this issue would be greatly appreciate.

@echo off

:START
cls
echo  Choose your option & echo.
echo     [P] play
echo     [R] rules
echo     [M] main menu
echo     [E] exit
set /p "cho=->"
if %cho%==e (goto EXIT
) else if /I %cho%==m (goto MAINMENU
) else if /I %cho%==r (goto GAMERULES
) else if /I %cho%==p (goto GAMERULES
) else if %cho%=="" (goto EMPTYINPUT
) else if %cho%==" " (goto EMPTYINPUT
) else cls
echo That is not valid
pause

:EMPTYINPUT
echo  This is not a valid option.
pause
goto START
  • or [How can I trap an ENTER keystroke in batch?](http://stackoverflow.com/questions/30824695/how-can-i-trap-an-enter-keystroke-in-batch#comment49695311_30824695) – Gary Jun 20 '15 at 23:24

2 Answers2

2

You may avoid all the option identification problems if you use choice instead set /P. Choice command will respond just to a limited set of keys defined by you, so its answer is always correct.

@echo off

:START
cls
echo  Choose your option & echo.
echo     [P] play
echo     [R] rules
echo     [M] main menu
echo     [E] exit
choice /C PRME /N /M "->"
goto option-%errorlevel%


:option-1  PLAY
echo In play
pause
goto start

:option-2  GAMERULES
echo In game rules
pause
goto start

:option-3  MAINMENU
echo In main menu
pause
goto start

:option-4  EXIT
echo Bye...
goto :EOF

For further details, type: choice /?

Aacini
  • 65,180
  • 12
  • 72
  • 108
1

You can also try it with a dynamic menu like this :

@echo off
Mode con cols=90 lines=15
:menuLOOP
Mode con cols=70 lines=15
Cls & color 0B
Title  Example of Dynamic Menu
echo(
echo(       ============================Menu===========================
echo(
for /f "tokens=2* delims=_ " %%A in ('"findstr /b /c:":menu_" "%~f0""') do echo(              %%A  %%B
echo(
echo(       ============================================================
set choice=
echo( & set /p choice=Make a choice or hit ENTER to quit: || GOTO :EOF
echo( & call :menu_[%choice%]
GOTO:menuLOOP

:menu_[P] PLAY
cls & Color 0A
echo.
echo In play
pause
GOTO:menuLOOP

:menu_[G] GAMERULES
cls & Color 0C
echo.
echo In game rules
pause
GOTO:menuLOOP

:menu_[M]  MAINMENU
cls & Color 0D
echo.
echo In main menu
pause
GOTO:menuLOOP

:menu_[E]  EXIT
echo Bye...
Exit
Hackoo
  • 18,337
  • 3
  • 40
  • 70