在Windows批处理中自动应答输入提示

use*_*212 17 windows batch-file

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

>someProgram.exe
> "Please enter "s" to start the program:
> 
Run Code Online (Sandbox Code Playgroud)

如何自动将"y"输入传递给提示符,以便我可以通过单击批处理来启动程序?

Cha*_*ara 23

你要这个:

echo y | [Command]
Run Code Online (Sandbox Code Playgroud)

例如:echo y | Program.exe文件

"echo <answer> | <batch command>"
Run Code Online (Sandbox Code Playgroud)

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

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

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

  • 我可以确认它适用于"del"命令,但不能与我的特定exe文件一起使用.内置的Windows命令(如"del"和任意.exe)之间是否存在差异? (5认同)
  • 我还发现有时有必要删除“y”和“|”之间的空格,例如“echo y|” [命令]`。 (3认同)

小智 7

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

(回声输入1 &&回声输入2)| 程式


小智 5

您可以使用 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}"
Run Code Online (Sandbox Code Playgroud)

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

例如:

@echo off
'command to run VBScript
filename.vbs
pause
Run Code Online (Sandbox Code Playgroud)

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