
DOS Input/Output Redirection
----------------------------

STDOUT (standard output device)
-------------------------------
Output that will normally show on the screen can be redirected to a
file (or to a device like the printer) by the ">" symbol.

Examples:     dir > saved.dir   (stored in file)
              dir > LPT1        (sent to printer)

Using double >> symbols, text can be appended to an existing file:

Examples:     echo This will be appended >> file.ext
              type file.1 >> file.2

The single redirection symbol will always create a new file. If the file
already existed, it will be overwritten without warning.

Note that output can only be redirected if the program sends it to the
"standard output device" (STDOUT). Error messages are usually sent to
the "error device" (STDERR), which is also the screen, but without the
option to redirect.

For example, you may want redirect the output message of a COPY command
"n file(s) copied" to the NUL device, which is sort of a black hole:

              copy *.txt c:\some\where > NUL

This works fine, but in case of an error the error message is not
redirected, and will show on the screen (which makes sense).

Another way programs may send output to the screen is done by BIOS
functions. This output cannot be redirected. If you want to know
whether a given program supports STDOUT, you will have to find out.


STDIN (standard input device)
-----------------------------
A program that supports STDIN will read any text file as input source
when the input redirection symbol ("<") is used.

Example:      sort < file.in > file.out

Without the input redirection the program would read from the keyboard,
which usually doesn't make much sense. That's why newer versions of
SORT and MORE also accept the input file as normal command argument.

Some programs (FIND, for example) have a different behaviour, when input
redirection is used instead of specifying the file as command argument.
Try a FIND command, and check the result. For example:

              find "string" < file.txt
        or:   find "string" file.txt

Note that many programs get their input thru BIOS functions instead of
the DOS standard input. In this case redirection is not supported.


PIPES
-----
The most efficient way of using STDIN and STDOUT is "piping" text
directly from one program to another by the "|" symbol.

Example:      dir *.* | find "07-07-99"

This sends the DIR output directly to FIND, which selects lines with the
given date tag.

Example:      echo Y | del *.*

This will automatically answer the "are you sure?" prompt.


Important notes
---------------
Before a command is interpreted, DOS (ie COMMAND) first cuts out the
redirection expressions, denoted by the ">" and "<" symbols, followed
by what is expected to be a file or device name.

Thus, the redirection expressions may be placed anywhere in the command
line, for example:

        < file.in > file.out SORT /+5

The pipe, of course, always connects the programs in the given order.

Redirection symbols occurring within (double) quote marks are not
recognized by COMMAND.COM.

Take care of blank spaces that may remain between command (arguments)
and redirection symbols!

Example:      echo something >> file.txt
              echo something>> file.txt

The first example will append the string "something ", followed by CR/LF.

More important if you produce a SET command:

Example:      echo SET X=something >> temp.bat

The resulting variable will include a trailing blank space, which is
probably not what you wanted. Note, however, that LMOD always removes
trailing spaces from the output line.


FOR loops
---------
COMMAND installs the redirections once before the loop is executed, and
not for each instance of the loop. Thus, the only useful application is
probably appending to an output file with double >> redirection symbols.

*** 06 DEC 1999, Horst Schaeffer
