I/O Redirection and Filters, or Making DOS Jump Through Your Hoops . . . By Ken Johnson, Chicago Computer Society Many personal computer users never move beyond the basic DOS commands they learned with their first computer -- the famous DIR, COPY, ERASE, CHKDSK, and (infamous) FORMAT. But once you begin to look a little further into DOS, you can find all sorts of interesting tricks to make you computing life faster and easier. This article will look at two of the most interesting "advanced" uses of DOS: Input/Output redirection and the DOS filters. Input/Output Redirection I/O Redirection simply means to redirect input to and output from a DOS command or program. Normally DOS gets it's input from the keyboard and directs it's output to the screen. But by using the greater than (>), less than (<), and pipe (|) symbols, you can have DOS use a different input source and output destination. COMMAND > FILENAME -- sends the screen output of that command to the file specified. Think of the ">" here as indicating "with output to." COMMAND >> FILENAME -- adds lines of output to the existing file specified. The ">>" indicates "append output to." (Note: if the file specified by "FILENAME" does not exist, DOS will create it). COMMAND < FILENAME -- contents of the file specified is the input of the command. Here the "<" indicates "with input from." COMMAND | COMMAND -- "pipes" the output of the first command as input to the second command. This redirection is invisible to the command or program. The command or program always reads from the keyboard and writes to the screen. With I/O redirection, you are changing the definition of "keyboard" and "screen". I/O redirection can work with all DOS commands, but is used most often with three special DOS programs called filters -- MORE.COM, SORT.EXE, and FIND.EXE. These DOS filters can process text files or DOS command output through I/O redirection. You also can use I/O redirection with DOS' ECHO command to send control characters to your printer. For information on this use of redirection, see George Kollar's article "Printer Setup Codes" in the January 1991 issue of HardCopy. Some examples of redirection: DIR > FILELIST.TXT -- This creates a text file with your directory listing in it. This file can now be TYPEd, PRINTed, or edited with any text editor. DIR | SORT -- The DIR sends it's output to the SORT filter, which will sort and display the file names alphabetically. VER >> PCINFO.TXT CHKDSK C: >> PCINFO.TXT TREE C: >> PCINFO.TXT -- This sequence of commands will write information about the PC to a textfile called PCINFO.TXT. This file will contain the DOS version (VER), information on the hard drive and memory (CHKDSK), and finally a listing of directories on the hard drive (TREE). DIR | SORT | FIND /V "i" > SORTDIR.TXT -- This is an interesting one! The DIR command sends it's output to the SORT filter, which sorts the file names. SORT then sends it's output to the FIND filter, which removes all lines containing a lower-case "i" (this removes some lines that don't contain file names). Finally, the output of the FIND filter is sent to a file called SORTDIR.TXT. (The SORT and FIND filters are explained below). DIR | SORT | FIND /V "i" > PRN -- This also creates a sorted list of your files, but sends the result to the printer. PRN is the DOS device name for the printer. One note when you redirect the output of the DIR command. You probably will see two zero-byte files listed with names like "0E0C314F". These are generated temporarily as holding areas for the standard I/O. The files are always created in DOS' default directory, so one way to avoid them is to make a different drive the default before issuing the redirected DIR command. FIND filter The multitalented FIND filter will find lines in a file that contain (or don't contain) a string you specify, and optionally show you the line number of that string or a count of matching lines. The syntax of the command is: FIND [/V] [/N] [/C] "string" filename [filename . . .] "string" is the sequence of characters you wish to find in the file specified; it must be an exact match (upper and lower case ARE different). /V is a reVerse FIND; only output lines that DON'T contain the string. /N outputs a line Number before each matching line. /C outputs only a Count of matching lines, not the text of the lines. Let's look at some examples: FIND /N "Do Case" MENU1.PRG MENU2.PRG -- displays all lines of the files MENU1.PRG and MENU2.PRG that contain the character string "Do Case". Because of the /N switch, the line number will be displayed before the text of each line found. DIR C:\ | FIND "<" -- will display the subdirectories in the root directory of your hard drive (which have a in their directory listing). This is an alternative to using "DIR *.", which will include both subdirectories and files without an extension. DIR | FIND /V "i" -- takes the text of the DIR command and discards (/V) lines that contain the lower case "i". Since DOS file names are all capital letters, the /V will get rid of extra lines such as: Volume in drive.... Directory of drive .... XX files XXX bytes free CHKDSK C: /V | FIND /V "e" > MYFILES.LST -- creates a file called MYFILES.LST that contains a complete list of all files (including hidden files) on your hard drive. Filtering out all lines containing a lower case "e" will get rid of lines that don't contain a file name. TREE C: | FIND "Path" > SUBDIR.LST -- creates a file containing a listing of all subdirectories on your hard disk. The output from the TREE command is piped into the FIND filter, which keeps only the lines that list the subdirectories. FIND will output these lines into the file SUBDIR.LST. (This example won't work with DOS 4.01, since the output of the TREE command is different from DOS 3.x.) FIND /C /V "!@#" MENUTWO.PGM -- this is a fancy way of counting the number of lines in a file. It is unlikely that your file MENUTWO.PGM contains the string "!@#". FIND will count (/C) the number of lines that don't contain (/V) this weird string; therefore the number of lines in the file. MORE filter Tired of having DOS scroll your data off the top of the screen? Try MORE. The MORE filter is used to display either DOS output or a text file one screen at a time. You use I/O redirection to feed your output into MORE, which will read and display lines of the output. When MORE reaches the 24th line, it will display "-- More --" and wait for you to press a key. Simply press any key to get the next screen. Some examples: MORE < README.DOC -- displays the file README.DOC page by page TYPE README.DOC | MORE -- also displays the file README.DOC page by page. The output of the TYPE command is piped into the MORE filter. But, this method is less efficient and slower than MORE < README.DOC. DIR C:*.* | MORE -- displays all the files in the root directory of the C: drive page by page (similar to DIR /P). TREE C: /F | MORE -- displays a list of all files and directories of C: page by page. An important note: If you forget to redirect the input with "<" or "|" MORE will wait for you to type something in from the keyboard (remember, normal input is from the keyboard). You have to press Ctrl-Break or Ctrl-C, to exit the MORE filter. SORT filter The SORT filter sorts lines of input from a text file or redirected from another DOS command or program. The lines are sorted alphabetically from the first character. However, you can do a reverse sort or specify what position to start sorting on. SORT will sort in ASCII order (numbers, then letters), and UPPER-CASE and lower-case are considered the same. The syntax is: SORT [/R] [/+n] FILENAME /R means to sort Reverse alphabetic (Z to A, 9 to 0). /+n means to start sorting on the nth character of each line. You might want to try sorting on file extension (/+10) or file size (/+14). FILENAME indicate input from and output to a file. Some examples: DIR B: | SORT /+10 -- shows a list of files on the B: drive, sorted by extension (the +10 position). DIR B: | SORT /+10 | FIND /V "i" | MORE -- again we're looking at files on the B: drive sorted by extension, but this time the SORT output goes into the FIND filter, which will remove lines not containing file names. The FIND output then goes into the MORE filter, showing you one page of information at a time. SORT SORTFONE.TXT -- sorts the lines of the input file (PHONEBK.TXT) and creates a sorted output file (SORTFONE.TXT). A note with SORT: if you forget the I/O redirection (by a <, >, or |) SORT will wait for its input from the keyboard. You'll need to press Ctrl-Break or Ctrl-C to stop SORT. Conclusion I/O redirection and the MORE, SORT, and FIND filters are capabilities of DOS that are often overlooked by users. This article has tried to show why you really shouldn't overlook them. The DOS filters used with redirection can help you manage files and tame your hard disk. Besides, it's fun to watch DOS jump through your hoops! Author Information: Ken Johnson is Training and Support Manager at the law firm of Mayer, Brown & Platt in Chicago. He is a contributing editor to Hard-Copy, the Journal of the Chicago Computer Society, and a contributing writer to the Lawyers MicroComputer Users Group newsletter.