Redirecting Output to a File in Windows Batch Scripts

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 will append the 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 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 the >& 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 across 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 effectively 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.

What about combining stdout and stderr when piping? It may not be readily apparent, but you can also accomplish this with the 2>&1 redirection. It’s just important where you place it; the redirection must occur before the pipe. So something like this would do the trick:

mycmd 2>&1 | othercmd

This would properly send both stdout and stderr from “mycmd” into the stdin stream for “othercmd”.

I find the above operators the most commonly used in my development. I hope you found this information useful.

What about sending just stderr? This is simple. Take a look at Redirecting Errors to a File in Windows Batch Scripts for a quick post on doing that.

Levels of JavaScript Knowledge

Jenny posted about an interesting article over at Particletree called Levels of Web Development Knowledge. My favorite article, which I was cracking up over, was Dean Edwards’ Levels of JavaScript Knowledge, which actually has 6 variants of JavaScript Hello World code to demonstrate 6 levels of JavaScript developer expertise. I love it!

It’s fun because, if you’ve been in the business for a bit, you can look at the levels and recall when you were there. Of course, the new kids coming into the business are learning the modern methods up front and may have never even coded in the lower levels. For a while, though, there was no better way to do things reliably. JavaScript support has come a long way recently.

I’m only at level 5 right now, but I’ve been aware of level 6, trying to wrap my brain around it, and will hopefully begin coding in that manner soon.

Dump Your JavaScript!

No, I’m not saying you should get rid of your JavaScript. Just dump it. :-D

Of course, I’m referring to dumping elements to the screen to help you debug and inspect data while developing an application. Have you ever wondered what precisely is going on inside that JavaScript array or struct, since you’re not getting the output you expect? In the ColdFusion world, cfdump is invaluable for this kind of thing.

Well, the Net Grow folks have developed an awesome JavaScript Dump method that behaves very similar to ColdFusion’s dump functionality. Awesome! This is definitely a must-have.

Spam Protection

As a side point, I really, really, really need to get CAPTCHA for the blog comment posting.

Sure, the existing spam protection prevents spam comments from being visible. The stupid spammers can never figure out that gobs of links give away the fact that it’s spam.

Or I could use a spam service like Akismet. Either Akismet or Nio’s Anti Spam Image plugin is what it shall be.

UPDATE: Okay, and when it’s all said and done, I went with Spam Karma 2. They seemed pretty good. We’ll see how things turn out.

  Theme Brought to you by Directory Journal and Elegant Directory.