By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,127 Members | 1,343 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,127 IT Pros & Developers. It's quick & easy.

run batch file with Standard Output and Input

P: n/a
Hi,

In my windows applicationm, i need to excute a batch file.
this batch file throws some text and questions to the screen, i need to
catch the standard Output, check if it's a question, in case it's a question,
i want to popup a messageBox or something, and bring back to the batch file
the result (Yes\No question). I know how to excute the batch file and get all
the Standard output at the end, but i don't know who can i read it line by
line and check if it's a question, and in case it is, bring the result to the
batch file back.

can anyone help me here?

Thanks,
Gidi.
Mar 24 '08 #1
Share this Question
Share on Google+
14 Replies


P: n/a
On Mon, 24 Mar 2008 02:56:00 -0700, Gidi <sh*****@hotmail.com.dontspamwrote:
>Hi,

In my windows applicationm, i need to excute a batch file.
this batch file throws some text and questions to the screen, i need to
catch the standard Output, check if it's a question, in case it's a question,
i want to popup a messageBox or something, and bring back to the batch file
the result (Yes\No question). I know how to excute the batch file and get all
the Standard output at the end, but i don't know who can i read it line by
line and check if it's a question, and in case it is, bring the result to the
batch file back.

can anyone help me here?
Hmm, check if the sample contained in this MSDN entry helps - sounds
approximately like what you intend to do ...

http://msdn2.microsoft.com/en-us/lib...treadline.aspx

Regards,
Gilles.

Mar 24 '08 #2

P: n/a
Thanks,

but i already read this, and it's not quite helping me, since it's not
executing a batch file, it's just open command line and reading the lines...
it's not what i need...

Thanks again,
Gidi.

"Gilles Kohl [MVP]" wrote:
On Mon, 24 Mar 2008 02:56:00 -0700, Gidi <sh*****@hotmail.com.dontspamwrote:
Hi,

In my windows applicationm, i need to excute a batch file.
this batch file throws some text and questions to the screen, i need to
catch the standard Output, check if it's a question, in case it's a question,
i want to popup a messageBox or something, and bring back to the batch file
the result (Yes\No question). I know how to excute the batch file and get all
the Standard output at the end, but i don't know who can i read it line by
line and check if it's a question, and in case it is, bring the result to the
batch file back.

can anyone help me here?

Hmm, check if the sample contained in this MSDN entry helps - sounds
approximately like what you intend to do ...

http://msdn2.microsoft.com/en-us/lib...treadline.aspx

Regards,
Gilles.

Mar 24 '08 #3

P: n/a
If you know how to read in all the standard output, then just split the
output on newline characters. You then would have the questions and
responses. Perhaps I'm not understanding where you are having the problem.
You should include a small set of code that runs the batch file and reads the
output clarifying where your concern is.

"Gidi" wrote:
Hi,

In my windows applicationm, i need to excute a batch file.
this batch file throws some text and questions to the screen, i need to
catch the standard Output, check if it's a question, in case it's a question,
i want to popup a messageBox or something, and bring back to the batch file
the result (Yes\No question). I know how to excute the batch file and get all
the Standard output at the end, but i don't know who can i read it line by
line and check if it's a question, and in case it is, bring the result to the
batch file back.

can anyone help me here?

Thanks,
Gidi.
Mar 24 '08 #4

P: n/a
On Mon, 24 Mar 2008 04:15:00 -0700, Gidi <sh*****@hotmail.com.dontspamwrote:
>Thanks,

but i already read this, and it's not quite helping me, since it's not
executing a batch file, it's just open command line and reading the lines...
it's not what i need...
You may have noticed that it also sends _input_ to the sort command, not only
captures its output.

This is how you could e.g. set up launching a batch file:

ProcessStartInfo startInfo = new ProcessStartInfo(@"c:\tmp\test.bat");
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardOutput = true;

startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;

m_TheBatch = new Process();
m_TheBatch.StartInfo = startInfo;

m_TheBatch.OutputDataReceived += new
DataReceivedEventHandler(theBatch_OutputDataReceiv ed);
m_TheBatch.Start();
m_TheBatch.BeginOutputReadLine();

(m_TheBatch is a member variable e.g. of your Form and of type Process)
This is what your handling could look like:

void theBatch_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if(e.Data == null)
{
return;
}

string lineRead = e.Data;

if(lineRead.Contains("enter first name now"))
{
m_TheBatch.StandardInput.Write("Gidi\r");
}
}

A problem you'll encounter is that OutputDataReceived is only called for
complete lines. So if your batch file asks for input e.g. this way:

set /p Name=Name?

