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.
- How to use set
- How to use choice
@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:BEGINCLSCHOICE /N /C:123 /M “PICK A NUMBER (1, 2, or 3)"%1 IF ERRORLEVEL ==3 GOTO THREEIF ERRORLEVEL ==2 GOTO TWOIF ERRORLEVEL ==1 GOTO ONEGOTO END:THREEECHO YOU HAVE PRESSED THREEGOTO END:TWOECHO YOU HAVE PRESSED TWOGOTO END:ONEECHO YOU HAVE PRESSED ONE:ENDpause
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.
- See our choice command page for further information and options.
Related information
- How to make a batch file.
- See the choice our set page for further information about these commands.
- Batch file help.