Redirection is a function common to most command-line interpreters, that can redirect standard streams to user-specified locations. The input or output stream location is referred to as a handle.
Definition: Redirection is the switching of a standard stream of data so that it comes from a source other than its default source or or that it goes to some destination other than its default destination.
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. |
The following table lists the available handles:
Handle | Numeric equivalent | 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:
command 2> filename | Redirect any error message into a file |
command 2>> filename | Append any error message into a file |
(command) 2> filename | Redirect any CMD.exe error into a file |
command > file 2>&1 | Redirect errors and output to one file |
command > file 2<&1 | Redirect output and errors to one file |
command > fileA 2> fileB | Redirect output and errors to separate files |
command 2>&1 > filename | This will fail! |
Redirect to NUL (hide errors):
command 2> nul | Redirect error messages to NUL |
command >nul 2>&1 | Redirect error and output to NUL |
command >filename 2> nul | Redirect output to file but suppress error |
(command) >filename 2> nul | Redirect output to file but suppress CMD.exe errors |
Note, any long filenames must be surrounded in "double quotes". A CMD error is an error raised by the command processor itself rather than the program/command.
Redirection with > or 2> will overwrite any existing file.
You can also redirect to a printer with > PRN or >LPT1
To prevent the > and < characters from causing redirection, escape with a caret: ^> or ^<
Using the pipe operator (|)
The pipe operator (|) takes the output (by default, STDOUT) of one command and directs it into the input (by default, STDIN) of another command. For example, the following command sorts a directory:
dir | sort
In this example, both commands start simultaneously, but then the sort command pauses until it receives the dir command's output. The sort command uses the dir command's output as its input, and then sends its output to handle 1 (that is, STDOUT).
Examples of redirection:
DIR >MyFileListing.txt DIR /o:n >"Another list of Files.txt" ECHO y| DEL *.txt ECHO Some text ^<html tag^> more text MEM /C >>MemLog.txt Date /T >>MemLog.txt SORT < MyTextFile.txt SET _output=%_missing% 2>nul DIR C:\ >List_of_C.txt 2>errorlog.txt FIND /i "Jones" < names.txt >logfile.txt DIR C:\ >List_of_C.txt & DIR D:\ >List_of_D.txt ECHO DIR C:\ ^> c:\logfile.txt >NewScript.cmd (TYPE logfile.txt >> newfile.txt) 2>nul