You won't see the "Name?" prompt line (and won't be able to react to it) as it
does NOT output the CRLF required for OutputDataReceived to be triggered.

In this case, asynchronous reading of lines is indeed not what you need.

Do you need to get this to work for _any_ batch file, or for batch files that
you know? That you could maybe modify to work around the problem above?

I'm asking because a general solution to this problem seems difficult - how in
general tell that the batch is waiting for input? (The stream not having any
characters ready may just mean that it is performing a lengthy operation)

Even more difficult, how to detect where exactly the question that you want to
ask your user is in all the stuff the batch did output?

Before we delve deeper into reading the StandardOutput stream with other
means, can you elaborate on the problem you want to solve - maybe there are
better alternatives?

Regards,
Gilles.

Mar 24 '08 #5

P: n/a
The thing is that my batch file has a certain flow, and i've a question which
detrmain which flow to choose.
so i need to read line by line, and when i get to the question, i want to
display the question to the user and wait for his answer, and send it back to
the batch file and continue to the right flow.

this is my batch file:

@ECHO OFF

ECHO "GOOD MORNING %1%"
:ASK
SET /P ANSWER="Do you want to continue (y/n) ? "
IF "%ANSWER%" == "y" GOTO PRESS_YES
IF "%ANSWER%" == "Y" GOTO PRESS_YES
IF "%ANSWER%" == "n" GOTO PRESS_NO
IF "%ANSWER%" == "N" GOTO PRESS_NO
GOTO ASK

:PRESS_YES
exit 0

:PRESS_NO
exit 1

how can i send the batch file the answer i got?

thanks,
Gidi.

"Family Tree Mike" wrote:
If you know how to read in all the standard output, then just split the
output on newline characters. You then would have the questions and
responses. Perhaps I'm not understanding where you are having the problem.
You should include a small set of code that runs the batch file and reads the
output clarifying where your concern is.

"Gidi" wrote:
Hi,

In my windows applicationm, i need to excute a batch file.
this batch file throws some text and questions to the screen, i need to
catch the standard Output, check if it's a question, in case it's a question,
i want to popup a messageBox or something, and bring back to the batch file
the result (Yes\No question). I know how to excute the batch file and get all
the Standard output at the end, but i don't know who can i read it line by
line and check if it's a question, and in case it is, bring the result to the
batch file back.

can anyone help me here?

Thanks,
Gidi.
Mar 24 '08 #6

P: n/a
My recommendation is to not use a batch file at all. If you know the flow
then rewrite the batch file as a routine in your code. If you are required
to use the batch file, then you need to look at redirecting standard input to
the cmd.exe process which launched the batch file.

"Gidi" wrote:
The thing is that my batch file has a certain flow, and i've a question which
detrmain which flow to choose.
so i need to read line by line, and when i get to the question, i want to
display the question to the user and wait for his answer, and send it back to
the batch file and continue to the right flow.

this is my batch file:

@ECHO OFF

ECHO "GOOD MORNING %1%"
:ASK
SET /P ANSWER="Do you want to continue (y/n) ? "
IF "%ANSWER%" == "y" GOTO PRESS_YES
IF "%ANSWER%" == "Y" GOTO PRESS_YES
IF "%ANSWER%" == "n" GOTO PRESS_NO
IF "%ANSWER%" == "N" GOTO PRESS_NO
GOTO ASK

:PRESS_YES
exit 0

:PRESS_NO
exit 1

how can i send the batch file the answer i got?

thanks,
Gidi.

"Family Tree Mike" wrote:
If you know how to read in all the standard output, then just split the
output on newline characters. You then would have the questions and
responses. Perhaps I'm not understanding where you are having the problem.
You should include a small set of code that runs the batch file and reads the
output clarifying where your concern is.

"Gidi" wrote:
Hi,
>
In my windows applicationm, i need to excute a batch file.
this batch file throws some text and questions to the screen, i need to
catch the standard Output, check if it's a question, in case it's a question,
i want to popup a messageBox or something, and bring back to the batch file
the result (Yes\No question). I know how to excute the batch file and get all
the Standard output at the end, but i don't know who can i read it line by
line and check if it's a question, and in case it is, bring the result to the
batch file back.
>
can anyone help me here?
>
Thanks,
Gidi.
Mar 24 '08 #7

P: n/a
Hi,

first thanks for the help...

second, at the end, i won't run batch but another exe file, which i've to
use since it's too much work to rewrite all over again to C#...

this is my code for now (while testing it):

