4

I have an executable that I can run interactively from the commandline. Here is how it looks:

C:\Users\Me> my_executable.exe  # Running the executable from CMD

Welcome! Please choose one:
0: Exit
1: Sub-task 1
2: Sub-task 2
Enter your input: 2             # I entered this interactively

Sub-task 2 chosen.
Please choose next option:
0: Return to previous menu
1: Connect to server
2: Disconnect from server
3: Call server API 1
4: Call server API 2
Enter your input: 1             # I entered this interactively

I cannot specify the input arguments before-hand using flags. For instance, nothing of this sort works:

C:\Users\Me> my_executable.exe 2 # Running the executable from CMD with first argument specified

Sub-task 2 chosen.
Please choose next option:
0: Return to previous menu
1: Connect to server
2: Disconnect from server
3: Call server API 1
4: Call server API 2

Enter your input: 

What would be the right way to automate this using a batch file? I came across a similar requirement in this SO thread, but the difference is that the executable there takes command-line arguments (unlike in my case).

Chaos
  • 466
  • 1
  • 5
  • 12
  • If you interested in programming or scripting you can write simple code using Python or other automation tools like AutoIt . – Mojtaba Tajik Sep 09 '18 at 07:01
  • I am trying to do this in Python (Although facing other issues there). I thought that a batch-file based solution should be possible for this workflow. – Chaos Sep 09 '18 at 07:03
  • You can use `%1` `%2` etc. If it is actually a batch file. So in your script have `if "%1 equ "2" do something` the first argument after cmd will be `%1` the next `%2` etc. – Gerhard Sep 09 '18 at 07:13
  • That being said. If it is actually an executable it should probably have actual switch options. So try run the file with `/?` or `--help`. – Gerhard Sep 09 '18 at 07:34
  • @GerhardBarnard, thank you for the help. Unfortunately, it is an actual executable and it does not have 'help' or '/?' options. :( It seems the only way to interact with it is to pass inputs in command line. I tried piping inputs like this: "ECHO 2 | my_executable.exe" which produces strange behavior (the 1st menu is shown and then the exe exits). – Chaos Sep 09 '18 at 07:51
  • @Mofi It is an actual EXE. It's written in C++ and was compiled to target a specific system (the one where I am running it currently). I don't have access to the source code. – Chaos Sep 09 '18 at 07:53
  • You can't do it using batch file, when your program run the control redirect to your program and your batch file couldn't do anything until the program end. The only way is write the script to achieve this. – Mojtaba Tajik Sep 09 '18 at 08:15
  • 1
    Create an inputFile.txt with two lines: `2` and `1`, and then try: `my_executable.exe < inputFile.txt`. If this not work, see [SendKeys](https://stackoverflow.com/questions/17038282/press-keyboard-keys-using-a-batch-file/17050135#17050135); in this case, `SendKeys` first and then run the .exe – Aacini Sep 09 '18 at 11:14

2 Answers2

6

Assuming your executable reads stdin, and does not access the keyboard directly, then you can use redirection or a pipe to provide all of the responses needed to complete the run.

Let's assume that you want the 2, 1 responses you indicated, but then after the server connection is achieved the exe loops back to the first menu. Assuming you want to quit, you would also need to follow up with 0.

To use redirection, you need to prepare a text file with all of the needed responses, one response per line.

@echo off
> response.txt (
  echo 2
  echo 1
  echo 0
)
my_executable.exe < response.txt
del response.txt

Or you might prefer to use a FOR loop

@echo off
(for %%A in (2 1 0) do echo %%A) > response.txt
my_executable.exe < response.txt
del response.txt

You can avoid the temporary file if you use a pipe

@echo off
(
  echo 2
  echo 1
  echo 0
) | my_executable

or with a FOR loop

@echo off
(for %%A in (2 1 0) do echo %%A) | my_executable
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • To clarify the first sentence, no console application accesses the keyboard directly. The issue is that an application may call `ReadConsole` or open "CONIN$" to read directly from the input buffer of the attached console, as opposed to generically reading the `StandardInput` handle via `ReadFile`. – Eryk Sun Sep 09 '18 at 21:01
  • Only the kernel side of the window manager (win32k.sys) has direct access to the keyboard driver. It creates the keyboard input messages (e.g. `WM_KEYDOWN`) that get posted to the message queue of a thread that owns a given window, such as the input thread of an instance of the console host, conhost.exe. The console's input buffer in turn contains input records for keyboard and mouse events that are translated from these GUI window messages. – Eryk Sun Sep 09 '18 at 21:09
  • @eryksun - Thanks for the clarification – dbenham Sep 10 '18 at 09:34
  • In my case, only temporary file works and pipe does not help. I don't know why. ``` @echo off > response.txt ( echo 2 echo. echo 0 ) my_executable.exe < response.txt del response.txt ``` My second input from echo is an Enter, that "echo." cannot be transferred by pipe. – Togor Feb 08 '22 at 05:06
0

I had similar problem automating installation of golden gate monitoring agent instance in windows server, following sample helped -

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
Start-Process -FilePath C:\myexecbatchfile.bat

# Wait the application start for 2 sec 
Start-Sleep -m 2000

# Send keys
[System.Windows.Forms.SendKeys]::SendWait("input1")
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
Start-Sleep -m 3000

[System.Windows.Forms.SendKeys]::SendWait("input2")
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")
Vijred
  • 349
  • 1
  • 2
  • 10