How to use choice and set in a batch file
Updated: 09/03/2019 by Computer Hope
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.
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.
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
ECHO YOU HAVE PRESSED THREE
ECHO YOU HAVE PRESSED TWO
ECHO YOU HAVE PRESSED ONE
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.