Redirection and Pipes |
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 (eg a printer or file) or to another program or command. There are four redirection functions:
>Redirects a command's output from the "standard output device" (usually the monitor) to another device (eg printer) or a file. Syntax:To redirect output to a device:
To redirect output to a file:
Notes:
Examples:Probably the most common uses of this redirection function is to send directory listings to the printer or to save them as a file. (One of Windows Explorer's biggest weaknesses is that it does not enable either of these operations).
>>Appends the output from a command to the specified file. Syntax:Command >> Filename Note: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). Example:To add the directory listing of the files in the c:\windows\system directory to that created above:
<Directs input to a command from a source other than the default (the default source usually being the keyboard). Syntax:Command < Datasource Example:To sort the lines in a text file (c:\data\address list.txt) on the 12th character, the SORT command is fed input from the file:
|The "pipe" redirects the output of a program or command to a second program or command. Syntax:Command1 | Command2 Example:To sort a directory listing based on the time the files were last modified, the output of a directory listing is piped to the SORT filter which sorts on the 39th character of each line:
Note that if the output of the DIR command had been redirected to SORT /+39 using >, Dos would return an "invalid switch" error after attempting to create a file called Sort. |
|