Link to home
Start Free TrialLog in
Avatar of JElster
JElsterFlag for United States of America

asked on

Run a DOS batch file from C# and have C# press the Y and ENTER button

I need to run a dos batch file.. but at the end it prompts with Y and an ENTER.. to continue and overwrite a file.
Any ideas how I can have the Process pause and press the Y and Enter key??

TIA

            ProcessStartInfo psi = new ProcessStartInfo(ConfigurationSettings.AppSettings["UpLoadLocation"]+"\\pgpREGED.bat");
                        psi.UseShellExecute = true;
                                    Process process = Process.Start(psi);
Avatar of csharpbloke
csharpbloke

What I have done in such situations is as follows :

1) In the process start info of the process, set the RedirectStandardOutput to true
2) after launching the process, do somthing like
string str = process.StandardOutput.ReadToEnd();
3) Parse this string to see if the process is asking for something.

I have usually done it to parse the messages from the process after it has finished so can't provide exact code to you but this should definitely give you enough pointers to get going.

Regards,
Avatar of JElster

ASKER

So if the process is asking for something how do I 'say' Y and press the Enter key?
thx
ASKER CERTIFIED SOLUTION
Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of JElster

ASKER

It runs.. I get the black cmd window and it just waits.. and nothing happens until I close the cmd wind??
Any ideas?
thanks


            ProcessStartInfo psi = new ProcessStartInfo(ConfigurationSettings.AppSettings["UpLoadLocation"]+"\\REGED.bat");
                        psi.UseShellExecute = false;
                        psi.RedirectStandardInput = true;
                        psi.RedirectStandardOutput = true;
                        psi.RedirectStandardError = true;
                        Process process = Process.Start(psi);
                        
                        string str = process.StandardOutput.ReadToEnd();



                        process.StandardInput.WriteLine("y");  //and done :)





SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Once it works, you may polish your app by using
psi.CreateNoWindow = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;

to hide the black dos box.
Try This :
                    psi.RedirectStandardOutput = false;
                    psi.RedirectStandardError = false;
If something is goin wrong with the proggy itself we wont get wat it is saying. so dont redirect std out so u can see
if anything is goin wrong.

Regards,
Chinmay
Avatar of JElster

ASKER

It's as thought the    

process.StandardInput.WriteLine("y");  //and done :)

Doesn't work...  Do I need to force an ENTER too?

thanks!
did you try flushing it? as I had mentioned in the last post

<Quote>
Try adding the following line after the writeline statement.
process.StandardInput.Flush();
</Quote>
Avatar of JElster

ASKER

Yes...
Just does do anything.. just hangs with the confirmation msg??
thx
Hi There,

Try to put another process.StandardInput.WriteLine();

If this doesn't work for you, can u pls. be spcific wat kind of batch you are running so we
can have better understaning of the scenario.

Regards,
Chinmay
Dear JElster,
You may be happy to know that your process have been working. I am not sure why the output is not appearing. I tried similar thing here and got the same result. If you append the following code after the code that you already have, you should be able to see your output in the trace window

while (true)
{
      string str = proc.StandardOutput.ReadLine();
      if (str == null)
            break;
      Trace.WriteLine(str);
}

also modify the initial code to set RedirectStandardOutput to true.

Let us know how it goes.
Curry in a Hurry.... I am always in a hurry to click the submit button, anyways here's how my complete code looks :

ProcessStartInfo _psi = new ProcessStartInfo(@"d:\myfolder\myfile.bat");

_psi.WorkingDirectory = @"D:\myfolder";

_psi.UseShellExecute = false;
_psi.RedirectStandardInput = true;
_psi.RedirectStandardOutput = true;
_psi.RedirectStandardError = false;

_psi.Arguments = @"SP"; //my batch file takes an argument

Process proc = new Process();
proc.StartInfo = _psi;
proc.Start();

StreamWriter procWriter = proc.StandardInput;
procWriter.WriteLine("Y");

while (true)
{
      string str = proc.StandardOutput.ReadLine();
      if (str == null) //once there is nothing it returns null not an empty string
            break;
      Trace.WriteLine(str);
}
Avatar of JElster

ASKER

I'm trying to run PGP.exe (encrypts a file using PGP with a bunch of params)

It gives you a few message lines and some blank lines and then the confirmation (Y/N?)

When I run it with the code provided I get the first  few message lines and then it just 'hangs'

any more ideas?