home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / private / mpc93mar.zip / FORINDO.DAT < prev    next >
Text File  |  1993-02-16  |  6KB  |  146 lines

  1.  
  2.                        FOR...IN...DO    [FORINDO]
  3.  
  4.  
  5.      By: Norman T. Welford, October 1992 LILYPAD, FROG Computer
  6.  
  7.      FOR...IN...DO is a command that Microsoft describes as "Runs a
  8.      specified command for each file in a set of files". It can be used in
  9.      a batch file, or at the DOS prompt. As I found this explanation of its
  10.      operation not too clear, I put together this collection of examples to
  11.      give a fuller understanding of this command.
  12.  
  13.      [REMEMBER: Even when these commands take two-or-three lines in this
  14.      column, they're entered -- direct or BAT -- ALL of each on ONE LINE!}
  15.  
  16.      The form of this command is, when used in a batch file:
  17.  
  18.             FOR %%variable IN (set) DO command [%%variable]
  19.  
  20.      When used at the DOS prompt use only single % signs.
  21.  
  22.      The variable can be any single letter; for convenience I will use V.
  23.      The set is a number of items enclosed in parentheses, with each item
  24.      separated by a delimiter such as a space, a comma, an equals sign, a
  25.      semicolon or a forward slash. The items can be filenames, pathnames,
  26.      character strings, metacharacters (such as replaceable parameters %0
  27.      through %9) or commands. In batch files environmental variables can be
  28.      used. If more than one, each must be separated by a space, a comma or
  29.      a semicolon.
  30.  
  31.      Wildcard characters can be used in filenames. It is important to
  32.      remember that DOS will try to interpret each member of the set as a
  33.      filename of a file in the default directory.
  34.  
  35.      Some examples to show how FOR works.
  36.  
  37.      When FOR is invoked, for each item in the set the command after DO is
  38.      actioned. A simple test can be made at the DOS prompt with:
  39.  
  40.                 FOR %V IN (1 2 3 4) DO ECHO Hello
  41.  
  42.                "Hello" will be echoed four times.
  43.  
  44.      If instead you use:
  45.  
  46.                 FOR %V IN (1 2 3 4) DO ECHO %V
  47.  
  48.                it will echo "1", "2", "3' and "4".
  49.  
  50.      If instead you use:
  51.  
  52.                 FOR %V IN (* ? /) DO ECHO %V
  53.  
  54.                 it may echo "*" and "?" but will ignore the "/". However as
  55.                 * and ? are wildcard file names, if there are in the
  56.                 default directory any file names without extensions, it
  57.                 will echo each file name in response to "*", and each
  58.                 single character file name in response to "?".
  59.  
  60.      Should you want for some reason to echo the actual symbols it can be
  61.      done by:
  62.  
  63.                 FOR %V IN (/* /? //) DO ECHO %V
  64.  
  65.                 here the "/" acts to cue a literal character to come.
  66.  
  67.      To make directory listings of all BAT and COM files you might use:
  68.  
  69.                 FOR %V IN (*.BAT *.COM) DO DIR %V
  70.  
  71.      But this would give a one entry directory for each BAT and COM file.
  72.  
  73.      Instead use:
  74.                FOR %V IN (BAT COM) DO DIR *.%V
  75.  
  76.      and you will get two directory listings one for BAT and one for COM
  77.      files.
  78.  
  79.      One use is in a batch file to test user input obtained as a
  80.      replaceable parameter:
  81.  
  82.               FOR %%V IN (H HELP h help) DO IF "%1" == "%%V" GOTO HELP
  83.  
  84.      This will go to HELP for any one of the four items in the set.
  85.  
  86.      Due to the peculiar properties of the "/" it could also be written as:
  87.  
  88.               FOR %%V IN (/HHELP/hhelp) DO IF "%1" == "%%V" GOTO HELP
  89.  
  90.      In a batch file you can also use environmental variables:
  91.  
  92.               FOR %%V IN (%PATH%) DO DIR %%V\*.BAK
  93.  
  94.      would do a DIR of BAK files for each directory in your PATH.
  95.  
  96.      There is one caution: be very careful when using an environment
  97.      variable such as PATH that it is not TOO LONG. The DOS command will be
  98.      expanded in the batch file to include all the contents of the variable
  99.      and thus may exceed the 127 character limit for a DOS command. Strange
  100.      things may happen as DOS truncates the command you gave it.
  101.  
  102.      One use I have made of FOR is in a batch file to backup the multiple
  103.      subdirectories for my WordStar data files:
  104.  
  105.         FOR %%V IN (SUBD1 SUBD2 SUBD3) DO XCOPY C:\WS\%%V\*.* B:\%%V\ /M
  106.  
  107.      As I have seven subdirectories this makes the batch file shorter and
  108.      requires less typing.
  109.  
  110.      FOR...IN...DO commands can be nested if you load a secondary copy of
  111.      COMMAND.COM immediately after the DO so that the command becomes
  112.      FOR...IN...DO COMMAND/C. You cannot substitute CALL for COMMAND/C
  113.  
  114.  
  115.  
  116.      DUPS.BAT is a batch file can be used to make a list of duplicate files:
  117.  
  118.               CD %2
  119.               FOR %%V IN (%1) DO IF EXIST \%3\%%V ECHO %%V
  120.  
  121.      Invoke with DUPS.BAT *.* SUBD1 SUBD2. Use full path for
  122.      subdirectories. Duplicates are echoed to the screen.
  123.  
  124.      Thinking it would be nice to have a printed list, I tried:
  125.  
  126.              FOR %%V IN (%1) DO IF EXIST \%3\%%V ECHO %%V > PRN
  127.  
  128.      This will print the first duplicate found but just ECHO the rest.
  129.      [ED Note:  Ah -- but that has to do with the nature of the IF EXIST
  130.      function. NEVER follow an IF... with something you want to happen
  131.      consistently. Instead of ENDING with the output re-direction, type the
  132.      line like this:
  133.  
  134.              FOR %%V IN (%1) DO >PRN IF EXIST \%3\%%V ECHO %%V
  135.  
  136.      And ALL the output will go to the printer!   -- RWC]
  137.  
  138.      Just for fun try this. Create X.X with ECHO X > X.X, and then run:
  139.  
  140.              FOR %V IN (*.X) DO COPY %V X%V
  141.  
  142.      and see what you get.
  143.  
  144.      For more information on redirection see The Waite Group's "Tricks of
  145.      the MS- DOS Masters" Second edition page 113.
  146.