private void button1_Click(object sender, EventArgs e)
{
Process pepprocess = new Process();
pepprocess.StartInfo.FileName = "helloworld.bat";
pepprocess.StartInfo.UseShellExecute = false;
pepprocess.StartInfo.RedirectStandardInput = true;
pepprocess.StartInfo.RedirectStandardOutput = true;
pepprocess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
pepprocess.Start();

StreamWriter sw = pepprocess.StandardInput;
StreamReader rd = pepprocess.StandardOutput;

string line = rd.ReadToEnd();

richTextBox1.Text = line;
if (line.Contains("?") == true)
{
sw.Write("y");
}
Thread.Sleep(1000);
sw.Flush();

}

i tried to read it line by line, but after the first line, the command line
is stuck, and in addition, altough i set the window style to hidden, i still
get the cmd window.

Thanks,
Gidi.

"Family Tree Mike" wrote:
My recommendation is to not use a batch file at all. If you know the flow
then rewrite the batch file as a routine in your code. If you are required
to use the batch file, then you need to look at redirecting standard input to
the cmd.exe process which launched the batch file.

"Gidi" wrote:
The thing is that my batch file has a certain flow, and i've a question which
detrmain which flow to choose.
so i need to read line by line, and when i get to the question, i want to
display the question to the user and wait for his answer, and send it back to
the batch file and continue to the right flow.

this is my batch file:

@ECHO OFF

ECHO "GOOD MORNING %1%"
:ASK
SET /P ANSWER="Do you want to continue (y/n) ? "
IF "%ANSWER%" == "y" GOTO PRESS_YES
IF "%ANSWER%" == "Y" GOTO PRESS_YES
IF "%ANSWER%" == "n" GOTO PRESS_NO
IF "%ANSWER%" == "N" GOTO PRESS_NO
GOTO ASK

:PRESS_YES
exit 0

:PRESS_NO
exit 1

how can i send the batch file the answer i got?

thanks,
Gidi.

"Family Tree Mike" wrote:
If you know how to read in all the standard output, then just split the
output on newline characters. You then would have the questions and
responses. Perhaps I'm not understanding where you are having the problem.
You should include a small set of code that runs the batch file and reads the
output clarifying where your concern is.
>
"Gidi" wrote:
>
Hi,

In my windows applicationm, i need to excute a batch file.
this batch file throws some text and questions to the screen, i need to
catch the standard Output, check if it's a question, in case it's a question,
i want to popup a messageBox or something, and bring back to the batch file
the result (Yes\No question). I know how to excute the batch file and get all
the Standard output at the end, but i don't know who can i read it line by
line and check if it's a question, and in case it is, bring the result to the
batch file back.

can anyone help me here?

Thanks,
Gidi.
Mar 24 '08 #8

P: n/a
Here are a few things to change:

1. Batchfiles cannot be the executable process so I'm surprised it
started... I would change the code to do the following:
peprocess.StartInfo.FileName = "cmd.exe";
peprocess.StartInfo.Arguments = "helloworld.bat"

2. Usually the waiting batch prompt is waiting for text and a newline.
Change your statement inside the if (line.contains("?")) to:
sw.WriteLine("y");
"Gidi" wrote:
Hi,

first thanks for the help...

second, at the end, i won't run batch but another exe file, which i've to
use since it's too much work to rewrite all over again to C#...

this is my code for now (while testing it):

private void button1_Click(object sender, EventArgs e)
{
Process pepprocess = new Process();
pepprocess.StartInfo.FileName = "helloworld.bat";
pepprocess.StartInfo.UseShellExecute = false;
pepprocess.StartInfo.RedirectStandardInput = true;
pepprocess.StartInfo.RedirectStandardOutput = true;
pepprocess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
pepprocess.Start();

StreamWriter sw = pepprocess.StandardInput;
StreamReader rd = pepprocess.StandardOutput;

string line = rd.ReadToEnd();

richTextBox1.Text = line;
if (line.Contains("?") == true)
{
sw.Write("y");
}
Thread.Sleep(1000);
sw.Flush();

}

i tried to read it line by line, but after the first line, the command line
is stuck, and in addition, altough i set the window style to hidden, i still
get the cmd window.

Thanks,
Gidi.

"Family Tree Mike" wrote:
My recommendation is to not use a batch file at all. If you know the flow
then rewrite the batch file as a routine in your code. If you are required
to use the batch file, then you need to look at redirecting standard input to
the cmd.exe process which launched the batch file.

