tags:

views:

202

answers:

1

I have program, that must interact with at DOS program before my program can continue what it is doing. I'm trying to avoid my user from having to interact with this dos program. So, I created a .bat file that does everything I need to do except for the last step which still requires user interaction that I'm trying to avoid.

Specifically, the command I type ends up at a dos prompt where I need to automatically enter y and then enter (to say yes to the prompt) and then I want to exit out.

Is there any way that I can make this happen automatically without my user having to enter y and enter? Ideally, I'd like to have the console-window NOT even pop up while this is going on.

+5  A: 

You can pipe in a 'y' character into the program like so:

echo y | executable.exe

Multiple lines can be entered like so:

(echo y
echo n) | executable.exe

...which will pass first 'y' then 'n'.

See tip from Microsoft here.

James Kolpack
How does it know when to enter the y, when you pipe like this?
LonnieBest
It doesn't, it just sits in the buffer until the program asks for input
Jason
James, that worked. The script crashes, but not before it does the work I need done. Thanks.
LonnieBest
What do you do when you want to pass more than one command in?
LonnieBest
I've edited the response with the way to do this.
James Kolpack
James, in my case, after the "y" comes a username prompt follows. But, at that point, I'd like to exit out instead of entering the username and password, because after the "y" is entered, the needed work is accomplished. Is there any way to pass a 2nd command that will make the console exit even though, that at that point, it is at another prompt that doesn't expect such a command?
LonnieBest
You *might* be able to echo in the Ctrl+C ascii character - (\x03) - but I'm trying it now on an exe that should break, and it's not. An alternate approach could be to host/monitor your application in another executable. There are capabilities for this in .NET using System.Diagnositics.Proceess - check out this article for some VB-based examples http://www.thescarms.com/dotnet/Process.aspx . Using that approach, you could monitor StandardOutput, wait for the prompt, enter 'y', and then terminate the application.
James Kolpack