Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!hellgate.utah.edu!helios.ee.lbl.gov!nosc!cod!baird From: baird@cod.NOSC.MIL (John M. Baird) Newsgroups: comp.sys.ibm.pc Subject: Re: Redirection and piping Message-ID: <1725@cod.NOSC.MIL> Date: 13 Dec 89 23:28:51 GMT References: <89345.110313MHS108@PSUVM.BITNET> Organization: Naval Ocean Systems Center, San Diego Lines: 148 From article <89345.110313MHS108@PSUVM.BITNET>, by MHS108@PSUVM.BITNET (Mark Solsman): > > Can any one out there explain how to use the redirection and the pipeline > features with ms-dos? What follows in not technical. It just gives short examples of how they can be used, and warnings about when they can't. It is fairly long. John Baird, Naval Ocean Systems Center, San Diego, CA USA -------- From HELPSB, a DOS VMS-like help utility's files: ******** Information about |(piping) ******* To pipe (send, channel, direct) the output of one command into another command. Output from a command that would normally appear on the screen can be used as input to another command (that would normally get its input from the keyboard) by piping it. Piping is controlled by the use of the vertical bar (|) character. Some versions of DOS may not support the use of piping in BATCH files. If a text file, maybe some sort of document, had 10,000 lines in it, it could be displayed on the screen one full screen at a time, with the command TYPE BIGFILE.TXT | MORE If a directory contained 20 entries, to get a listing of the contents of the directory in alphabetical order, the command DIR | SORT could be used. If the directory had 120 entries, an alphabetical display one full screen at a time could be obtained by the command DIR | SORT | MORE ECHO Hello >> TST.TXT will redirect the string "Hello" from the screen to the file TST.TXT. If TST.TXT did not previously exist, this will create it. SORT < RANDOM.TXT >> SORTED.TXT will redirect input from the keyboard to come from the file RANDOM.TXT and redirect output to the screen to be added to the end of the file SORTED.TXT. TYPE CON >> EXISTING.TXT will read everything entered from the keyboard (until a Ctrl-Z is entered) and add it to the end of file EXISTING.TXT, rather than displaying it on the screen. Another way to to the same thing is with COPY EXISTING.TXT+CON It may sometimes appear that >> didn't work. If a file has a Ctrl-Z character in it, >> will still add to the end of the file, AFTER the Ctrl-Z. Some editors, and the DOS TYPE and COPY /A commands, quit when they encounter the first Ctrl-Z. MORE < NEW.TXT will redirect input which would normally come from the keyboard to come from the file NEW.TXT. MORE will still display its output on the screen. SORT < RANDOM.TXT > SORTED.TXT will redirect input from the keyboard to come from the file RANDOM.TXT and redirect output to the screen to the file SORTED.TXT. ******** Information about < or > or >> (redirection) ******* To change the normal destination or source of input or output for a command is done by redirection. Input that normally comes from the keyboard for a command can be redirected to come from a file by the < symbol (INPUT gives examples). Output from a command that normally goes to the screen can be redirected to a file by the > symbol (OUTPUT gives examples) or >> symbols (APPEND gives examples). Device names are really useful with redirection when used by DOS commands like COPY, ECHO, ESC, SORT and TYPE. Separately purchased programs may or may allow the use of redirection. If you encounter problems or erratic behavior in the use of redirection, it may be because the number of files that can be open at one time is set too low. This number is specified on the FILES line in the CONFIG.SYS file. ******** Information about > (output) ******* ECHO Hello > TST.TXT will redirect the string "Hello" from the screen to the file TST.TXT. If TST.TXT already exists, its previous contents will be lost. Use >> to add to an existing file. SORT < RANDOM.TXT > SORTED.TXT will redirect input from the keyboard to come from the file RANDOM.TXT and redirect output to the screen to the file SORTED.TXT. COPY OLD NEW > NUL will copy the file OLD in the current directory to the file NEW, and the usual COPY message "1 File(s) copied" will not appear on the screen. It will be redirected to the NUL device. This is a way to reduce the number of messages that appear on the screen while a batch file is being executed. REM > EMPTYFIL will create the file EMPTYFIL, a file that is empty (it has no data in it and a DIR command will show its size is zero). ECHO remark PAUSE > NUL will redirect the usual Strike any key when ready... message to NUL, allowing a remark of your choosing to appear on the screen. ******** Information about < (input) ******* MORE < NEW.TXT will redirect input which would normally come from the keyboard to come from the file NEW.TXT. MORE will still display its output on the screen. SORT < RANDOM.TXT > SORTED.TXT will redirect input from the keyboard to come from the file RANDOM.TXT and redirect output to the screen to the file SORTED.TXT. ******** Information about >> (append) ******* ECHO Hello >> TST.TXT will redirect the string "Hello" from the screen to the file TST.TXT. If TST.TXT did not previously exist, this will create it. SORT < RANDOM.TXT >> SORTED.TXT will redirect input from the keyboard to come from the file RANDOM.TXT and redirect output to the screen to be added to the end of the file SORTED.TXT. TYPE CON >> EXISTING.TXT will read everything entered from the keyboard (until a Ctrl-Z is entered) and add it to the end of file EXISTING.TXT, rather than displaying it on the screen. Another way to to the same thing is with COPY EXISTING.TXT+CON It may sometimes appear that >> didn't work. If a file has a Ctrl-Z character in it, >> will still add to the end of the file, AFTER the Ctrl-Z. Some editors, and the DOS TYPE and COPY /A commands, quit when they encounter the first Ctrl-Z. If a file has internal Ctrl-Z characaters in it, the EDLIN command can be used to remove them (see the EDLIN HINTS CONTROL_CHARACTERS topic.)