"Gidi" wrote:
The thing is that my batch file has a certain flow, and i've a question which
detrmain which flow to choose.
so i need to read line by line, and when i get to the question, i want to
display the question to the user and wait for his answer, and send it back to
the batch file and continue to the right flow.
>
this is my batch file:
>
@ECHO OFF
>
ECHO "GOOD MORNING %1%"
:ASK
SET /P ANSWER="Do you want to continue (y/n) ? "
IF "%ANSWER%" == "y" GOTO PRESS_YES
IF "%ANSWER%" == "Y" GOTO PRESS_YES
IF "%ANSWER%" == "n" GOTO PRESS_NO
IF "%ANSWER%" == "N" GOTO PRESS_NO
GOTO ASK
>
:PRESS_YES
exit 0
>
:PRESS_NO
exit 1
>
how can i send the batch file the answer i got?
>
thanks,
Gidi.
>
"Family Tree Mike" wrote:
>
If you know how to read in all the standard output, then just split the
output on newline characters. You then would have the questions and
responses. Perhaps I'm not understanding where you are having the problem.
You should include a small set of code that runs the batch file and reads the
output clarifying where your concern is.

"Gidi" wrote:

Hi,
>
In my windows applicationm, i need to excute a batch file.
this batch file throws some text and questions to the screen, i need to
catch the standard Output, check if it's a question, in case it's a question,
i want to popup a messageBox or something, and bring back to the batch file
the result (Yes\No question). I know how to excute the batch file and get all
the Standard output at the end, but i don't know who can i read it line by
line and check if it's a question, and in case it is, bring the result to the
batch file back.
>
can anyone help me here?
>
Thanks,
Gidi.
Mar 24 '08 #9

P: n/a
I believe this code will produce the results you want, but I believe there
are better alternatives to this...:

Process pepprocess = new Process();

pepprocess.StartInfo.FileName = "cmd.exe";
pepprocess.StartInfo.Arguments = "/c helloworld.bat";
pepprocess.StartInfo.UseShellExecute = false;
pepprocess.StartInfo.RedirectStandardInput = true;
pepprocess.StartInfo.RedirectStandardOutput = true;
pepprocess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
pepprocess.StartInfo.CreateNoWindow = true;
pepprocess.Start();

StreamWriter sw = pepprocess.StandardInput;
StreamReader rd = pepprocess.StandardOutput;

while (!rd.EndOfStream)
{
string line = rd.ReadLine ();
richTextBox1.Text = line;
if (line.Contains ( "?" ))
{
sw.WriteLine ( "n" );
}
}

MessageBox.Show ( string.Format ( "{0}",
pepprocess.ExitCode.ToString () ) );
"Family Tree Mike" wrote:
Here are a few things to change:

1. Batchfiles cannot be the executable process so I'm surprised it
started... I would change the code to do the following:
peprocess.StartInfo.FileName = "cmd.exe";
peprocess.StartInfo.Arguments = "helloworld.bat"

2. Usually the waiting batch prompt is waiting for text and a newline.
Change your statement inside the if (line.contains("?")) to:
sw.WriteLine("y");
"Gidi" wrote:
Hi,

first thanks for the help...

second, at the end, i won't run batch but another exe file, which i've to
use since it's too much work to rewrite all over again to C#...

this is my code for now (while testing it):

private void button1_Click(object sender, EventArgs e)
{
Process pepprocess = new Process();
pepprocess.StartInfo.FileName = "helloworld.bat";
pepprocess.StartInfo.UseShellExecute = false;
pepprocess.StartInfo.RedirectStandardInput = true;
pepprocess.StartInfo.RedirectStandardOutput = true;
pepprocess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
pepprocess.Start();

StreamWriter sw = pepprocess.StandardInput;
StreamReader rd = pepprocess.StandardOutput;

string line = rd.ReadToEnd();

richTextBox1.Text = line;
if (line.Contains("?") == true)
{
sw.Write("y");
}
Thread.Sleep(1000);
sw.Flush();

}

i tried to read it line by line, but after the first line, the command line
is stuck, and in addition, altough i set the window style to hidden, i still
get the cmd window.

Thanks,
Gidi.

"Family Tree Mike" wrote:
My recommendation is to not use a batch file at all. If you know the flow
then rewrite the batch file as a routine in your code. If you are required
to use the batch file, then you need to look at redirecting standard input to
the cmd.exe process which launched the batch file.
>
"Gidi" wrote:
>
The thing is that my batch file has a certain flow, and i've a question which
detrmain which flow to choose.
so i need to read line by line, and when i get to the question, i want to
display the question to the user and wait for his answer, and send it back to
the batch file and continue to the right flow.

this is my batch file:

@ECHO OFF

