You can use redirection operators to redirect command input and output streams from the default locations to different locations. The input or output stream location is referred to as a handle
The following table lists operators that you can use to redirect command input and output streams.
|
Redirection operator
|
Description
|
|
>
|
Writes the command output to a file or a device, such as a printer, instead of the Command Prompt window.
|
|
<
|
Reads the command input from a file, instead of reading input from the keyboard.
|
|
>>
|
Appends the command output to the end of a file without deleting the information that is already in the file.
|
|
>&
|
Writes the output from one handle to the input of another handle.
|
|
<&
|
Reads the input from one handle and writes it to the output of another handle.
|
|
|
|
Reads the output from one command and writes it to the input of another command. Also known as a pipe.
|
By default, you send the command input (that is, the STDIN handle) from your keyboard to Cmd.exe, and then Cmd.exe sends the command output (that is, the STDOUT handle) to the Command Prompt window.
The following table lists the available handles.
|
Handle
|
Numeric equivalent of handle
|
Description
|
|
STDIN
|
0
|
Keyboard input
|
|
STDOUT
|
1
|
Output to the Command Prompt window
|
|
STDERR
|
2
|
Error output to the Command Prompt window
|
|
UNDEFINED
|
3-9
|
These handles are defined individually by the application and are specific to each tool.
|
The numbers zero through nine (that is, 0-9) represent the first 10 handles. You can use Cmd.exe to run a program and redirect any of the first 10 handles for the program. To specify which handle you want to use, type the number of the handle before the redirection operator. If you do not define a handle, the default < redirection input operator is zero (0) and the default > redirection output operator is one (1). After you type the < or > operator, you must specify where you want to read or write the data. You can specify a file name or another existing handle.
To specify redirection to existing handles, use the ampersand (&) character followed by the handle number that you want to redirect (that is, &handle#). For example, the following command redirects handle 2 (that is, STDERR) into handle 1 (that is, STDOUT):
1<&2
Duplicating handles
The & redirection operator duplicates output or input from one specified handle to another specified handle. For example, to send dir output to File.txt and send the error output to File.txt, type:
dir>c:\file.txt 2>&1
When you duplicate a handle, you duplicate all characteristics of the original occurrence of the handle. For example, if a handle has write-only access, all duplicates of that handle have write-only access. You cannot duplicate a handle with read-only access into a handle with write-only access.