MS-DOS supports user input through some of its commands. One of the command is “CHOICE” command to get user option from the given options. The selected option will be returned.
CHOICE is the command which is helpful to display list of options and allows the users to select the option from list of options. Once the option is selected the selected option is copied into ERRORLEVEL environment variable. Depending on the ERRORLEVEL environment variable you can write the statements in your batch programming.
Lets take a simple example. Display 1, 2 or 3 as list of options. When the user selects any of these options, display the selected option in command prompt window.
Below is the batch program for this: Save the below code into “sample.bat” and run the code from command prompt.
@echo off CHOICE /c 123 /M "Choose 1, 2 or 3" IF ERRORLEVEL 3 ECHO You have selected option 3 IF NOT ERRORLEVEL 3 IF ERRORLEVEL 2 ECHO You have selected option 2 IF NOT ERRORLEVEL 2 IF ERRORLEVEL 1 ECHO You have selected option 1 @echo on
Here we use CHOICE command to get user inputs. We use /c switch to list the options 1,2 or 3. And we display the text using /M switch.
When the user selects the option, the selected option value will be stored into ERRORLEVEL environment variable. We check this environment variable, and based on this we displayed the relevant text.