ECHO "GOOD MORNING %1%"
:ASK
SET /P ANSWER="Do you want to continue (y/n) ? "
IF "%ANSWER%" == "y" GOTO PRESS_YES
IF "%ANSWER%" == "Y" GOTO PRESS_YES
IF "%ANSWER%" == "n" GOTO PRESS_NO
IF "%ANSWER%" == "N" GOTO PRESS_NO
GOTO ASK

:PRESS_YES
exit 0

:PRESS_NO
exit 1

how can i send the batch file the answer i got?

thanks,
Gidi.

"Family Tree Mike" wrote:

If you know how to read in all the standard output, then just split the
output on newline characters. You then would have the questions and
responses. Perhaps I'm not understanding where you are having the problem.
You should include a small set of code that runs the batch file and reads the
output clarifying where your concern is.
>
"Gidi" wrote:
>
Hi,

In my windows applicationm, i need to excute a batch file.
this batch file throws some text and questions to the screen, i need to
catch the standard Output, check if it's a question, in case it's a question,
i want to popup a messageBox or something, and bring back to the batch file
the result (Yes\No question). I know how to excute the batch file and get all
the Standard output at the end, but i don't know who can i read it line by
line and check if it's a question, and in case it is, bring the result to the
batch file back.

can anyone help me here?

Thanks,
Gidi.
Mar 24 '08 #10

P: n/a
Hi,

thanks so much for the help.
i'm still having problem reading the question line (SET /P ANSWER="Do you
want to continue (y/n) ? ").

the program is stuck at this point.
if i set the pepprocess.StartInfo.RedirectStandardInput to false, so it
works (regardless what the answer was).

do you have any idea?

Thanks so much,
Gidi.
"Family Tree Mike" wrote:
I believe this code will produce the results you want, but I believe there
are better alternatives to this...:

Process pepprocess = new Process();

pepprocess.StartInfo.FileName = "cmd.exe";
pepprocess.StartInfo.Arguments = "/c helloworld.bat";
pepprocess.StartInfo.UseShellExecute = false;
pepprocess.StartInfo.RedirectStandardInput = true;
pepprocess.StartInfo.RedirectStandardOutput = true;
pepprocess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
pepprocess.StartInfo.CreateNoWindow = true;
pepprocess.Start();

StreamWriter sw = pepprocess.StandardInput;
StreamReader rd = pepprocess.StandardOutput;

while (!rd.EndOfStream)
{
string line = rd.ReadLine ();
richTextBox1.Text = line;
if (line.Contains ( "?" ))
{
sw.WriteLine ( "n" );
}
}

MessageBox.Show ( string.Format ( "{0}",
pepprocess.ExitCode.ToString () ) );
"Family Tree Mike" wrote:
Here are a few things to change:

1. Batchfiles cannot be the executable process so I'm surprised it
started... I would change the code to do the following:
peprocess.StartInfo.FileName = "cmd.exe";
peprocess.StartInfo.Arguments = "helloworld.bat"

2. Usually the waiting batch prompt is waiting for text and a newline.
Change your statement inside the if (line.contains("?")) to:
sw.WriteLine("y");
"Gidi" wrote:
Hi,
>
first thanks for the help...
>
second, at the end, i won't run batch but another exe file, which i've to
use since it's too much work to rewrite all over again to C#...
>
this is my code for now (while testing it):
>
private void button1_Click(object sender, EventArgs e)
{
Process pepprocess = new Process();
pepprocess.StartInfo.FileName = "helloworld.bat";
pepprocess.StartInfo.UseShellExecute = false;
pepprocess.StartInfo.RedirectStandardInput = true;
pepprocess.StartInfo.RedirectStandardOutput = true;
pepprocess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
pepprocess.Start();
>
StreamWriter sw = pepprocess.StandardInput;
StreamReader rd = pepprocess.StandardOutput;
>
string line = rd.ReadToEnd();
>
richTextBox1.Text = line;
if (line.Contains("?") == true)
{
sw.Write("y");
}
>
>
Thread.Sleep(1000);
>
>
sw.Flush();
>
}
>
i tried to read it line by line, but after the first line, the command line
is stuck, and in addition, altough i set the window style to hidden, i still
get the cmd window.
>
Thanks,
Gidi.
>
"Family Tree Mike" wrote:
>
My recommendation is to not use a batch file at all. If you know the flow
then rewrite the batch file as a routine in your code. If you are required
to use the batch file, then you need to look at redirecting standard input to
the cmd.exe process which launched the batch file.

"Gidi" wrote:

