About Windows DOS Batch file writing common sense

Source: Internet
Author: User
Tags echo command

Redirect ' all ' output to a single file:

Run:

Test.bat > Test.txt 2>&1

And you'll get the This text on screen (we'll never get rid of this in screen, as it's sent to the Console and cannot be redirected):

This text goes to the Console

You should also get a file named Test.txt with the following content:

This text goes to standard outputthis text goes to standard Error
Note:THe commands
                test.bat  > test.txt 2>&1
                test.bat 1> test.txt 2>&1
            test.bat 2> test.txt 1>&2
All give identical results.


Redirect errors to a separate error log file:

Run:

Test.bat > Testlog.txt 2> testerrors.txt

And you'll get the This text on screen (we'll never get rid of this in screen, as it's sent to the Console and cannot be redirected):

This text goes to the Console

You should also get a file named Testlog.txt with the following content:

This text goes to standard Output

And another file named Testerrors.txt with the following content:

This text goes to standard Error


Some "Best practices" when using the redirection in batch files:
  • Use >filename.txt 2>&1 -to-merge standard Output and standard Error and redirect them together-a single file.
    Make sure to the redirection "commands" in this order.

  • Use the >logfile.txt 2>errorlog.txt redirect success and error messages to separate log files.

  • Use >CON to send text to the screen, no matter what, even if the batch file's output is redirected.
    This could was useful when prompting for input even if the batch file ' s output was being redirected to a file.

  • Use 1>&2 -to-send text to standard Error.
    This can is useful for error messages.

  • It ' s OK to use spaces in redirection commands. Note However, that a space between an ECHO command and a would be > redirected too.
    DIR>filename.txtAnd DIR > filename.txt is identical, ECHO Hello world>filename.txt and ECHO Hello world > filename.txt is not, even though they is both valid.
    It is a not OK to use spaces in or OR or >> 2> 2>&1 1>&2 (before or after is OK).

  • In Windows Nt 4, early Windows $ versions, and OS/2 there used to being some ambiguity with echoed lines Endin G with a 1 or 2, immediately followed by a  ; :
    ECHO Hello world2> file.txt  would result in an  empty  file  file.txt  and the text  Hello  world   (without the trailing "2") on screen (CMD. EXE would interpret it as  echo hello world 2> file.txt ).
    in Windows XP The result was no text on screen and  file.txt  containing the line  Hello&nbs P;world2 , including the trailing "2" (CMD. EXE interprets it as Echo hello world2 > file.txt ).
    to prevent the ambiguity, either use parentheses or insert an extra space yourself:
    ECHO Hello World2 > file.txt
    (ECHO Hello World2) file.txt

  • "Merging" standard Output and standard Error with 2>&1 can also is used to pipe a command's output to another CO Mmand ' s standard Input:
    somecommand 2>&1 | someothercommand



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.