windows Windows批处理中自动回答输入提示

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

Automatically answer to input prompt in windows batch

windowsbatch-file

提问by user1934212

In a windows batch, I want to start a program that prompts the user for an input:

在 Windows 批处理中,我想启动一个提示用户输入的程序:

>someProgram.exe
> "Please enter "s" to start the program:
> 

How can I automatically pass the "y" input to the prompt, such that I can start the program by just clicking the batch?

如何自动将“y”输入传递给提示,以便我只需单击批处理即可启动程序?

回答by Chandana Kumara

You want this:

你要这个:

echo y | [Command]

Eg: echo y | program.exe

例如:echo y | 程序.exe

"echo <answer> | <batch command>"

Ex: The del /P command option will prompt for user's confirmation before deleting the file. So if you are using this option in your batch script, it requires manual input to proceed further. To avoid this manual input, use the command "echo Y | del /P " in your batch script to answer the prompt.

例如:del /P 命令选项将在删除文件之前提示用户确认。因此,如果您在批处理脚本中使用此选项,则需要手动输入才能继续进行。为避免这种手动输入,请在批处理脚本中使用命令“echo Y | del /P”来回答提示。

You can try this echo command to pass input (ex: answer for username and password prompts) to your console application, when it is invoked through batch script.

当通过批处理脚本调用时,您可以尝试使用此 echo 命令将输入​​(例如:用户名和密码提示的答案)传递给您的控制台应用程序。

Refer: http://thirutechie.blogspot.com/2009/10/how-to-auto-answer-prompts-in-windows.html

参考:http: //thirutechie.blogspot.com/2009/10/how-to-auto-answer-prompts-in-windows.html

回答by ImagineMBE

For multiple inputs, do:

对于多个输入,请执行以下操作:

(echo input1 && echo input2) | program.exe

(echo input1 && echo input2) | 程序.exe

回答by spoorthi vaidya

You can automate the user input prompt using VBScript and then write command to run VBScript in the batch script.

您可以使用 VBScript 自动执行用户输入提示,然后编写命令以在批处理脚本中运行 VBScript。

Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "command_that_asks_for prompt", 9 
WScript.Sleep 500 

'Msg is first prompt answer
Dim Msg: Msg = WshShell.ExpandEnvironmentStrings( "%DB_TYPE%" )

'send character by character using for loop for first input prompt
For i = 1 To Len(Msg)
WScript.Sleep 200
WshShell.SendKeys Mid(Msg, i, 1)
Next

WshShell.SendKeys "{ENTER}"

'Msg2 is second input prompt answer
Msg2 = WshShell.ExpandEnvironmentStrings( "%CONNECTION_TYPE%" )


' send character by character for second input prompt
For i = 1 To Len(Msg2)
   WScript.Sleep 200
   WshShell.SendKeys Mid(Msg2, i, 1)
Next

WshShell.SendKeys "{ENTER}"

The above code is the example to provide the answer for 2 user input prompts. Similarly, we can provide for multiple prompts. Here I'm trying to provide environmental variables as an answer to the input prompts. The above code can be saved as filename.vbs and then write the command to VBScript in a batch script.

上面的代码是为 2 个用户输入提示提供答案的示例。同样,我们可以提供多个提示。在这里,我试图提供环境变量作为输入提示的答案。上面的代码可以保存为filename.vbs,然后在批处理脚本中将命令写入VBScript。

For example:

例如:

@echo off
'command to run VBScript
filename.vbs
pause

This can be used as the batch script to answer the input prompts automatically.

这可以用作批处理脚本来自动回答输入提示。