The thing is that my batch file has a certain flow, and i've a question which
detrmain which flow to choose.
so i need to read line by line, and when i get to the question, i want to
display the question to the user and wait for his answer, and send it back to
the batch file and continue to the right flow.
>
this is my batch file:
>
@ECHO OFF
>
ECHO "GOOD MORNING %1%"
:ASK
SET /P ANSWER="Do you want to continue (y/n) ? "
IF "%ANSWER%" == "y" GOTO PRESS_YES
IF "%ANSWER%" == "Y" GOTO PRESS_YES
IF "%ANSWER%" == "n" GOTO PRESS_NO
IF "%ANSWER%" == "N" GOTO PRESS_NO
GOTO ASK
>
:PRESS_YES
exit 0
>
:PRESS_NO
exit 1
>
how can i send the batch file the answer i got?
>
thanks,
Gidi.
>
"Family Tree Mike" wrote:
>
If you know how to read in all the standard output, then just split the
output on newline characters. You then would have the questions and
responses. Perhaps I'm not understanding where you are having the problem.
You should include a small set of code that runs the batch file and reads the
output clarifying where your concern is.

"Gidi" wrote:

Hi,
>
In my windows applicationm, i need to excute a batch file.
this batch file throws some text and questions to the screen, i need to
catch the standard Output, check if it's a question, in case it's a question,
i want to popup a messageBox or something, and bring back to the batch file
the result (Yes\No question). I know how to excute the batch file and get all
the Standard output at the end, but i don't know who can i read it line by
line and check if it's a question, and in case it is, bring the result to the
batch file back.
>
can anyone help me here?
>
Thanks,
Gidi.
Mar 25 '08 #11

P: n/a
On Tue, 25 Mar 2008 02:40:01 -0700, Gidi <sh*****@hotmail.com.dontspam
wrote:
[...]
while (!rd.EndOfStream)
{
string line = rd.ReadLine();
richTextBox1.Text = line;
if (line.Contains("?"))
{
sw.WriteLine("y");
}
}
Well, that certainly looks better. :)
the thing is that when i get to the line in my batch file, that expects
input, the process is stuck, and i can't send him the input.
I'm not saying that for sure, this would lead to a solution. But it's
difficult for anyone to know for sure what's going on without a complete
sample. We could theoretically come up with a sample .NET application
based on the code you posted, but we'd still need the batch file and any
relevant executables.

In other words, for best results, you should create such a
concise-but-complete sample and post it here. Keep everything as simple
as possible. You may want to post code for two different executables: the
one demonstrating the code you are trying to get working, and another that
is used from the batch file (if necessary...if you can reproduce the
behavior using the built-in "prompt" batch command, then the second
program may not be required).

Just from your description, I don't have a ready answer. Sorry.

Pete
Mar 25 '08 #12

P: n/a
I have no idea, as it is working for me using your batch file as provided in
your earlier message, and my code as provided in my earlier message.
"Gidi" wrote:
Hi,

thanks so much for the help.
i'm still having problem reading the question line (SET /P ANSWER="Do you
want to continue (y/n) ? ").

the program is stuck at this point.
if i set the pepprocess.StartInfo.RedirectStandardInput to false, so it
works (regardless what the answer was).

do you have any idea?

Thanks so much,
Gidi.
"Family Tree Mike" wrote:
I believe this code will produce the results you want, but I believe there
are better alternatives to this...:

Process pepprocess = new Process();

pepprocess.StartInfo.FileName = "cmd.exe";
pepprocess.StartInfo.Arguments = "/c helloworld.bat";
pepprocess.StartInfo.UseShellExecute = false;
pepprocess.StartInfo.RedirectStandardInput = true;
pepprocess.StartInfo.RedirectStandardOutput = true;
pepprocess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
pepprocess.StartInfo.CreateNoWindow = true;
pepprocess.Start();

StreamWriter sw = pepprocess.StandardInput;
StreamReader rd = pepprocess.StandardOutput;

while (!rd.EndOfStream)
{
string line = rd.ReadLine ();
richTextBox1.Text = line;
if (line.Contains ( "?" ))
{
sw.WriteLine ( "n" );
}
}

MessageBox.Show ( string.Format ( "{0}",
pepprocess.ExitCode.ToString () ) );
"Family Tree Mike" wrote:
Here are a few things to change:
>
1. Batchfiles cannot be the executable process so I'm surprised it
started... I would change the code to do the following:
peprocess.StartInfo.FileName = "cmd.exe";
peprocess.StartInfo.Arguments = "helloworld.bat"
>
2. Usually the waiting batch prompt is waiting for text and a newline.
Change your statement inside the if (line.contains("?")) to:
sw.WriteLine("y");
>
>
"Gidi" wrote:
>
Hi,

