You are here: > >

How to use choice and set in a batch file

Batch fileBelow is how you can use the choice and set command in a batch file to give your batch file the users ability to select from a list of options.

How to use set
How to use choice

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 by the user using the goto command 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 an example of how you can use the choice command to give batch file users the ability to press 1, 2, or 3 and perform the steps for the option pressed.

@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 above batch file, the choice has the available options of 1, 2, or 3 and if any of these is pressed goes to the appropriate label with goto and echoes the number pressed.

Additional information