| CHOICE |
|
| Prompts the user to make a choice in a batch program. Displays a specified |
| prompt and pauses for the user to choose from among a specified set of keys. |
| You can use this command only in batch programs. |
|
| For more information on the ERRORLEVEL parameter, see CHOICE--Note and the |
| IF command. |
|
| Syntax |
|
| CHOICE [/C[:]keys] [/N] [/S] [/T[:]c,nn] [text] |
|
| Parameters |
|
| text |
| Specifies text you want to be displayed before the prompt. Quotation |
| marks are necessary only if you include a switch character (/) as part |
| of the text before the prompt. If you don't specify text, CHOICE |
| displays only a prompt. |
|
| Switches |
|
| /C[:]keys |
| Specifies allowable keys in the prompt. When displayed, the keys will be |
| separated by commas, will appear in brackets ([]), and will be followed |
| by a question mark. If you don't specify the /C switch, CHOICE uses YN |
| as the default. The colon (:) is optional. |
|
| /N |
| Causes CHOICE not to display the prompt. The text before the prompt is |
| still displayed, however. If you specify the /N switch, the specified |
| keys are still valid. |
|
| /S |
| Causes CHOICE to be case sensitive. If the /S switch is not specified, |
| CHOICE will accept either upper or lower case for any of the keys that |
| the user specifies. |
|
| /T[:]c,nn |
| Causes CHOICE to pause for a specified number of seconds before |
| defaulting to a specified key. The values for the /T switch are as |
| follows: |
|
| c |
| Specifies the character to default to after nn seconds. The |
| character must be in the set of choices specified in the /C switch. |
|
| nn |
| Specifies the number of seconds to pause. Acceptable values are from |
| 0 to 99. If 0 is specified, there will be no pause before |
| defaulting. |
|
|
| CHOICE--Note |
|
| ERRORLEVEL parameters |
|
| The first key you assign returns a value of 1, the second a value of 2, the |
| third a value of 3, and so on. If the user presses a key that is not among |
| the keys you assigned, CHOICE sounds a warning beep (that is, it sends a |
| BEL, or 07h, character to the console). |
|
| If CHOICE detects an error condition, it returns an ERRORLEVEL value of 255. |
| If the user presses CTRL+BREAK or CTRL+C, CHOICE returns an ERRORLEVEL value |
| of 0. |
|
| When you use ERRORLEVEL parameters in a batch program, list them in |
| decreasing order. |
|
|
| CHOICE--Examples |
|
| What the user sees when you use CHOICE in a batch file |
|
| If you use the following syntax in a batch file, |
|
| choice /c:ync |
|
| the user sees the following when CHOICE is started: |
|
| [Y,N,C]? |
|
| If you add text to the syntax, |
|
| choice /c:ync Yes, No, or Continue |
|
| the user sees the following when CHOICE is started: |
|
| Yes, No, or Continue [Y,N,C]? |
|
| What the user sees if you leave out a prompt |
|
| If, as in the following example, you use the /N switch to leave out the |
| prompt in a batch program, |
|
| choice /n Yes, No, or Continue? |
|
| the user sees only the text you specified when CHOICE is started: |
|
| Yes, No, or Continue? |
|
| What the user sees if you use the T switch |
|
| If you use the following syntax in a batch program, |
|
| choice /c:ync /t:n,5 |
|
| the user sees the following when CHOICE is started: |
|
| [Y,N,C]? |
|
| If, after 5 seconds, the user hasn't pressed a key, CHOICE chooses N and |
| returns an ERRORLEVEL value of 2. If the user presses a key before 5 |
| seconds, CHOICE returns the value corresponding to the user's choice. |
|
| To have the option of defragmenting drive C when you start your computer, |
| you could add the following lines to your AUTOEXEC.BAT file: |
|
| choice Defrag drive /ty,5 |
| if errorlevel 2 goto SkipDefrag |
| defrag c: |
| :SkipDefrag |
|
| If you press N within 5 seconds, DEFRAG will not run and CHOICE returns an |
| ERRORLEVEL value of 2. If you do not press N within 5 seconds, or if you |
| choose Y, DEFRAG is run on drive C. |
|
| Using CHOICE in a batch program |
|
| The following batch program demonstrates using the CHOICE option to select |
| one of three programs: MS-DOS Editor, Microsoft Anti-Virus, or Microsoft |
| Backup. |
|
| Notice that the IF ERRORLEVEL statements are listed in decreasing order. |
| MS-DOS will consider the IF statement true if the ERRORLEVEL parameter |
| returned by CHOICE is greater than or equal to the parameter specified in |
| the IF command. |
|
| @echo off |
| cls |
| echo. |
| echo A Microsoft Editor |
| echo B Microsoft Anti-Virus |
| echo C Microsoft Backup |
| echo. |
| choice /c:abc Choose an option |
| if errorlevel 3 goto MSBackup |
| if errorlevel 2 goto Msav |
| if errorlevel 1 goto Edit |
|
| :Edit |
| edit |
| goto End |
|
| :Msav |
| msav |
| goto End |
|
| :Msbackup |
| msbackup |
| goto End |
|
| :End |
|
|