first thanks for the help...

second, at the end, i won't run batch but another exe file, which i've to
use since it's too much work to rewrite all over again to C#...

this is my code for now (while testing it):

private void button1_Click(object sender, EventArgs e)
{
Process pepprocess = new Process();
pepprocess.StartInfo.FileName = "helloworld.bat";
pepprocess.StartInfo.UseShellExecute = false;
pepprocess.StartInfo.RedirectStandardInput = true;
pepprocess.StartInfo.RedirectStandardOutput = true;
pepprocess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
pepprocess.Start();

StreamWriter sw = pepprocess.StandardInput;
StreamReader rd = pepprocess.StandardOutput;

string line = rd.ReadToEnd();

richTextBox1.Text = line;
if (line.Contains("?") == true)
{
sw.Write("y");
}


Thread.Sleep(1000);


sw.Flush();

}

i tried to read it line by line, but after the first line, the command line
is stuck, and in addition, altough i set the window style to hidden, i still
get the cmd window.

Thanks,
Gidi.

"Family Tree Mike" wrote:

My recommendation is to not use a batch file at all. If you know the flow
then rewrite the batch file as a routine in your code. If you are required
to use the batch file, then you need to look at redirecting standard input to
the cmd.exe process which launched the batch file.
>
"Gidi" wrote:
>
The thing is that my batch file has a certain flow, and i've a question which
detrmain which flow to choose.
so i need to read line by line, and when i get to the question, i want to
display the question to the user and wait for his answer, and send it back to
the batch file and continue to the right flow.

this is my batch file:

@ECHO OFF

ECHO "GOOD MORNING %1%"
:ASK
SET /P ANSWER="Do you want to continue (y/n) ? "
IF "%ANSWER%" == "y" GOTO PRESS_YES
IF "%ANSWER%" == "Y" GOTO PRESS_YES
IF "%ANSWER%" == "n" GOTO PRESS_NO
IF "%ANSWER%" == "N" GOTO PRESS_NO
GOTO ASK

:PRESS_YES
exit 0

:PRESS_NO
exit 1

how can i send the batch file the answer i got?

thanks,
Gidi.

"Family Tree Mike" wrote:

If you know how to read in all the standard output, then just split the
output on newline characters. You then would have the questions and
responses. Perhaps I'm not understanding where you are having the problem.
You should include a small set of code that runs the batch file and reads the
output clarifying where your concern is.
>
"Gidi" wrote:
>
Hi,

In my windows applicationm, i need to excute a batch file.
this batch file throws some text and questions to the screen, i need to
catch the standard Output, check if it's a question, in case it's a question,
i want to popup a messageBox or something, and bring back to the batch file
the result (Yes\No question). I know how to excute the batch file and get all
the Standard output at the end, but i don't know who can i read it line by
line and check if it's a question, and in case it is, bring the result to the
batch file back.

can anyone help me here?

Thanks,
Gidi.
Mar 25 '08 #13

P: n/a
On Tue, 25 Mar 2008 09:55:19 -0700, Peter Duniho
<Np*********@nnowslpianmk.comwrote:
[...]
In other words, for best results, you should create such a
concise-but-complete sample and post it here.
My apologies. I had forgotten that you'd already posted a batch file that
could be used to test the code. My suggestion was obviously not useful,
given that.

Unfortunately, I see that Mike was unable to reproduce the problem using
that batch file, and I don't have any suggestions other than what he's
already offered. You may have to look a little harder to find out what's
specifically different about your configuration that could be causing the
unexpected behavior.

Pete
Mar 25 '08 #14

P: n/a
Hi Mike,

i found this article http://www.codeproject.com/KB/cs/ProcessStartDemo.aspx
and it explains how to do what i need.

the code there is very similar to the one you gave me, but there are still
some problems there.
now i'm not running batch file, but exe file which i created in c++, and
displays output and waits for input. if i use the WaitForExit() function, the
process is stuck, if i'm not using it, the process finishes without any
output or input.

do you have any idea?

Thanks again
Gidi.

first

"Family Tree Mike" wrote:
I have no idea, as it is working for me using your batch file as provided in
your earlier message, and my code as provided in my earlier message.
"Gidi" wrote:
Hi,

thanks so much for the help.
i'm still having problem reading the question line (SET /P ANSWER="Do you
want to continue (y/n) ? ").

the program is stuck at this point.
if i set the pepprocess.StartInfo.RedirectStandardInput to false, so it
works (regardless what the answer was).

do you have any idea?

