473,372 Members | 1,020 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

run batch file with Standard Output and Input

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
14 12730
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
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
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
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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Zeya | last post by:
I have created a very simple batch file and trying to retrieve the standard output but everytime I run the code it returns ExitCode as 1. I have created a batch file as simple as ping localhost...
3
by: emman_54 | last post by:
Hi every one, I am trying to run a batch file using my asp.net application. I am using the Process class to run the batch file. When I run my web application, In the task manager, i could see...
1
by: mvdkwong | last post by:
What's the trick to running a batch file from VBA? I'm trying to call it using the Shell function but it's not working for me. If I double-click the batch file or run it from the command line it...
1
by: steve | last post by:
Hi all, Here's some work in progress that should allow you to run a batch file as a custom action in a VS deployment project. Yup I know you can use js or wsh, but the target may not have...
0
by: Elroyskimms | last post by:
I need to execute a batch file via ASP.Net. In my VB.Net code, I'm using System.Diagnostics.Process to call the batch file and its appropriate command line arguments. I'm using...
4
by: ed | last post by:
Hi all, I'm very new to vb (2nd day) and I need to create a small app that will replace my old batch file with a flashy gui. I had some experience with access 2.0 which helps ;) What I would...
7
by: Nananana | last post by:
Hi. I have 2 DB2 statements: connect select How can I create a batch file and redirect the output to a file? I would like something like this:
4
by: mitymouse | last post by:
I am fairly familiar with scripting dos commands and that's about it. I remember some syntax and whatnot from a few programming classes I've taken but I don't remember any of the modules or functions...
2
by: Devine123 | last post by:
Hi, Iím creating a perl script that takes incoming http/s requests, logs the standard input, output and error, before returning the output to the client. The input, output and error log is appended...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.