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

Author Topic: ">" and ">>" in Batch files and parameters to them  (Read 4120 times)

0 Members and 1 Guest are viewing this topic.

tboyerva

    Topic Starter


    Rookie

    • Experience: Expert
    • OS: Windows 7
    ">" and ">>" in Batch files and parameters to them
    « on: March 14, 2016, 09:01:23 AM »
    Hi all,

    Unfortunately the search engine does not allow me to search for ">" and ">>", so I apologize if this is a duplicate post.

    I am modifying someone else's BAT and the ">>" style is used for appending to a LOG file.

    My issue is that I am not finding any documentation regarding this style and I'm not sure how to interpret lines like the following:

    Quote
    >>out_log.log 2>&1 copy /Y %1 somefile.txt

    So, I see the name of the LOG file and that there is a COPY going on of "somefile.txt". I also see that %1 is the first parameter to my BAT file, but I really have no idea about the "2>&1" means.

    Your help is appreciated.

    foxidrive



      Specialist
    • Thanked: 268
    • Experience: Experienced
    • OS: Windows 8
    Re: ">" and ">>" in Batch files and parameters to them
    « Reply #1 on: March 14, 2016, 11:54:52 AM »
    Code: [Select]
    >>out_log.log 2>&1 copy /Y %1 somefile.txt

    EDIT: Your example is legitimate syntax, it's just not often written with both redirections at the beginning. It's the same as this code:
    Code: [Select]
    copy /Y %1 somefile.txt >>out_log.log 2>&1

    In the example below are two kinds of output - the screen output and the error output.  They are both seen on the screen in normal use, but when redirecting output to a file you will get the screen output, but not the error messages.

    They call these things streams, and STDOUT is the normal screen output while STDERR is where errors are generally written to.

    STDOUT is represented by number 1
    STDERR  is represented by number 2

    Code: [Select]
    dir 1>>out_log.log 2>&1
    The code above shows the DIR output being appended to the file >>out_log.log and while I added the 1, you will always see the 1 on the console if you use echo on with the batch file.

    The portion 2>&1 shows that the STDERR stream (number 2) is being sent to the same place as number 1 (>&1) and number 1 has been set to go to the file out_log.log.

    So what the command does is send both the screen output and the error messages to the file out_log.log.

    You will never see >> in the 2>&1 syntax, it is always just one > character.


    This example will send the DIR 1 output to the NUL device and it will disappear, and STDERR is being sent to stream 1 so it disappears also.  No screen output will show at all.

    Code: [Select]
    dir >nul 2>&1

    In this example the DIR output goes to the file, and all error messages are hidden.

    Code: [Select]
    dir >file.txt 2>nul

    Lastly, there are 9 streams but 1 and 2 are the ones that are most often used in code.
    « Last Edit: March 14, 2016, 12:15:00 PM by foxidrive »

    tboyerva

      Topic Starter


      Rookie

      • Experience: Expert
      • OS: Windows 7
      Re: ">" and ">>" in Batch files and parameters to them
      « Reply #2 on: March 14, 2016, 12:51:53 PM »
      Thanks so much. It has been some 30 years since I had to write or modify batch files.