Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: [SOLVED] redirecting output  (Read 11384 times)

0 Members and 1 Guest are viewing this topic.

Two-eyes

    Topic Starter


    Intermediate
  • Thanked: 4
    [SOLVED] redirecting output
    « on: September 11, 2009, 01:25:49 PM »
    Hi, I've been browsing and searching for this for 1/2 an hour or so, but I still don't get it.
    I have this batch file but isn't working.  From what I found, it should be correct, but.....

    Here's the code:
    Code: [Select]
    @echo off

    echo hiya 1>doesntexist\blabla.txt 2>error.txt

    When I run is from command prompt:
    Code: [Select]
    c:\> test.bat
    The system cannot find the path specified

    Shouldn't the error message be written in error.txt?  Or did I understand wrong?

    Thanks for any help

    Two-eyes
    « Last Edit: September 12, 2009, 07:59:14 AM by Two-eyes »
    Quote
    I believe the bushes in my yard will BURN before God picks up a PC to send a message


    macdad-



      Expert

      Thanked: 40
      Re: redirecting output
      « Reply #1 on: September 11, 2009, 05:08:02 PM »
      CMD always displays the errors in batch files, whenever it hits an error, it terminates the batch file
      If you dont know DOS, you dont know Windows...

      Thats why Bill Gates created the Windows NT Family.

      billrich

      • Guest
      Re: redirecting output
      « Reply #2 on: September 11, 2009, 06:25:29 PM »
      Two Eyes wrote:

      <<"Here's the code:
      Code: [Select]
      @echo off

       
      ">>

      Test the redirect at command prompt.


      C:\>cd  doesntexist

      C:\doesntexist>dir
       Volume in drive C has no label.
       Volume Serial Number is F4A3-D6B3

       Directory of C:\doesntexist

      09/11/2009  07:23 PM    <DIR>          .
      09/11/2009  07:23 PM    <DIR>          ..
      09/11/2009  07:23 PM                 7 blabla.txt
                     1 File(s)              7 bytes
                     2 Dir(s)  306,860,113,920 bytes free

      C:\doesntexist>type blabla.txt
      hiya

      C:\doesntexist>


      Two-eyes

        Topic Starter


        Intermediate
      • Thanked: 4
        Re: redirecting output
        « Reply #3 on: September 12, 2009, 02:25:17 AM »
        @macdad-: ...should there have been a fullstop/period between "errors" and in batch files".  If that's the case, I understand.  Is there a work around or a way to continue the batch file? I believe I can evaluate %errorlevel%, i.e.
        Code: [Select]
        IF %errorlevel% NEQ 0 ECHO error %errorlevel% occurred>error.txt
        But how do I get the actual message?

        @billrich: the directory "doesnt exist" does not exist, ergo the name.  So it should output the error "the system couldnt find path specified".  But I want to capture that error message, and print it in error.txt.

        Thanks Two-eyes
        Quote
        I believe the bushes in my yard will BURN before God picks up a PC to send a message


        Salmon Trout

        • Guest
        Re: redirecting output
        « Reply #4 on: September 12, 2009, 02:51:54 AM »
        @macdad-: ...should there have been a fullstop/period between "errors" and in batch files".  If that's the case, I understand. 

        Quote from: macdad
        CMD always displays the errors in batch files, whenever it hits an error, it terminates the batch file

        No; the sentence makes perfect sense as it stands. The first clause states that CMD always displays the errors caused when batch files are run. The second and third, which are linked, state that when it [cmd.exe] encounters an error, [any error] the batch file is terminated. Perhaps I would have put a semicolon after 'files', but to make that a criticism would be excessively picky in my opinion.

        T.C.



          Beginner

          Thanked: 13
          Re: redirecting output
          « Reply #5 on: September 12, 2009, 02:55:55 AM »
          CMD always displays the errors in batch files, whenever it hits an error, it terminates the batch file

          That's incorrect and a very dangerous assumption.  In lots of cases CMD issues the error message and continues with the next command.  Try this:

          Code: [Select]
          @echo off
          cls

          pushd doesntexist\

          dir /a-d

          The directory listing is produced for the default directory not the directory which was intended in the Pushd command.  If, instead of dir, the command had been del all sorts of brown stuff might occur.  With commands like Pushd and CD IMHO it's always best to use e.g.:

          Code: [Select]
          Pushd doesntexist\ || echo Error: Pushd failed - job terminated. && exit/b

          Salmon Trout

          • Guest
          Re: redirecting output
          « Reply #6 on: September 12, 2009, 03:24:31 AM »
          test.bat:
          Code: [Select]
          @echo off
          echo hiya > doesntexist\blabla.txt

          (1)
          Console:
          Code: [Select]
          S:\>test.bat
          The system cannot find the path specified.
          S:\>

          (2)
          Console:
          Code: [Select]
          S:\>test.bat > err.txt
          The system cannot find the path specified.
          S:\>

          err.txt:
          Code: [Select]


          (3)
          Console:
          Code: [Select]
          S:\>test.bat 2> err.txt
          S:\>

          err.txt:
          Code: [Select]
          The system cannot find the path specified.




          Two-eyes

            Topic Starter


            Intermediate
          • Thanked: 4
            Re: redirecting output
            « Reply #7 on: September 12, 2009, 04:28:27 AM »
            @ Salmon Trout: aaaaaaah, I see.  You put one redirection in the batch file, and the other in the console.  That solves it.

            Just a quick question: was my syntax wrong, i.e. I can't put two redirections in a line.  Cos I can do
            Code: [Select]
            something > myfile.txt 2>&1
            and that's two redirections, unless I'm not understanding this well.

            Thanks
            Two-eyes
            Quote
            I believe the bushes in my yard will BURN before God picks up a PC to send a message


            Salmon Trout

            • Guest
            Re: redirecting output
            « Reply #8 on: September 12, 2009, 04:34:15 AM »
            I think the answer is that the 2> and  &1 notation is for programs & scripts that write error messages to stderr and console messages to stdout, so that we may combine stderr and stdout into one stream, but not for cmd.exe internal commands. An error in one of these stops the show so you have to do it the way I showed.

             

            Two-eyes

              Topic Starter


              Intermediate
            • Thanked: 4
              Re: redirecting output
              « Reply #9 on: September 12, 2009, 04:42:18 AM »
              Quote
              is the simplest way I can think of putting it
              Thanks....I'm new to this :)

              When you say chain, do you mean
               1) it won't continue reading the current line ">myfile.txt 2>&1"
              or
               2) it won't continue reading the next lines of code(if this were a batch file)

              T-E
              Quote
              I believe the bushes in my yard will BURN before God picks up a PC to send a message


              Salmon Trout

              • Guest
              Re: redirecting output
              « Reply #10 on: September 12, 2009, 05:23:51 AM »
              1) it won't continue reading the current line ">myfile.txt 2>&1"

              Two-eyes

                Topic Starter


                Intermediate
              • Thanked: 4
                Re: redirecting output
                « Reply #11 on: September 12, 2009, 05:34:04 AM »
                But if it won't read that line, how will it send the error message to myfile.txt.  Or will it jump to the "2>"?

                I also re-ask the question: can i put two redirections in a line of command?

                Sorry, but I am like this.  I always want to know what the compiler/executer is doing. ;)  It's just my fascination how simple electric pulses do such complicated things :)

                Thanks
                Quote
                I believe the bushes in my yard will BURN before God picks up a PC to send a message


                Salmon Trout

                • Guest
                Re: redirecting output
                « Reply #12 on: September 12, 2009, 06:19:10 AM »
                Code: [Select]
                (echo hello > \notexistdir\file.txt) 2>error.txt
                This works. You have to break the line into blocks using brackets.

                This forces cmd.exe to process the echo statement first.
                « Last Edit: September 12, 2009, 06:51:09 AM by Salmon Trout »

                Two-eyes

                  Topic Starter


                  Intermediate
                • Thanked: 4
                  Re: redirecting output
                  « Reply #13 on: September 12, 2009, 06:24:58 AM »
                  ahaaaaa.
                  Got it
                  Thanks :D
                  Quote
                  I believe the bushes in my yard will BURN before God picks up a PC to send a message


                  Salmon Trout

                  • Guest
                  Re: redirecting output
                  « Reply #14 on: September 12, 2009, 06:26:16 AM »
                  Break the line. See edited post above.

                  Alternative: (note alternative placements of redirection)

                  Code: [Select]
                  2>error.txt (echo hello > \nodir\nofile.txt)
                  also note these formats for redirecting multiple lines

                  Code: [Select]
                  (
                  echo line 1
                  echo line 2
                  echo line 3
                  ) > output.txt

                  Or...

                  Code: [Select]
                  > output.txt (
                  echo line 1
                  echo line 2
                  echo line 3
                  )

                  or for 1 line

                  Code: [Select]
                  > output.txt echo Hello!



                  « Last Edit: September 12, 2009, 06:51:48 AM by Salmon Trout »