Incode Systems Home Page Incode Systems, Inc.


Programs that read their input from the Standard Input Device or write their output to the DOS Standard Output Device may have their input and/or output redirected to another device or file.

The < character is used to redirect input and the > character is used to redirect output. The | character (called a pipe) may be used to chain multiple programs together where each successive program receives the output of the previous program as its input.

This has very powerful implications for building highly versatile programs which may be piped together in batch files.

The following example produces a list of all files on the C drive to the screen:


Since WHEREIS sends it's output to the standard output device, we can print the list like this:


(PRN is a standard DOS device that means the printer).

If we want to see the list on the screen but it is too long to fit all at once, we can use the more filter to pause after each screen:


If we want to sort it:


Or, sort it to the printer:


If the redirection symbol is immediately preceeded by a number, it is assumed to be a handle of the device to which the redirected output will be sent. This is apparent when you run a batch file containing redirection with ECHO ON. For example a batch containg:

ECHO This>out

will ECHO this when run:

ECHO This 1>out

This causes a problem whenever you want to redirect a string which ends in a number. For example:

ECHO This 1>out

results in "This" followed by a space being redirected to the file out, because the command processor assumes the 1 in the command refers to the standard output handle. To workaround this problem, you can specify the redirection before the command, like this:

>out ECHO This 1

This will also work, but the string will end in a space:

ECHO This 1 >out

Another reason to preceed the command with the redirection is when the command line ends in an unclosed quoted string. For example:

ECHO "First quoted string" "beginning quote without ending>out

Since the redirection in the command above is contained within a quoted string, the redirection is ignored because it is considered to be part of the string. Obviously, when you are using ECHO with literal strings, you could simply avoid using the unclosed quoted string. But if what you are echoing is from a batch parameter or environment variable, you have no way to know if the string will contain an unclosed quote at the time it is run. So, to echo the string just as it appears above, do this:

>out ECHO "First quoted string" "beginning quote without ending

Programs commonly write to both the standard output device and the standard error device. This is convenient when you want to redirect the output since output written to the error device will still be sent to the screen by default even when the standard output has been redirected. So, a copyright notice, for example, sent to the standard error device will not end up in the redirected standard output. There are times when you want to redirect only the stardard error output, or both the standard error and standard output.

ECHO This to standard output only>out
ECHO This to standard output only 1>out
ECHO This to standard error only 2>out
ECHO Standard output to one file, error to another 1>out 2>err
ECHO Standard output and error to the same file >out 2>&1
2>&1 ECHO Standard output and error to a pipe|MyProg