Loading ...
Sorry, an error occurred while loading the content.
 

Re: [BATCH WORLD] Stdout, stderr in batch files

Expand Messages
  • Parag P. Doke
    Let us assume my batch file performs 5 actions... Before each of these actions, it echoes what will be performed. Can I direct the output of the first echo
    Message 1 of 11 , Dec 2, 2006
      Let us assume my batch file performs 5 actions...
      Before each of these actions, it echoes what will be performed. Can I direct
      the output of the first echo statement and that of the last one to 2
      different streams (1 & 2) without specifying the name of the files ?
      -----------------
      echo Starting %~nx0
      echo INFO: Performing action 1
      <command for action1>
      echo INFO: Performing action 2
      <command for action2>
      echo INFO: Performing action 3
      <command for action3>
      echo INFO: Performing action 4
      <command for action3>
      echo INFO: Performing action 5
      <command for action3>
      echo Exiting %~nx0
      :EOF
      -----------------
      I'm not sure if what I'm asking is syntactically correct, but can I have:
      echo Starting %~nx0 >>&1
      and
      echo INFO:..... >>&2

      -Parag P. Doke


      On 11/30/06, foxidrive <foxidrive@...> wrote:
      >
      > On Thu, 30 Nov 2006 12:43:12 +0530, "Parag P. Doke" <
      > paragpdoke@... <paragpdoke%40gmail.com>>
      > wrote:
      >
      > >Many thanks for the explanation. It is much clearer now.
      >
      > You're welcome.
      >
      > >I had 2 points to state:
      > >
      > >My cmd did not hang when I tried:
      > >-------------------
      > >batch.bat 2>&1 1>&3 3>&1
      > >-------------------
      > >And batch.bat contained:
      > >-------------------
      > >echo Hi
      > >
      > >-------------------
      >
      > I tried it just now - I don't understand the result.
      > It merely echoes to the screen, as if no redirection is used.
      >
      > >The second one is a question rather than a statement.
      > >It is possible to write a batch file that redirects its output to 2
      > streams
      > >?
      > >Meaning can I assign output of one echo statement to a stream and that of
      > >another to a new stream ?
      >
      > I'm not sure - what are you trying to achieve?
      >
      >
      >



      --
      Parag P. Doke
      http://paragpdoke.blogspot.com


      [Non-text portions of this message have been removed]
    • foxidrive
      On Sat, 2 Dec 2006 16:13:39 +0530, Parag P. Doke ... It results in the following: Q: echo Starting A.BAT 1 &1 The handle could not be duplicated during
      Message 2 of 11 , Dec 2, 2006
        On Sat, 2 Dec 2006 16:13:39 +0530, "Parag P. Doke"
        <paragpdoke@...> wrote:

        >Let us assume my batch file performs 5 actions...
        >Before each of these actions, it echoes what will be performed. Can I direct
        >the output of the first echo statement and that of the last one to 2
        >different streams (1 & 2) without specifying the name of the files ?
        >-----------------
        >echo Starting %~nx0
        >echo INFO: Performing action 1
        ><command for action1>
        >echo INFO: Performing action 2
        ><command for action2>
        >echo INFO: Performing action 3
        ><command for action3>
        >echo INFO: Performing action 4
        ><command for action3>
        >echo INFO: Performing action 5
        ><command for action3>
        >echo Exiting %~nx0
        >:EOF
        >-----------------
        >I'm not sure if what I'm asking is syntactically correct, but can I have:
        >echo Starting %~nx0 >>&1
        >and
        >echo INFO:..... >>&2

        It results in the following:

        Q:\>echo Starting A.BAT 1>>&1
        The handle could not be duplicated
        during redirection of handle 1.

        Q:\>echo INFO:..... 1>>&2
        INFO:.....

        Note that the implied standard out is filled in by cmd.exe with 1.

        All echo commands are always echoed to 'standard out', so the first
        one is asking 'standard out' to be redirected to 'standard out' and it
        ends in the error. The second command is fine and the text is echoed
        to 'standard error', which in the case above also goes to the console.
      • adoxe
        A little résumé regarding stdout and stderr... to output stdout to /dev/null simply redirect it to NUL ex. : reg add %KEY% /v value /t type /d data NUL for
        Message 3 of 11 , May 28, 2007
          A little résumé regarding stdout and stderr...

          to output stdout to /dev/null simply redirect it to NUL
          ex. : reg add %KEY% /v value /t type /d data > NUL
          for redirecting stderr to /dev/null
          ex. : reg add %KEY% /v value /t type /d data 2> NUL
          and for redirecting stderr to the same place as stdout, which is redirected
          to /dev/null
          ex. : reg add %KEY% /v value /t type /d data > NUL 2>&1
          also, you can redirect (and append) to files as well (and remember default
          redirection is to console)
          ex. : reg add %KEY% /v value /t type /d data >> worked_fine.txt 2>>
          went_wrong.txt

          I wish everything is now clear for everyone...
          if not, lemme know, I will add what I forgot... ;)

          bb

          foxidrive-5 wrote:
          >
          > On Sat, 2 Dec 2006 16:13:39 +0530, "Parag P. Doke"
          > <paragpdoke@...> wrote:
          >
          >>Let us assume my batch file performs 5 actions...
          >>Before each of these actions, it echoes what will be performed. Can I
          direct
          >>the output of the first echo statement and that of the last one to 2
          >>different streams (1 & 2) without specifying the name of the files ?
          >>-----------------
          >>echo Starting %~nx0
          >>echo INFO: Performing action 1
          >><command for action1>
          >>echo INFO: Performing action 2
          >><command for action2>
          >>echo INFO: Performing action 3
          >><command for action3>
          >>echo INFO: Performing action 4
          >><command for action3>
          >>echo INFO: Performing action 5
          >><command for action3>
          >>echo Exiting %~nx0
          >>:EOF
          >>-----------------
          >>I'm not sure if what I'm asking is syntactically correct, but can I have:
          >>echo Starting %~nx0 >>&1
          >>and
          >>echo INFO:..... >>&2
          >
          > It results in the following:
          >
          > Q:\>echo Starting A.BAT 1>>&1
          > The handle could not be duplicated
          > during redirection of handle 1.
          >
          > Q:\>echo INFO:..... 1>>&2
          > INFO:.....
          >
          > Note that the implied standard out is filled in by cmd.exe with 1.
          >
          > All echo commands are always echoed to 'standard out', so the first
          > one is asking 'standard out' to be redirected to 'standard out' and it
          > ends in the error. The second command is fine and the text is echoed
          > to 'standard error', which in the case above also goes to the console.
          >
          >
          >

          --
          View this message in context: http://www.nabble.com/Stdout%2C-stderr-in-batch-files-tf2655405.html#a10837740
          Sent from the Batch World mailing list archive at Nabble.com.
        • Parag P. Doke
          Hello Everyone. Sorry for replying to a very old thread. But here s what happened when looking for a non-existent file: ===== C: tmp dir xxx.txt 1 output.txt
          Message 4 of 11 , Sep 4, 2007
            Hello Everyone.
            Sorry for replying to a very old thread.
            But here's what happened when looking for a non-existent file:
            =====
            C:\tmp>dir xxx.txt 1> output.txt 2> error.txt

            C:\tmp>type output.txt
            Volume in drive C is SYSTEM
            Volume Serial Number is 40F0-C785

            Directory of C:\tmp


            C:\tmp>type error.txt
            File Not Found

            C:\tmp>
            =====
            Then I created a new batch file mybatch.bat with the following 2 lines:
            =====
            @echo off
            echo Hi>&2
            =====
            Here's what happened when I tried it:
            =====
            C:\tmp>mybatch.bat
            Hi

            C:\tmp>mybatch.bat 1> null.txt
            Hi

            C:\tmp>type null.txt

            C:\tmp>mybatch.bat 2> bat_err.txt

            C:\tmp>type bat_err.txt
            Hi

            C:\tmp>
            =====
            Now I understand that if I want to display something only to the error
            stream, I need to use >&2 within my batch files.

            Thank you adoxe and Mic for your inputs.

            Best regards,
            Parag P. Doke
            http://paragpdoke.blogspot.com

            On 5/28/07, adoxe <nabble@...> wrote:
            >
            >
            > A little résumé regarding stdout and stderr...
            >
            > to output stdout to /dev/null simply redirect it to NUL
            > ex. : reg add %KEY% /v value /t type /d data > NUL
            > for redirecting stderr to /dev/null
            > ex. : reg add %KEY% /v value /t type /d data 2> NUL
            > and for redirecting stderr to the same place as stdout, which is
            > redirected
            > to /dev/null
            > ex. : reg add %KEY% /v value /t type /d data > NUL 2>&1
            > also, you can redirect (and append) to files as well (and remember default
            > redirection is to console)
            > ex. : reg add %KEY% /v value /t type /d data >> worked_fine.txt 2>>
            > went_wrong.txt
            >
            > I wish everything is now clear for everyone...
            > if not, lemme know, I will add what I forgot... ;)
            >
            > bb
            >
            >
            > foxidrive-5 wrote:
            > >
            > > On Sat, 2 Dec 2006 16:13:39 +0530, "Parag P. Doke"
            > > <paragpdoke@... <paragpdoke%40gmail.com>> wrote:
            > >
            > >>Let us assume my batch file performs 5 actions...
            > >>Before each of these actions, it echoes what will be performed. Can I
            > direct
            > >>the output of the first echo statement and that of the last one to 2
            > >>different streams (1 & 2) without specifying the name of the files ?
            > >>-----------------
            > >>echo Starting %~nx0
            > >>echo INFO: Performing action 1
            > >><command for action1>
            > >>echo INFO: Performing action 2
            > >><command for action2>
            > >>echo INFO: Performing action 3
            > >><command for action3>
            > >>echo INFO: Performing action 4
            > >><command for action3>
            > >>echo INFO: Performing action 5
            > >><command for action3>
            > >>echo Exiting %~nx0
            > >>:EOF
            > >>-----------------
            > >>I'm not sure if what I'm asking is syntactically correct, but can I
            > have:
            > >>echo Starting %~nx0 >>&1
            > >>and
            > >>echo INFO:..... >>&2
            > >
            > > It results in the following:
            > >
            > > Q:\>echo Starting A.BAT 1>>&1
            > > The handle could not be duplicated
            > > during redirection of handle 1.
            > >
            > > Q:\>echo INFO:..... 1>>&2
            > > INFO:.....
            > >
            > > Note that the implied standard out is filled in by cmd.exe with 1.
            > >
            > > All echo commands are always echoed to 'standard out', so the first
            > > one is asking 'standard out' to be redirected to 'standard out' and it
            > > ends in the error. The second command is fine and the text is echoed
            > > to 'standard error', which in the case above also goes to the console.
            > >
            > >
            > >
            >
            > --
            > View this message in context: http://www.nabble.com/Stdout
            > %2C-stderr-in-batch-files-tf2655405.html#a10837740
            > Sent from the Batch World mailing list archive at Nabble.com.
            >
            >
            >



            --
            Parag P. Doke
            http://paragpdoke.blogspot.com


            [Non-text portions of this message have been removed]
          • foxidrive
            On Tue, 4 Sep 2007 20:43:01 +0530, Parag P. Doke ... Here is a method of using stdout and stderr, see if it makes sense. Your method
            Message 5 of 11 , Sep 4, 2007
              On Tue, 4 Sep 2007 20:43:01 +0530, "Parag P. Doke" <paragpdoke@...>
              wrote:

              >Hello Everyone.
              >Sorry for replying to a very old thread.

              >Then I created a new batch file mybatch.bat with the following 2 lines:
              >=====
              >@echo off
              >echo Hi>&2
              >=====
              >Here's what happened when I tried it:
              >=====
              >C:\tmp>mybatch.bat
              >Hi
              >
              >C:\tmp>mybatch.bat 1> null.txt
              >Hi
              >
              >C:\tmp>type null.txt
              >
              >C:\tmp>mybatch.bat 2> bat_err.txt
              >
              >C:\tmp>type bat_err.txt
              >Hi
              >
              >C:\tmp>
              >=====
              >Now I understand that if I want to display something only to the error
              >stream, I need to use >&2 within my batch files.

              Here is a method of using stdout and stderr, see if it makes sense.
              Your method is unusual.

              ::hi.bat
              @echo off
              echo Hi

              M:\>hi.bat
              Hi

              M:\>hi.bat 1>output.txt

              M:\>type output.txt
              Hi

              M:\>hi.bat 2>output.txt
              Hi

              M:\>type output.txt

              M:\>hi.bat 2>output.txt 1>&2

              M:\>type output.txt
              Hi
            Your message has been successfully submitted and would be delivered to recipients shortly.