Automatically answer to input prompt in windows batch

后端 未结 3 644
小蘑菇
小蘑菇 2021-02-03 21:52

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

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


        
相关标签:
3条回答
  • 2021-02-03 22:00

    For multiple inputs, do:

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

    0 讨论(0)
  • 2021-02-03 22:12

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

    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.

    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.

    0 讨论(0)
  • 2021-02-03 22:16

    You want this:

    echo y | [Command]
    

    Eg: echo y | program.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.

    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.

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

    0 讨论(0)
提交回复
热议问题