run batch file with Standard Output and Input

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?R2lkaQ==?=

    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.
  • Gilles Kohl [MVP]

    #2
    Re: run batch file with Standard Output and Input

    On Mon, 24 Mar 2008 02:56:00 -0700, Gidi <shnapsi@hotmai l.com.dontspamw rote:
    >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 ...



    Regards,
    Gilles.

    Comment

    • =?Utf-8?B?R2lkaQ==?=

      #3
      Re: run batch file with Standard Output and Input

      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 <shnapsi@hotmai l.com.dontspamw rote:
      >
      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 ...
      >

      >
      Regards,
      Gilles.
      >
      >

      Comment

      • =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?=

        #4
        RE: run batch file with Standard Output and Input

        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.

        Comment

        • Gilles Kohl [MVP]

          #5
          Re: run batch file with Standard Output and Input

          On Mon, 24 Mar 2008 04:15:00 -0700, Gidi <shnapsi@hotmai l.com.dontspamw rote:
          >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:

          ProcessStartInf o startInfo = new ProcessStartInf o(@"c:\tmp\test .bat");
          startInfo.Redir ectStandardInpu t = true;
          startInfo.Redir ectStandardErro r = true;
          startInfo.Redir ectStandardOutp ut = true;

          startInfo.UseSh ellExecute = false;
          startInfo.Creat eNoWindow = true;

          m_TheBatch = new Process();
          m_TheBatch.Star tInfo = startInfo;

          m_TheBatch.Outp utDataReceived += new
          DataReceivedEve ntHandler(theBa tch_OutputDataR eceived);
          m_TheBatch.Star t();
          m_TheBatch.Begi nOutputReadLine ();

          (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_Output DataReceived(ob ject sender, DataReceivedEve ntArgs e)
          {
          if(e.Data == null)
          {
          return;
          }

          string lineRead = e.Data;

          if(lineRead.Con tains("enter first name now"))
          {
          m_TheBatch.Stan dardInput.Write ("Gidi\r");
          }
          }

          A problem you'll encounter is that OutputDataRecei ved 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 OutputDataRecei ved 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.

          Comment

          • =?Utf-8?B?R2lkaQ==?=

            #6
            RE: run batch file with Standard Output and Input

            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.

            Comment

            • =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?=

              #7
              RE: run batch file with Standard Output and Input

              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.

              Comment

              • =?Utf-8?B?R2lkaQ==?=

                #8
                RE: run batch file with Standard Output and Input

                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(o bject sender, EventArgs e)
                {
                Process pepprocess = new Process();
                pepprocess.Star tInfo.FileName = "helloworld.bat ";
                pepprocess.Star tInfo.UseShellE xecute = false;
                pepprocess.Star tInfo.RedirectS tandardInput = true;
                pepprocess.Star tInfo.RedirectS tandardOutput = true;
                pepprocess.Star tInfo.WindowSty le = ProcessWindowSt yle.Hidden;
                pepprocess.Star t();

                StreamWriter sw = pepprocess.Stan dardInput;
                StreamReader rd = pepprocess.Stan dardOutput;

                string line = rd.ReadToEnd();

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


                Thread.Sleep(10 00);


                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.

                Comment

                • =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?=

                  #9
                  RE: run batch file with Standard Output and Input

                  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.Start Info.FileName = "cmd.exe";
                  peprocess.Start Info.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(o bject sender, EventArgs e)
                  {
                  Process pepprocess = new Process();
                  pepprocess.Star tInfo.FileName = "helloworld.bat ";
                  pepprocess.Star tInfo.UseShellE xecute = false;
                  pepprocess.Star tInfo.RedirectS tandardInput = true;
                  pepprocess.Star tInfo.RedirectS tandardOutput = true;
                  pepprocess.Star tInfo.WindowSty le = ProcessWindowSt yle.Hidden;
                  pepprocess.Star t();
                  >
                  StreamWriter sw = pepprocess.Stan dardInput;
                  StreamReader rd = pepprocess.Stan dardOutput;
                  >
                  string line = rd.ReadToEnd();
                  >
                  richTextBox1.Te xt = line;
                  if (line.Contains( "?") == true)
                  {
                  sw.Write("y");
                  }
                  >
                  >
                  Thread.Sleep(10 00);
                  >
                  >
                  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.

                  Comment

                  • =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?=

                    #10
                    RE: run batch file with Standard Output and Input

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

                    Process pepprocess = new Process();

                    pepprocess.Star tInfo.FileName = "cmd.exe";
                    pepprocess.Star tInfo.Arguments = "/c helloworld.bat" ;
                    pepprocess.Star tInfo.UseShellE xecute = false;
                    pepprocess.Star tInfo.RedirectS tandardInput = true;
                    pepprocess.Star tInfo.RedirectS tandardOutput = true;
                    pepprocess.Star tInfo.WindowSty le = ProcessWindowSt yle.Hidden;
                    pepprocess.Star tInfo.CreateNoW indow = true;
                    pepprocess.Star t();

                    StreamWriter sw = pepprocess.Stan dardInput;
                    StreamReader rd = pepprocess.Stan dardOutput;

                    while (!rd.EndOfStrea m)
                    {
                    string line = rd.ReadLine ();


                    richTextBox1.Te xt = line;
                    if (line.Contains ( "?" ))
                    {
                    sw.WriteLine ( "n" );
                    }
                    }

                    MessageBox.Show ( string.Format ( "{0}",
                    pepprocess.Exit Code.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.Start Info.FileName = "cmd.exe";
                    peprocess.Start Info.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(o bject sender, EventArgs e)
                    {
                    Process pepprocess = new Process();
                    pepprocess.Star tInfo.FileName = "helloworld.bat ";
                    pepprocess.Star tInfo.UseShellE xecute = false;
                    pepprocess.Star tInfo.RedirectS tandardInput = true;
                    pepprocess.Star tInfo.RedirectS tandardOutput = true;
                    pepprocess.Star tInfo.WindowSty le = ProcessWindowSt yle.Hidden;
                    pepprocess.Star t();

                    StreamWriter sw = pepprocess.Stan dardInput;
                    StreamReader rd = pepprocess.Stan dardOutput;

                    string line = rd.ReadToEnd();

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


                    Thread.Sleep(10 00);


                    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.

                    Comment

                    • =?Utf-8?B?R2lkaQ==?=

                      #11
                      RE: run batch file with Standard Output and Input

                      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.Star tInfo.RedirectS tandardInput 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.Star tInfo.FileName = "cmd.exe";
                      pepprocess.Star tInfo.Arguments = "/c helloworld.bat" ;
                      pepprocess.Star tInfo.UseShellE xecute = false;
                      pepprocess.Star tInfo.RedirectS tandardInput = true;
                      pepprocess.Star tInfo.RedirectS tandardOutput = true;
                      pepprocess.Star tInfo.WindowSty le = ProcessWindowSt yle.Hidden;
                      pepprocess.Star tInfo.CreateNoW indow = true;
                      pepprocess.Star t();
                      >
                      StreamWriter sw = pepprocess.Stan dardInput;
                      StreamReader rd = pepprocess.Stan dardOutput;
                      >
                      while (!rd.EndOfStrea m)
                      {
                      string line = rd.ReadLine ();
                      >
                      >
                      richTextBox1.Te xt = line;
                      if (line.Contains ( "?" ))
                      {
                      sw.WriteLine ( "n" );
                      }
                      }
                      >
                      MessageBox.Show ( string.Format ( "{0}",
                      pepprocess.Exit Code.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.Start Info.FileName = "cmd.exe";
                      peprocess.Start Info.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(o bject sender, EventArgs e)
                      {
                      Process pepprocess = new Process();
                      pepprocess.Star tInfo.FileName = "helloworld.bat ";
                      pepprocess.Star tInfo.UseShellE xecute = false;
                      pepprocess.Star tInfo.RedirectS tandardInput = true;
                      pepprocess.Star tInfo.RedirectS tandardOutput = true;
                      pepprocess.Star tInfo.WindowSty le = ProcessWindowSt yle.Hidden;
                      pepprocess.Star t();
                      >
                      StreamWriter sw = pepprocess.Stan dardInput;
                      StreamReader rd = pepprocess.Stan dardOutput;
                      >
                      string line = rd.ReadToEnd();
                      >
                      richTextBox1.Te xt = line;
                      if (line.Contains( "?") == true)
                      {
                      sw.Write("y");
                      }
                      >
                      >
                      Thread.Sleep(10 00);
                      >
                      >
                      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.

                      Comment

                      • Peter Duniho

                        #12
                        Re: run batch file with Standard Output and Input

                        On Tue, 25 Mar 2008 02:40:01 -0700, Gidi <shnapsi@hotmai l.com.dontspam
                        wrote:
                        [...]
                        while (!rd.EndOfStrea m)
                        {
                        string line = rd.ReadLine();
                        >
                        >
                        richTextBox1.Te xt = 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

                        Comment

                        • =?Utf-8?B?RmFtaWx5IFRyZWUgTWlrZQ==?=

                          #13
                          RE: run batch file with Standard Output and Input

                          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.Star tInfo.RedirectS tandardInput 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.Star tInfo.FileName = "cmd.exe";
                          pepprocess.Star tInfo.Arguments = "/c helloworld.bat" ;
                          pepprocess.Star tInfo.UseShellE xecute = false;
                          pepprocess.Star tInfo.RedirectS tandardInput = true;
                          pepprocess.Star tInfo.RedirectS tandardOutput = true;
                          pepprocess.Star tInfo.WindowSty le = ProcessWindowSt yle.Hidden;
                          pepprocess.Star tInfo.CreateNoW indow = true;
                          pepprocess.Star t();

                          StreamWriter sw = pepprocess.Stan dardInput;
                          StreamReader rd = pepprocess.Stan dardOutput;

                          while (!rd.EndOfStrea m)
                          {
                          string line = rd.ReadLine ();


                          richTextBox1.Te xt = line;
                          if (line.Contains ( "?" ))
                          {
                          sw.WriteLine ( "n" );
                          }
                          }

                          MessageBox.Show ( string.Format ( "{0}",
                          pepprocess.Exit Code.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.Start Info.FileName = "cmd.exe";
                          peprocess.Start Info.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(o bject sender, EventArgs e)
                          {
                          Process pepprocess = new Process();
                          pepprocess.Star tInfo.FileName = "helloworld.bat ";
                          pepprocess.Star tInfo.UseShellE xecute = false;
                          pepprocess.Star tInfo.RedirectS tandardInput = true;
                          pepprocess.Star tInfo.RedirectS tandardOutput = true;
                          pepprocess.Star tInfo.WindowSty le = ProcessWindowSt yle.Hidden;
                          pepprocess.Star t();

                          StreamWriter sw = pepprocess.Stan dardInput;
                          StreamReader rd = pepprocess.Stan dardOutput;

                          string line = rd.ReadToEnd();

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


                          Thread.Sleep(10 00);


                          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.

                          Comment

                          • Peter Duniho

                            #14
                            Re: run batch file with Standard Output and Input

                            On Tue, 25 Mar 2008 09:55:19 -0700, Peter Duniho
                            <NpOeStPeAdM@nn owslpianmk.comw rote:
                            [...]
                            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

                            Comment

                            • =?Utf-8?B?R2lkaQ==?=

                              #15
                              RE: run batch file with Standard Output and Input

                              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.Star tInfo.RedirectS tandardInput 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.Star tInfo.FileName = "cmd.exe";
                              pepprocess.Star tInfo.Arguments = "/c helloworld.bat" ;
                              pepprocess.Star tInfo.UseShellE xecute = false;
                              pepprocess.Star tInfo.RedirectS tandardInput = true;
                              pepprocess.Star tInfo.RedirectS tandardOutput = true;
                              pepprocess.Star tInfo.WindowSty le = ProcessWindowSt yle.Hidden;
                              pepprocess.Star tInfo.CreateNoW indow = true;
                              pepprocess.Star t();
                              >
                              StreamWriter sw = pepprocess.Stan dardInput;
                              StreamReader rd = pepprocess.Stan dardOutput;
                              >
                              while (!rd.EndOfStrea m)
                              {
                              string line = rd.ReadLine ();
                              >
                              >
                              richTextBox1.Te xt = line;
                              if (line.Contains ( "?" ))
                              {
                              sw.WriteLine ( "n" );
                              }
                              }
                              >
                              MessageBox.Show ( string.Format ( "{0}",
                              pepprocess.Exit Code.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.Start Info.FileName = "cmd.exe";
                              peprocess.Start Info.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(o bject sender, EventArgs e)
                              {
                              Process pepprocess = new Process();
                              pepprocess.Star tInfo.FileName = "helloworld.bat ";
                              pepprocess.Star tInfo.UseShellE xecute = false;
                              pepprocess.Star tInfo.RedirectS tandardInput = true;
                              pepprocess.Star tInfo.RedirectS tandardOutput = true;
                              pepprocess.Star tInfo.WindowSty le = ProcessWindowSt yle.Hidden;
                              pepprocess.Star t();
                              >
                              StreamWriter sw = pepprocess.Stan dardInput;
                              StreamReader rd = pepprocess.Stan dardOutput;
                              >
                              string line = rd.ReadToEnd();
                              >
                              richTextBox1.Te xt = line;
                              if (line.Contains( "?") == true)
                              {
                              sw.Write("y");
                              }
                              >
                              >
                              Thread.Sleep(10 00);
                              >
                              >
                              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.

                              Comment

                              Working...