DOS - How to redirect command I/O

DOS has three I/O streams: one input stream (stdin), and two output streams (stdout and stderr).

Each stream has a number:

0 = stdin (default device: keyboard)
1 = stdout (default device: console window)

2 = stderr (default device: console window)

DOS has 6 redirect operators to send output to a stream (other than the console window), and receive input from a stream (other than the keyboard):

Redirect input: < <&
Redirect output: > >> >&
Redirect input and output: |

Input redirect operators

(those starting with <): <, <&

< (less than operator)
Redirect the standard input stream stdin (0) to read from a file or device, instead of the keyboard.

Example: read input from commands.txt, send to cmd.exe as input.

cmd.exe < commands.txt
c:\temp> echo %DATE%
Wed 09/27/2017

c:\temp> echo %TIME%
21:30:51.00

commands.txt

echo %DATE%
echo %TIME%
Which is the same as:
cmd.exe 0< commands.txt

Example: read input from list.txt, send to sort.exe as input.

sort < list.txt
1
2
3
4

list.txt

4
3
2
1

<& (less than ampersand operator)
Redirect the input of one numbered stream (left) to the output of another numbered stream (right).

Example: send stderr (2) to stdout (1).
reg 1> out.txt 2<&1
Which is the same as:
reg > out.txt 2<&1

Output redirect operators

(those starting with >): >, >>, >&

> (greater than operator)
Redirect the standard output stream stdout (1), to write to a file or device, instead of the console window.

Example:

dir 1> out.txt
Which is the same as:
dir > out.txt

Example: send stdout (1) to output.html, and stderr (2) to errors.txt.

tidy.exe input.html 1> output.html 2> errors.txt

Example: send stdout (1) to the nul device.

chkdsk > nul
echo %errorlevel%
I.e. discard standard output. In this example we are interested in the errorlevel return value only.
>> (double greater than operator)
Redirect the standard output stream stdout (1) , to write to a file or device, instead of the console window. Append if the file already exists, else create.

Example:

echo %DATE% %TIME% > out.txt
echo. >> out.txt
dir >> out.txt
>& (greater than ampersand operator)
Redirect the output of one numbered stream (left) to the input of another numbered stream (right).

Example: send stderr (2) to stdout (1).
reg 1> out.txt 2>&1
Which is the same as:
reg > out.txt 2>&1

Example: if we run the console application reg.exe with no parameters (or bad parameters), we will still see an error message in the console window, even with stdout redirected to the file out.txt, which will be empty:

reg > out.txt
ERROR: Invalid syntax.
Type "REG /?" for usage.

This is because reg.exe writes errors to stderr, and normal output to stdout. (The decision to write errors to the stderr stream is made by the application developer. Many, if not most console applications, use stdout only).

To send stderr (2) to stdout (1), we would write:
reg 1> out.txt 2>&1
Which is the same as:
reg > out.txt 2>&1

Input/Output redirect operator

: |

| (pipe operator)
Redirect the stdout (1) stream of one process (left of pipe), to the stdin (0) stream of another process (right of pipe).

Example:
type list.txt | sort
1
2
3
4

list.txt

4
3
2
1

Example: put the hostname on the clipboard

hostname | clip

Ads by Google

Ask a question, send a comment, or report a problem - click here to contact me.

© Richard McGrath