[Zz] redirecting output to a file in Windows Batch scripts

Source: Internet
Author: User
Tags echo command
Redirecting output to a file in Windows Batch scripts November 14th, 2006 by Josh Batch scripting, Microsoft

Microsoft's command redirection operators page provides the factual information on how to do redirection, but actual implementation gets a bit confusing for anything beyond command> filename.txt. here's a rundown on how to do some useful things. i'm not going to cover every aspect of redirection, But I'll point you to the cases I find myself in most often.

The basics.You want to generate either a log file or a file that you will be executing after you're done generating it. you'll likely be using a combination of ECHO commands as well as some output from a handful of commands. welcome to the> and> operators. the first will send the output to a file and erase its existing contents; the second willAppendThe output to the file.

Examples:

# This will send the Dir Command's output to a fresh file:
Dir> myfile.txt

# Here I will create a fresh file with an echo command,
# And append the Dir output to that:
Echo here is a directory> myfile.txt
Dir> myfile.txt

Redirection for logging.This is a great way to make log files, which can even have the date and time with a simple improvement over that last example:

Echo % date %, % time %: listing a directory.> myfile.txt
Dir> myfile.txt

Using this technique, you can log not just what your Batch scripts are trying to do, but what the results are. for instance, let's say we're re generating a batch script that automatically creates accounts and drops them in a group. if I'm doing this in ad, I might have code like this:

# Add User "Joe" to the "mygroup" group.
Net group mygroup Joe/Add

To log the result of this, I might try:

Net group mygroup Joe/Add> addaccts. Log

Better error logging.When this command succeeds, it outputs, "this command completed successfully. "It will output this to the log file, and you know the net group command succeeded. however, if there were errors, it will output, "The command completed with one or more errors. "However, the standard output doesn't include the actual error messages, even though you can see them outputting to the screen. this is because there is a separate error stream, and by default, the redirection operator only streams the standard output.

This is where> &Operator comes in. this will pass the output from one "stream", or "handle" into another. this means you can take a command's error stream, and pass it into its output stream, so that both standard output and error output will be together.

Everything thus far was in Microsoft's documentation page I referred to earlier. But now how do you use the> & operator? I stumbled guest SS Eric jarvi's blog post, "redirecting stderr to stdout on the command line", which has an example in the comments.

A quick preface: The standard output stream has a handle of 1, and the error stream has a handle of 2. that being said, take the above example, and modify it like this so that both stdout and stderr will stream to your file:

Net group mygroup Joe/Add 1> addaccts. log 2> & 1

This code is required tively saying: "perform the net group command, then output the stdout to addaccts. log, but also redirect the stderr stream into the stdout stream at the same time ."

As a result, you have stdout and stderr both in your file. Your log will now give you much more meaningful feedback on any errors your Batch scripts might be encountering.

Whereas other forms of redirection are very useful (such as the | pipe), I find the above operators the most commonly used in my development.

Related Article

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.