命令行大全

choice

舞夕之 发表于 2020-06-29 14:26浏览次数:

choice命令允许用户在进行一系列选择时阻止批处理文件和脚本运行。 具有使用choice命令的批处理文件的Microsoft Windows 2000和Windows XP用户应修改该批处理文件以使用MS-DOS set命令。

查看英文版

目录:

1 choice 运行系统环境

2 choice 语法

3 choice 示例

choice 运行系统环境

Windows 95

Windows 98

Windows vista

Windows 7

Windows 8

Windows 10

MS-DOS 6.0及更高版本

choice 语法

Windows Vista和更高版本的语法

CHOICE [/C choices] [/N] [/CS] [/T timeout /D choice] [/M text]
/C choices 指定要创建的选项列表。 默认列表为“ YN”。
/N 隐藏提示中的选项列表。 显示提示前的消息,并且选项仍处于启用状态。
/CS 允许选择区分大小写的选项。 默认情况下,该实用程序不区分大小写。
/T timeout 做出默认选择之前要暂停的秒数。 可接受的值是0到9999。如果指定0,则不会暂停,并且选择了默认选项。
/D choice 在nnnn秒后指定默认选项。 字符必须在/ C选项指定的选项集中,并且还必须使用/ T指定nnnn。
/M text 指定在提示之前显示的消息。 如果未指定,则该实用程序仅显示提示。

注意:

ERRORLEVEL 环境变量设置为从选项集中选择的键的索引。列出的第一个选项返回值1,第二个返回值2,依此类推。如果用户按下不是有效选项的键,该工具会发出警告蜂鸣声。如果工具检测到错误情况,它将返回255的ERRORLEVEL值。如果用户按Ctrl + BREAK或Ctrl + C,则该工具将返回ERRORLEVEL值0。在批处理程序中使用ERRORLEVEL参数时,请以降序列出订购。

例子:

CHOICE /?
CHOICE /C YNC /M "Press Y for Yes, N for No or C for Cancel."
CHOICE /T 10 /C ync /CS /D y
CHOICE /C ab /M "Select a for option 1 and b for option 2."
CHOICE /C ab /N /M "Select a for option 1 and b for option 2."
Windows XP和更早的语法
choice [/C[:]choices] [/N] [/S] [/T[:]c,nn] [text]
选项
/C[:]choices
指定允许的密钥。默认为是/否。
/ N 不显示选项和“?” 在提示字符串的末尾。
/S 将选择键区分大小写。
/ T [:] c,nn nn秒后默认选择为c。
text 显示提示字符串。
Windows Vista and later syntax
CHOICE [/C choices] [/N] [/CS] [/T timeout /D choice] [/M text]
/C choices Specifies the list of choices to be created. Default list 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.
/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.
/D choice Specifies the default choice after nnnn seconds. Character must be in the set of choices specified by /C option and must also specify nnnn with /T.
/M text Specifies the message to show before the prompt. If not specified, the utility displays only a prompt.

NOTE:

The ERRORLEVEL environment variable is set to 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, etc. 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:

CHOICE /?
CHOICE /C YNC /M "Press Y for Yes, N for No or C for Cancel."
CHOICE /T 10 /C ync /CS /D y
CHOICE /C ab /M "Select a for option 1 and b for option 2."
CHOICE /C ab /N /M "Select a for option 1 and b for option 2."
Windows XP and earlier syntax
choice [/C[:]choices] [/N] [/S] [/T[:]c,nn] [text]
Options
/C[:]choices Specifies allowable keys. Default is Y/N.
/N Do not display choices and "?" at end of prompt string.
/S Treat choice keys as case-sensitive.
/T[:]c,nn Default choice to c after nn seconds.
text Prompt string to display.

查看英文版

查看中文版

choice 示例

如何使用选择并在批处理文件中进行设置
如何在批处理文件中使用set
下面是如何使用set命令使批处理文件用户能够按1、2或3并执行所按选项的步骤的方法。

@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

在上面的批处理文件中,当用户输入数据并使用set / p选择行按Enter时,将分配%choice%变量。 如果输入1、2或3,则goto转到相应的标签并执行回显,然后转到批处理文件的末尾。

  • 请参阅我们的set命令页面以获取更多信息和选项。
如何在批处理文件中使用选择
以下是如何使用choice命令提供选项1、2或3并执行所按选项的步骤。
@ECHO OFF
:BEGIN
CLS
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
GOTO END
:THREE
ECHO YOU HAVE PRESSED THREE
GOTO END
:TWO
ECHO YOU HAVE PRESSED TWO
GOTO END
:ONE
ECHO YOU HAVE PRESSED ONE
:END
pause

在上面的批处理文件中,choice具有1、2或3选项。 如果按下其中任何一个,它将转到带有goto的标签,并回显所按下的数字。

How to use choice and set in a batch file

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.

  • 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 options 1, 2, or 3 and perform the steps for the option pressed.

@ECHO OFF
:BEGIN
CLS
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
GOTO END
:THREE
ECHO YOU HAVE PRESSED THREE
GOTO END
:TWO
ECHO YOU HAVE PRESSED TWO
GOTO END
:ONE
ECHO YOU HAVE PRESSED ONE
:END
pause

In the above batch file, 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.

查看英文版

查看中文版