如何在 Windows 上将控制台输入(是/否)作为批处理文件的一部分提供。

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15147900/
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-09 10:19:03  来源:igfitidea点击:

How to supply console input ( yes / no ) as part of batch file on Windows.

windowsbatch-fileconsole

提问by CodeBlue

I am writing a simple batch file (remove.bat) to remove a directory and all its subdirectories. The file contains the following command-

我正在编写一个简单的批处理文件 (remove.bat) 来删除目录及其所有子目录。该文件包含以下命令 -

rmdir /S modules

where modulesis the name of the non-empty directory.

其中modules是非空目录的名称。

I get the following message -

我收到以下消息 -

C:\...\bin>rmdir /S modules
modules, Are you sure (Y/N)?

How can I supply through the batch filethe console input "Y" to the Y/N question above? Is there a command that can do this?

如何通过批处理文件将控制台输入“Y”提供给上面的 Y/N 问题?有没有可以做到这一点的命令?

回答by dbenham

As others have pointed out, you should use the /Qoption. But there is another "old school" way to do it that was used back in the day when commands did not have options to suppress confirmation messages. Simply ECHO the needed response and pipe the value into the command.

正如其他人指出的那样,您应该使用该/Q选项。但是还有另一种“老派”的方式来做到这一点,它在命令没有选项来抑制确认消息的那一天使用过。只需 ECHO 所需的响应并将值通过管道传输到命令中。

echo y|rmdir /s modules

I recommend using the /Qoption instead, but the pipe technique might be important if you ever run into a command that does not provide an option to suppress confirmation messages.

我建议改用该/Q选项,但如果您遇到不提供抑制确认消息选项的命令,则管道技术可能很重要。

回答by sschrass

Do rmdir /Sfor deleting a non-empty directory and do rmdir /Qfor not prompting. Combine to rmdir /S /Qfor quietly delete non-empty directories.

rmdir /S了删除非空目录,做rmdir /Q不提示。结合 tormdir /S /Q悄悄删除非空目录。

回答by 500 - Internal Server Error

You can do

你可以做

rmdir /Q

Q is for quiet

Q代表安静

回答by Kevin Richardson

Use rmdir /S /Q modules

rmdir /S /Q modules

/Qsuppresses the confirmation prompt.

/Q禁止确认提示。

回答by u8it

I just want to add that, although not applicable to Rmdir, a force switch may also be the solution in some cases. So in a general sense you should look at your command switches for /f, /q, or some variant thereof (for example, Netdom RenameComputer uses /Force, not /f).

我只想补充一点,虽然不适用于 Rmdir,但在某些情况下,强制开关也可能是解决方案。因此,一般而言,您应该查看 /f、/q 或其某些变体的命令开关(例如,Netdom RenameComputer 使用 /Force,而不是 /f)。

The echo pipe is a neat trick and very useful to keep around since you wont always find an appropriate switch. For instance, I think it's the only way to bypass this Y/N prompt...

回声管是一个巧妙的技巧,并且非常有用,因为您不会总能找到合适的开关。例如,我认为这是绕过这个 Y/N 提示的唯一方法......

Echo y|NETDOM COMPUTERNAME WorkComp /Add:Work-Comp

Link to nearly identical StackOverflow post

链接到几乎相同的 StackOverflow 帖子