Click here to Skip to main content
15,918,808 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi all
I run a batch file which runs the same application numerous times with variable parameters
between each run it requires any key to be presses
How do i mimic the space bar hit in the cmd line?

the batch file looks like this:

cd "C:\My App"
"C:\MyApp\MyApp.exe" "1"
"C:\MyApp\MyApp.exe" "5"
"C:\MyApp\MyApp.exe" "0"

between the "C:\MyApp\MyApp.exe" "1" and the "C:\MyApp\MyApp.exe" "5"
the cmd line waits for any key press from the user.
so i want to write this key press in the cmd line
somtheing like:

cd "C:\My App"
"C:\MyApp\MyApp.exe" "1"
"PressKey" {"SpaceBar"}
"C:\MyApp\MyApp.exe" "5"
"PressKey" {"SpaceBar"}
"C:\MyApp\MyApp.exe" "0"



What I have tried:

Googling it
SendKeys command, maybe i used it wrong?
Richard MacCutchan 28-Jan-24 7:17am    
Why do you need it? The script will not continue until each execution terminates.
l o o l 24-Apr-24 6:55am    
I wouldn't say that, because there is an option to call any executable in /Background, where the script continues. But you must know this, or don't you?
Richard MacCutchan 24-Apr-24 6:59am    
That has nothing to do with the question, which clearly states that OP wants each process to complete before the next one starts.
Dave Kreskowiak 28-Jan-24 11:09am    
From a batch file, you can't. The next line of the batch file will not execute until the previous line finishes executing. If that previous line is an executable, the next line of the batch will not execute until the executable exits. If the executable is sitting there waiting for a keypress, it will do so forever because your batch file will never execute until the keypress is seen, allowing the executable to exit.
l o o l 24-Apr-24 6:59am    
Are you sure you say that? You may know (or not vbs) and through hybrid bats and calls in /Background, your statements do not reflect what the execution flow allows, besides advancing to the next line and at the same time waiting for a key to be pressed. But you must also know that this is possible, or don't you?

You can't send a key to the app after you start it from the command line: the execution of your commands (or batch file) doesn't happen until the app you ran is closed.

However you may be able to use pipelining to send a key to the app: about Pipelines - PowerShell | Microsoft Learn[^]

A better way if it's your app as your example seems to show would be to add a command line switch to your app which make it operate "unattended" - a global variable is set in the app startup and checked every time your app wants user input.
Share this answer
As Griff said, if you wrote the console application, then your best choice is to alter it so that it doesn't prompt for input.

If it is not something you can change, then maybe it already has something like that. You should investigate it further.

Failing that, you can write a console application which runs the application via a Process, detects the prompt, and responds to it. This doesn't always work, but you can try.

I have done this sort of thing : ProcessCommunicator[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900