Thanks so much,
Gidi.
"Family Tree Mike" wrote:
I believe this code will produce the results you want, but I believe there
are better alternatives to this...:
>
Process pepprocess = new Process();
>
pepprocess.StartInfo.FileName = "cmd.exe";
pepprocess.StartInfo.Arguments = "/c helloworld.bat";
pepprocess.StartInfo.UseShellExecute = false;
pepprocess.StartInfo.RedirectStandardInput = true;
pepprocess.StartInfo.RedirectStandardOutput = true;
pepprocess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
pepprocess.StartInfo.CreateNoWindow = true;
pepprocess.Start();
>
StreamWriter sw = pepprocess.StandardInput;
StreamReader rd = pepprocess.StandardOutput;
>
while (!rd.EndOfStream)
{
string line = rd.ReadLine ();
>
>
richTextBox1.Text = line;
if (line.Contains ( "?" ))
{
sw.WriteLine ( "n" );
}
}
>
MessageBox.Show ( string.Format ( "{0}",
pepprocess.ExitCode.ToString () ) );
>
>
"Family Tree Mike" wrote:
>
Here are a few things to change:

1. Batchfiles cannot be the executable process so I'm surprised it
started... I would change the code to do the following:
peprocess.StartInfo.FileName = "cmd.exe";
peprocess.StartInfo.Arguments = "helloworld.bat"

2. Usually the waiting batch prompt is waiting for text and a newline.
Change your statement inside the if (line.contains("?")) to:
sw.WriteLine("y");


"Gidi" wrote:

Hi,
>
first thanks for the help...
>
second, at the end, i won't run batch but another exe file, which i've to
use since it's too much work to rewrite all over again to C#...
>
this is my code for now (while testing it):
>
private void button1_Click(object sender, EventArgs e)
{
Process pepprocess = new Process();
pepprocess.StartInfo.FileName = "helloworld.bat";
pepprocess.StartInfo.UseShellExecute = false;
pepprocess.StartInfo.RedirectStandardInput = true;
pepprocess.StartInfo.RedirectStandardOutput = true;
pepprocess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
pepprocess.Start();
>
StreamWriter sw = pepprocess.StandardInput;
StreamReader rd = pepprocess.StandardOutput;
>
string line = rd.ReadToEnd();
>
richTextBox1.Text = line;
if (line.Contains("?") == true)
{
sw.Write("y");
}
>
>
Thread.Sleep(1000);
>
>
sw.Flush();
>
}
>
i tried to read it line by line, but after the first line, the command line
is stuck, and in addition, altough i set the window style to hidden, i still
get the cmd window.
>
Thanks,
Gidi.
>
"Family Tree Mike" wrote:
>
My recommendation is to not use a batch file at all. If you know the flow
then rewrite the batch file as a routine in your code. If you are required
to use the batch file, then you need to look at redirecting standard input to
the cmd.exe process which launched the batch file.

"Gidi" wrote:

The thing is that my batch file has a certain flow, and i've a question which
detrmain which flow to choose.
so i need to read line by line, and when i get to the question, i want to
display the question to the user and wait for his answer, and send it back to
the batch file and continue to the right flow.
>
this is my batch file:
>
@ECHO OFF
>
ECHO "GOOD MORNING %1%"
:ASK
SET /P ANSWER="Do you want to continue (y/n) ? "
IF "%ANSWER%" == "y" GOTO PRESS_YES
IF "%ANSWER%" == "Y" GOTO PRESS_YES
IF "%ANSWER%" == "n" GOTO PRESS_NO
IF "%ANSWER%" == "N" GOTO PRESS_NO
GOTO ASK
>
:PRESS_YES
exit 0
>
:PRESS_NO
exit 1
>
how can i send the batch file the answer i got?
>
thanks,
Gidi.
>
"Family Tree Mike" wrote:
>
If you know how to read in all the standard output, then just split the
output on newline characters. You then would have the questions and
responses. Perhaps I'm not understanding where you are having the problem.
You should include a small set of code that runs the batch file and reads the
output clarifying where your concern is.

"Gidi" wrote:

Hi,
>
In my windows applicationm, i need to excute a batch file.
this batch file throws some text and questions to the screen, i need to
catch the standard Output, check if it's a question, in case it's a question,
i want to popup a messageBox or something, and bring back to the batch file
the result (Yes\No question). I know how to excute the batch file and get all
the Standard output at the end, but i don't know who can i read it line by
line and check if it's a question, and in case it is, bring the result to the
batch file back.
>
can anyone help me here?
>
Thanks,
Gidi.
Mar 26 '08 #15

This discussion thread is closed

Replies have been disabled for this discussion.