Classic Computer Magazine Archive COMPUTE! ISSUE 129 / MAY 1991 / PAGE 80

Plumbing the depths of DOS with pipes and filters. (column)
by Tony Roberts

Redirection, pipes, and filters are tools that let DOS users accomplish seemingly impossible tasks. First, we need a few definitions. Your keyboard is the standard input device, and your monitor is the standard output device. Together, the keyboard and monitor are known as CON, or the console device. Most DOS input comes from the keyboard, and most DOS output is displayed on the monitor.

Redirection allows DOS to accept input from a source other than the keyboard and to direct output to a destination other than the screen. The greater and less-than symbols are used to accomplish the redirection, and these symbols point out the direction of the information flow.

For example, DIR > DIRFILE redirects the output of the DIR command (which displays a directory listing) to a file called DIRFILE. That file can then be edited, printed, or treated like any other file.

If DIRFILE had already existed, the command would've erased the existing file and created a new one. Redirected information can be appended to the end of an existing file by using two greater-than signs: DIR >> DIRFILE. Redirection also can be used to fetch information from a source other than the keyboard. To do this, use a less-than symbol to reverse the direction of the data flow, as in SORT < INPUT.TXT. This command sorts the information in the INPUT.TXT file and displays it, in alphabetical order, on the screen.

Piping is similar to redirection, but it allows the output of one program to be used as the input for another program. The vertical bar is used as the pipe symbol.

Many computer users steer clear of redirection and piping because the results seem unpredictable. The key to setting up successful commands lies in knowing the distinction between redirection and piping. Redirection links a program with a device or a file; piping connects two programs.

Filters, which modify s data, are often used in conjunction with redirection or piping. DOS comes equipped with three filters--MORE.COM, FIND.EXE, and SORT.EXE. To use these filters, the program files must be on your system's default disk or path.

MORE is a filter commonly used to display text files. It displays a file one screenful at a time and pauses until the user presses a key. MORE can be used with redirection or with piping, depending on the situation. For example, TYPE SAMPLE.TXT I MORE uses piping because two programs (TYPE and MORE) are involved. The output of the TYPE command is piped through the MORE filter. The command MORE < SAMPLE.TXT accomplishes the same thing, but it does so using redirection. The MORE command gets its input from the SAMPLE.TXT file.

SORT can be used with redirection as indicated above, but if you want a sorted directory listing, you must use piping because SORT and DIR are both commands. DIR | SORT pipes the output of the DIR command through the SORT filter.

There are several options you can use with the SORT command. You can sort a directory listing by file extension, for example, using the command DIR | SORT /+10. The /+10 specifies that the sorting should be based on the tenth character in each line, the file extension. It also is possible to reverse the direction of the sort by including the /R switch. This would order entries from Z to A, then from 9 to 0.

Both SORT and MORE create temporary files on the default disk while they work, so be certain the disk is neither full nor write protected, or you'll get an error.

The FIND filter locates specific text strings in text files. The syntax for this filter is FIND switch "text string" filename. For example, FIND Fred" NAMES.TXT displays all the lines containing the name Fred in a file called NAMES.TXT. FIND looks for an exact match, so if you're looking for Fred, you can't spell it FRED.

You can specify more than one filename for FIND to scan, but wildcards are not allowed. The switches that are used with the FIND filter are /N, which supplies a line number for each line it displays; /C, which simply counts the number of occurrences of the text string; and /V, which displays all the lines not containing the text string. Here are two examples of how to combine filters, piping, and redirection.

CHKDSK /V | FIND ".BAT" runs CHKDSK with the /V (verbose) switch, creating a list of all files on the disk. That list is then piped into the FIND filter, which searches for the string.BAT, the batch-file extension. What you see onscreen is a catalog of all the batch files on your disk. This command can locate a group of files or a single file. Just be sure to type the filename in all uppercase letters.

SORT < NAMES.TXT > NAMES.SRT uses SORT to alphabetize the names in the NAMES.TXT file. The sorted output will be placed in a file called NAMES.SRT.