用于 Windows XP 和 2003 的 Windows 批处理选择命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/566944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:00:36  来源:igfitidea点击:

Windows batch choice command for Windows XP & 2003

windowsbatch-filecmdwindows-xp

提问by Marcus Leon

Is there a way to prompt users for input (ie: Yes/No) from a Windows batch script that works on XP and Windows 2003 server? It seems some commands (ie: choice) only work on one OS and not others.

有没有办法从适用于 XP 和 Windows 2003 服务器的 Windows 批处理脚本提示用户输入(即:是/否)?似乎某些命令(即:选择)仅适用于一种操作系统,而不适用于其他操作系统。

回答by Nick Fortescue

Use the SETcommand with the /P switch.

SET命令与 /P 开关一起使用。

回答by aphoria

SET /P RESULT=Y or N?
ECHO %RESULT%

回答by James Messinger

Note that the SET /Pcommand does not support all the same features as the CHOICEcommand. Namely:

请注意,该SET /P命令不支持与该CHOICE命令相同的所有功能。即:

  • It doesn't restrict the user to entering a valid value
  • The user has to press enter
  • You have to check for casing differences (e.g. "A" vs "a")
  • There is no way to default to a certain choice after a certain amount of time
  • 它不限制用户输入有效值
  • 用户必须按回车键
  • 您必须检查大小写差异(例如“A”与“a”)
  • 没有办法在一定时间后默认为某个选择

For these reasons, I still prefer to use the CHOICEcommand rather than the SET /Pcommand. To do this, you just need to include CHOICE.COMalong with your batch file. You can download CHOICE.COMfrom Microsoft via the MS-DOS 6.22 Supplemental Disk. Here's the link:

由于这些原因,我仍然更喜欢使用CHOICE命令而不是SET /P命令。为此,您只需要在批处理文件中包含CHOICE.COM。您可以通过MS-DOS 6.22 Supplemental Disk从 Microsoft下载CHOICE.COM。这是链接:

http://support.microsoft.com/kb/117600

http://support.microsoft.com/kb/117600

回答by Snake

This will basically mimic what choice does, you will need to put it as a subroutine in your batch file. I also prefer choice but I need something portable that will run on Windows XP.

这将基本上模仿选择的作用,您需要将其作为子程序放入批处理文件中。我也更喜欢选择,但我需要可以在 Windows XP 上运行的便携式设备。

You can then modify this to accept other "choices," however this will work as case insensitive and repeat the prompt until the user explicitly enters Y, y, N, or n.

然后您可以修改它以接受其他“选择”,但是这将不区分大小写并重复提示,直到用户明确输入 Y、y、N 或 n。

:yesorno
set /p choice=%2
if /i NOT %choice% == n (
    if /i NOT %choice% == y goto yesorno
)
set "%~1=%choice%"
goto :eof

You would then call this subroutine via:

然后,您将通过以下方式调用此子程序:

call :yesorno answer "Do you want to continue? [Y/n]: "

It's been working very well for me so far.

到目前为止,它对我来说效果很好。

回答by PARASITE

For instance you could use this:

例如你可以使用这个:

SET /P ANSWER=y OR n?
If "%answer%"=="y" goto yes
If "%answer%"=="n" goto no

Enjoy!

享受!

回答by BlueWings

Windows Millenium's CHOICE.COM works fine for me under XP SP3. However, mine is hungarian language, but you can probably find its original english variant, for example searching for "windows millenium ebd".

Windows Millenium 的 CHOICE.COM 在 XP SP3 下对我来说很好用。但是,我的语言是匈牙利语,但您可能会找到它的原始英语变体,例如搜索“windows millenium ebd”。

http://s000.tinyupload.com/index.php?file_id=57468192666746678653

http://s000.tinyupload.com/index.php?file_id=57468192666746678653