batch. if/else statements. wanting to go back if say user doesnt input and only presses enter [duplicate]

If a user were to press ENTER, then the else portion of the statement is ignored and the process ends and closes.

:options
cls
echo. 
echo What would you like to do?
echo. 
pause\>nul

echo 1) Main Menu
echo 2) Controls
echo 3) Introduction 
echo 4) exit 

set "choice="
set/p choice=choice: 
pause \>nul

if %choice%== 1 goto mainmenu 
if %choice%== 2 goto Controls
if %choice%== 3 goto introduction
if %choice%== 4 goto eof1
if not defined "%choice%"=="" ( 
    
) else (
    
) goto options

I would prefer not to use the choice command.

  • Please consider choice which is designed for this task. Use the search facility for [batch-file] choice eg. Gerhard’s example or see the documentation – choice /? from the prompt.. With your code, if not defined "%choice%"=="" ( is completely wrong – if not defined choice ( would take the action if the response was [enter]. if "%choice%"=="" ( would do the same.

    – 

  • Do not use set /P and an environment variable with name choice. Use the Windows command CHOICE which is designed for a choice menu as described in my answer in full details on the referenced duplicate question.

    – 

A Quick and short answer is

if “%choice%” == ” ” call :options

Just a question and suggestion about the options below

:options
cls
echo.
echo What would you like to do?
echo.
pause>nul <————— why use pause?

when set /p does the same ? for a better look try using TITLE to output “what would you like to do” which cleans up the menu and places the question in the border title bar.

like:

TITLE=”What would you like to do ?”
also put your set here

set choice= (to clear the last choice )

echo 1) Main Menu

echo 2) Controls

echo 3) Introduction

echo 4) exit

set /p choice=choice:

if %choice% == 1 goto mainmenu

if %choice% == 2 goto Controls

if %choice% == 3 goto introduction

if %choice% == 4 goto eof1

if not defined “%choice%” (
call :options
) else (
exit /b
)

Leave a Comment