How to use choice and set in a batch file

Updated: 09/03/2019 by Computer Hope
Batch file

Below is how to use the choice and set command in a batch file to give users a list of selectable options.

How to use set in a batch file

Below is how you can use the set command to give batch file users the ability to press 1, 2, or 3 and perform the steps for the option pressed.

@ECHO off
cls
:start
ECHO.
ECHO 1. Print Hello
ECHO 2. Print Bye
ECHO 3. Print Test
set choice=
set /p choice=Type the number to print text.
if not '%choice%'=='' set choice=%choice:~0,1%
if '%choice%'=='1' goto hello
if '%choice%'=='2' goto bye
if '%choice%'=='3' goto test
ECHO "%choice%" is not valid, try again
ECHO.
goto start
:hello
ECHO HELLO
goto end
:bye
ECHO BYE
goto end
:test
ECHO TEST
goto end
:end
pause

In the above batch file, the %choice% variable is assigned when the user enters data and presses enter with the set /p choice line. If 1, 2, or 3 is entered, goto goes to the corresponding label and performs the echo and goes to the end of the batch file.

  • See our set command page for further information and options.

How to use choice in a batch file

Below is how to use the choice command to give three options and perform a specific step.

@ECHO OFF
:BEGIN
CLS
CHOICE /N /C:123 /M "PICK A NUMBER (1, 2, or 3)"%1 IF ERRORLEVEL ==3 GOTO THREE
IF ERRORLEVEL ==2 GOTO TWO
IF ERRORLEVEL ==1 GOTO ONE
GOTO END
:THREE
ECHO YOU HAVE PRESSED THREE
GOTO END
:TWO
ECHO YOU HAVE PRESSED TWO
GOTO END
:ONE
ECHO YOU HAVE PRESSED ONE
:END
pause

In the batch file above, choice has the 1, 2, or 3 option. If any of these are pressed, it goes to the label with goto and echoes the number pressed.