Redirection
A number of DOS commands send output to the screen and/or require input from the user. Redirection is a mechanism whereby the output of a command can be fed either to some other device for example, a printer or file, or to another program or command.
There are four redirection functions:
> |
Redirect output |
>> |
Append |
< |
Redirect input |
| |
Pipe |
>
Redirects a command's output from the "standard output device" (usually the monitor) to another device (e.g. printer) or a file.
Syntax:
To redirect output to a device:
Command > Device
To redirect output to a file:
Command > Filename
Acceptable Device names are, CON (Monitor), PRN (LPT1 - assumed to be the printer), LPT1 - 3 (Parallel Ports - usually connected to a printer), COM 1 - 4 (Serial Ports) and NUL (an electronic void).
If anything other than a recognized device is specified, it is assumed to be the name of a file. If a file already exists with the specified Filename, it is overwritten without any Warnings. Let us see some Examples to better understand it.
Probably the most common uses of this redirection function is to send directory listings to the printer or to save them as a file. To print out a sorted directory listing of all files in the Windows directory:
DIR c:\windows /o/a > PRN
To create a file containing the directory listing of the same directory:
DIR c:\windows /o/a > d:\windows.txt
>>
Appends the output from a command to the specified file.
Syntax:
Command >> Filename
If Filename does not exist, it is created. If Filename does exist, the output from the command is added to it, unlike the > function where the original contents are overwritten. Let us better understand it with example.
To add the directory listing of the files in the c:\windows\system directory to that created before:
DIR c:\windows\system /o/a >> d:\windows.txt
|