\[Editor's Note: Email your scripting solutions (400 words or less) to Reader to Reader at email@example.com. Please include your script and phone number. We'll edit submissions for style, grammar, and length. If we print your contribution, you receive $100.\]
People commonly use the following redirection symbols:
> For redirecting the output of a command to a file
< For redirecting the input of a command from a file
| For piping the output of a command to the input of another command
>> For appending the output of a command to a file
However, to get full use of these redirection symbols, you need to understand the difference between the three standard file handles in command-line programs: standard input (STDIN), standard output (STDOUT), and standard error (STDERR). File handles are numbers that a program uses to identify open files or pipes. STDIN is 0, STDOUT is 1, and STDERR is 2.
When you use unmodified redirection symbols, you're using STDIN and STDOUT; STDERR remains attached to the console. For example, if you run the Dir command
dir foo >stdout.txt
where foo is a nonexistent file, the message File Not Found appears, even though the system redirects all other files to stdout.txt.
To redirect STDERR to a file, place STDERR's value (i.e., 2) before the > symbol. For example, the command
dir foo >stdout.txt 2>stderr.txt
captures all the Dir command's output and redirects it to the respective files.
Adding the value of 2 doesn't work for piping. However, you can attach STDERR to STDOUT, then redirect them together. For example, the command
dir foo >out.txt 2>&1
attaches STDERR to STDOUT, then redirects STDOUT (and STDERR) to the file out.txt. Thus, to pipe both STDERR and STDOUT, you can use the command
dir foo 2>&1 | sort
to attach STDERR to STDOUT, pipe STDOUT (and STDERR), and sort the results. (For more information about file-handle manipulation, see "Chapter 4, OS/2 Command Symbols" of the OS/2 1.3 manual.)
When redirecting files or performing other tasks with command-line programs, you can use conditional processing symbols to control a command's execution. For example, you can use parentheses to group commands within a command-line statement:
(dir foo & dir bar) 2>&1 | sort
In this code, the parentheses combine the output from the two Dir commands and pipe it to the Sort command. Windows NT Help documentation outlines the conditional processing symbols. Under the Start menu, select Help. Go to commands, Windows NT, and click Display. Select Commands Index, and click Display. Click conditional processing symbols.
Two other command-line tricks are to use the nul file as a discard file and to force the Echo command to output a blank line. To discard all output from a command, use the nul file in a command such as
dir >nul 2>nul
dir >nul 2>&1
To force the Echo command to output a blank line, type
The period after echo tricks the Echo command into thinking data exists (which prevents the command from returning ECHO is on). However, because the Echo command operates on everything following the first character after the command, it outputs only a blank line.