2019年4月14日 星期日

[Batch File] CHOICE Parameter

CHOICE is available again in Windows Vista and later versions.

Syntax:

CHOICE [ /C choices ] [ /N ] [ /CS ] [ /T timeout /D choice ] [ /M text ]
 
Description:
    This tool allows users to select one item from a list of choices and returns the index of the selected choice.
Parameter List:
 
    /C choices    Specifies the list of choices to be created.
Default list for English versions is YN
 /N Hides the list of choices in the prompt.
The message before the prompt is displayed and the choices are still enabled.
 /CS Enables case-sensitive choices to be selected.
By default, the utility is case-insensitive.

Note: DOS and NT Resource Kit versions use /S instead
 /T timeout The number of seconds to pause before a default choice is made.
Acceptable values are from 0 to 9999.
If 0 is specified, there will be no pause and the default choice is selected.

Note: DOS and NT Resource Kit versions use /T:default,timeout instead.
 /D default Specifies the default choice after timeout seconds.
Character must be in the set of choices specified by /Coption and must also specify timeout with /T.

Note: DOS and NT Resource Kit versions use /T:default,timeout instead.
 /M text Specifies the message to be displayed before the prompt.
If not specified, the utility displays only a prompt.
 
    The ERRORLEVEL is set to the offset of the index of the key that was selected from the set of choices.
The first choice listed returns a value of 1, the second a value of 2, and so on.
If the user presses a key that is not a valid choice, the tool sounds a warning beep.
If tool detects an error condition, it returns an ERRORLEVEL value of 255.
If the user presses CTRL+BREAK or CTRL+C, the tool returns an ERRORLEVEL value of 0.
When you use ERRORLEVEL parameters in a batch program, list them in decreasing order.

Examples:

  • The command:

    CHOICE /M "Do yo really want to quit"

    Will display the following line:

    Do yo really want to quit? [YN]

    If the user presses Y, CHOICE exits with return code ("errorlevel") 1 (1st character in choices), if the user presse N, CHOICE exits with return code 2 (2nd character in choices).
     
  • CHOICE /C ABCDN /N /T 10 /D C /M "Format drive A:, B:, C:, D: or None?"
    IF ERRORLEVEL 1 SET DRIVE=drive A:
    IF ERRORLEVEL 2 SET DRIVE=drive B:
    IF ERRORLEVEL 3 SET DRIVE=drive C:
    IF ERRORLEVEL 4 SET DRIVE=drive D:
    IF ERRORLEVEL 5 SET DRIVE=None
    ECHO You chose to format %DRIVE%


    The CHOICE command in this example will prompt the user with the following line:

    Format drive A:, B:, C:, D: or None?

    If the user presses C, CHOICE exits with a return code ("errorlevel") 3 (3rd character in choices).
    The IF ERRORLEVEL checks for 1, 2 and 3 are true (see the errorlevel page for an explanation of errorlevels), so the variable DRIVE will be set to "drive A:" first, then to "drive B:", and finaly to "drive C:".
    So the ECHO command will display the line:

    You chose to format drive C:

1 則留言: