home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1993 #2 / Image.iso / magazine / mpc93jun.zip / DOSFLTR.TXT < prev    next >
Text File  |  1993-05-30  |  11KB  |  252 lines

  1.      I/O Redirection and Filters, or
  2.      Making DOS Jump Through Your Hoops . . .
  3.  
  4.      By Ken Johnson, Chicago Computer Society
  5.  
  6.      Many personal computer users never move beyond the basic DOS
  7.      commands they learned with their first computer -- the famous DIR,
  8.      COPY, ERASE, CHKDSK, and (infamous) FORMAT.  But once you begin to
  9.      look a little further into DOS, you can find all sorts of
  10.      interesting tricks to make you computing life faster and easier. 
  11.      This article will look at two of the most interesting "advanced"
  12.      uses of DOS:  Input/Output redirection and the DOS filters.
  13.  
  14.      Input/Output Redirection
  15.  
  16.      I/O Redirection simply means to redirect input to and output from
  17.      a DOS command or program.  Normally DOS gets it's input from the
  18.      keyboard and directs it's output to the screen.  But by using the
  19.      greater than (>), less than (<), and pipe (|) symbols, you can have
  20.      DOS use a different input source and output destination.  
  21.  
  22.      COMMAND > FILENAME 
  23.      --  sends the screen output of that command to the file specified. 
  24.      Think of the ">" here as indicating "with output to."
  25.  
  26.      COMMAND >> FILENAME
  27.      --  adds lines of output to the existing file specified.  The ">>"
  28.      indicates "append output to."
  29.  
  30.      (Note:  if the file specified by "FILENAME" does not exist, DOS
  31.      will create it).
  32.  
  33.      COMMAND < FILENAME
  34.      --  contents of the file specified is the input of the command. 
  35.      Here the "<" indicates "with input from."
  36.      
  37.      COMMAND | COMMAND
  38.      -- "pipes" the output of the first command as input to the second
  39.      command.
  40.      
  41.      This redirection is invisible to the command or program.  The
  42.      command or program always reads from the keyboard and writes to the
  43.      screen.  With I/O redirection, you are changing the definition of
  44.      "keyboard" and "screen".
  45.      
  46.      I/O redirection can work with all DOS commands, but is used most
  47.      often with three special DOS programs called filters -- MORE.COM,
  48.      SORT.EXE, and FIND.EXE.  These DOS filters can process text files
  49.      or DOS command output through I/O redirection.
  50.  
  51.      You also can use I/O redirection with DOS' ECHO command to send
  52.      control characters to your printer.  For information on this use of
  53.      redirection, see George Kollar's article "Printer Setup Codes" in
  54.      the January 1991 issue of HardCopy.
  55.      
  56.      Some examples of redirection:
  57.      
  58.      DIR > FILELIST.TXT
  59.      --  This creates a text file with your directory listing in it. 
  60.      This file can now be TYPEd, PRINTed, or edited with any text
  61.      editor.
  62.      
  63.      DIR | SORT
  64.      --  The DIR sends it's output to the SORT filter, which will sort
  65.      and display the file names alphabetically.
  66.      
  67.      VER >> PCINFO.TXT
  68.      CHKDSK C: >> PCINFO.TXT
  69.      TREE C: >> PCINFO.TXT
  70.      --  This sequence of commands will write information about the PC
  71.      to a textfile called PCINFO.TXT.  This file will contain the DOS
  72.      version (VER), information on the hard drive and memory (CHKDSK),
  73.      and finally a listing of directories on the hard drive (TREE).
  74.      
  75.      DIR | SORT | FIND /V "i" > SORTDIR.TXT
  76.      -- This is an interesting one!  The DIR command sends it's output
  77.      to the SORT filter, which sorts the file names.  SORT then sends
  78.      it's output to the FIND filter, which removes all lines containing
  79.      a lower-case "i" (this removes some lines that don't contain file
  80.      names).  Finally, the output of the FIND filter is sent to a file
  81.      called SORTDIR.TXT.  (The SORT and FIND filters are explained
  82.      below).
  83.      
  84.      DIR | SORT | FIND /V "i" > PRN
  85.      -- This also creates a sorted list of your files, but sends the
  86.      result to the printer.  PRN is the DOS device name for the printer.
  87.      
  88.      One note when you redirect the output of the DIR command.  You 
  89.      probably will see two zero-byte files listed with names like
  90.      "0E0C314F".  These are generated temporarily as holding areas for
  91.      the standard I/O.  The files are always created in DOS' default
  92.      directory, so one way to avoid them is to make a different drive
  93.      the default before issuing the redirected DIR command.
  94.      
  95.      
  96.      FIND filter
  97.      
  98.      The multitalented FIND filter will find lines in a file that
  99.      contain (or don't contain) a string you specify, and optionally
  100.      show you the line number of that string or a count of matching
  101.      lines.  The syntax of the command is:
  102.      
  103.      FIND [/V] [/N] [/C] "string" filename [filename  . . .]
  104.      
  105.      "string" is the sequence of characters you wish to find in the file
  106.      specified; it must be an exact match (upper and lower case ARE
  107.      different).
  108.      
  109.      /V is a reVerse FIND; only output lines that DON'T contain the
  110.      string.
  111.      
  112.      /N outputs a line Number before each matching line.
  113.      
  114.      /C outputs only a Count of matching lines, not the text of the
  115.      lines.
  116.      
  117.      Let's look at some examples:
  118.      
  119.      FIND /N "Do Case" MENU1.PRG MENU2.PRG
  120.      -- displays all lines of the files MENU1.PRG and MENU2.PRG that
  121.      contain the character string "Do Case".  Because of the /N switch,
  122.      the line number will be displayed before the text of each line
  123.      found.
  124.      
  125.      DIR C:\ | FIND "<"
  126.      -- will display the subdirectories in the root directory of your
  127.      hard drive (which have a <DIR> in their directory listing).  This
  128.      is an alternative to using "DIR *.", which will include both
  129.      subdirectories and files without an extension.
  130.      
  131.      DIR | FIND /V "i" 
  132.      -- takes the text of the DIR command and discards (/V) lines that
  133.      contain the lower case "i".  Since DOS file names are all capital
  134.      letters, the /V will get rid of extra lines such as:
  135.      Volume in drive....
  136.      Directory of drive ....
  137.      XX files  XXX bytes free
  138.  
  139.      CHKDSK C: /V | FIND /V "e" > MYFILES.LST
  140.      -- creates a file called MYFILES.LST that contains a complete list
  141.      of all files (including hidden files) on your hard drive. 
  142.      Filtering out all lines containing a lower case "e" will get rid of
  143.      lines that don't contain a file name.  
  144.      
  145.      TREE C: | FIND "Path" > SUBDIR.LST
  146.      -- creates a file containing a listing of all subdirectories on
  147.      your hard disk.  The output from the TREE command is piped into the
  148.      FIND filter, which keeps only the lines that list the
  149.      subdirectories.  FIND will output these lines into the file
  150.      SUBDIR.LST.  (This example won't work with DOS 4.01, since the
  151.      output of the TREE command is different from DOS 3.x.)
  152.      
  153.      FIND /C /V "!@#" MENUTWO.PGM 
  154.      -- this is a fancy way of counting the number of lines in a file. 
  155.      It is unlikely that your file MENUTWO.PGM contains the string
  156.      "!@#".  FIND will count (/C) the number of lines that don't contain
  157.      (/V) this weird string; therefore the number of lines in the file.
  158.      
  159.      
  160.      MORE filter
  161.      
  162.      Tired of having DOS scroll your data off the top of the screen? 
  163.      Try MORE.  The MORE filter is used to display either DOS output or
  164.      a text file one screen at a time.  You use I/O redirection to feed
  165.      your output into MORE, which will read and display lines of the
  166.      output.  When MORE reaches the 24th line, it will display
  167.  
  168.      "-- More --" 
  169.  
  170.      and wait for you to press a key.  Simply press any key to get the
  171.      next screen.
  172.  
  173.      Some examples:
  174.  
  175.      MORE < README.DOC
  176.      -- displays the file README.DOC page by page
  177.      
  178.      TYPE README.DOC | MORE
  179.      -- also displays the file README.DOC page by page.  The output of
  180.      the TYPE command is piped into the MORE filter.  But, this method
  181.      is less efficient and slower than MORE < README.DOC.
  182.      
  183.      DIR C:*.* | MORE
  184.      --  displays all the files in the root directory of the C: drive
  185.      page by page (similar to DIR /P).
  186.      
  187.      TREE C: /F | MORE
  188.      -- displays a list of all files and directories of C: page by page.
  189.      
  190.      An important note:  If you forget to redirect the input with "<" or
  191.      "|" MORE will wait for you to type something in from the keyboard
  192.      (remember, normal input is from the keyboard).  You have to press
  193.      Ctrl-Break or Ctrl-C, to exit the MORE filter.
  194.      
  195.      
  196.      SORT filter
  197.      
  198.      The SORT filter sorts lines of input from a text file or redirected
  199.      from another DOS command or program.  The lines are sorted
  200.      alphabetically from the first character.  However, you can do a
  201.      reverse sort or specify what position to start sorting on.  SORT
  202.      will sort in ASCII order (numbers, then letters), and UPPER-CASE
  203.      and lower-case are considered the same.  The syntax is:
  204.      
  205.      SORT [/R] [/+n] <FILENAME >FILENAME
  206.           
  207.      /R means to sort Reverse alphabetic (Z to A, 9 to 0).
  208.  
  209.      /+n means to start sorting on the nth character of each line.  You
  210.      might want to try sorting on file extension (/+10) or file size
  211.      (/+14).
  212.  
  213.      <FILENAME and >FILENAME indicate input from and output to a file.
  214.  
  215.      Some examples:
  216.      
  217.      DIR B: | SORT /+10
  218.      -- shows a list of files on the B: drive, sorted by extension (the
  219.      +10 position).
  220.  
  221.      DIR B: | SORT /+10 | FIND /V "i" | MORE
  222.      -- again we're looking at files on the B: drive sorted by
  223.      extension, but this time the SORT output goes into the FIND filter,
  224.      which will remove lines not containing file names. The FIND output
  225.      then goes into the MORE filter, showing you one page of information
  226.      at a time.
  227.      
  228.      SORT <PHONEBK.TXT >SORTFONE.TXT
  229.      -- sorts the lines of the input file (PHONEBK.TXT) and creates a
  230.      sorted output file (SORTFONE.TXT).
  231.      
  232.      A note with SORT:  if you forget the I/O redirection (by a <, >, or
  233.      |) SORT will wait for its input from the keyboard.  You'll need to
  234.      press Ctrl-Break or Ctrl-C to stop SORT.
  235.      
  236.      
  237.      Conclusion
  238.      
  239.      I/O redirection and the MORE, SORT, and FIND filters are
  240.      capabilities of DOS that are often overlooked by users.  This
  241.      article has tried to show why you really shouldn't overlook them. 
  242.      The DOS filters used with redirection can help you manage files and
  243.      tame your hard disk.  Besides, it's fun to watch DOS jump through
  244.      your hoops!
  245.      
  246.      
  247.      Author Information:  Ken Johnson is Training and Support Manager at
  248.      the law firm of Mayer, Brown & Platt in Chicago.  He is a
  249.      contributing editor to Hard-Copy, the Journal of the Chicago
  250.      Computer Society, and a contributing writer to the Lawyers
  251.      MicroComputer Users Group newsletter.
  252.