Saturday, 22 June 2013

FOR..IN..DO

This command is similar to the programmer's FOR..NEXT loop in that a particular action
can be repeated a given number of times. The syntax (which may look a bit complicated) is:
FOR %%Variable IN (Set) DO Command
%%Variable is one-letter with the mandatory %% before it. Two percentage signs are used
so this variable won't be confused with a marker.
(Set)  is  one  or  more  filenames  or  commands  you  want  %%Variable to  assume  while  the
command is being executed. Use a space between entries, and pathnames are allowed. Don't forget
the parenthesis around the set. Wildcards may be used within (Set) if you are using filenames.
Command  is  the  particular  DOS  command  or  batch  subcommand  you  want  to  have  performed.
Usually one or more of these commands will contain the %%Variable in it. If (Set) contains DOS
commands, only %%Variable is used.
In effect, what this subcommand does is cause %%Variable to be an index into the (Set) for
use by Command.
An example:
Problem:Compare all files on the disk in drive A: with those on the disk in  drive B: and report
those that match.
Answer:The following two-line batch file run from the A: drive will do that task:
ECHO OFF
FOR %%Z IN (*.*) DO IF EXIST B:%%Z ECHO %%Z is on A: and B:
Let's see how...
The first line turns command display off to clear the clutter.
The second line is executed as many times as there are files on the disk in A: [the set (*.*)
assures this]. Each of those filenames are assigned to %%Z in turn and then checked for presence
on drive B: with the EXIST logical statement. If EXIST is true, then the message at the end of the
IF subcommand is sent to the screen, otherwise nothing is printed  and the next file on drive A: is
assigned and checked.
The following will diagram this process...
Files on drive A:
COMMAND.COM
FILE.ONE
FILE.TWO

Files on drive B:
COMMAND.COM
FILE.ONE
FILE.LTR
The batch subcommand we are investigating is:
FOR %%Z IN (*.*) DO IF EXIST B:%%Z ECHO %%Z is on A: and B:
Each  filename  on  A:  is  substituted  in  the  IF  subcommand  and  then  executed.  To  get  the
same effect you would have to type:
IF EXIST B:COMMAND.COM ECHO COMMAND.COM is on A: and B:
IF EXIST B:FILE.ONE ECHO FILE.ONE is on A: and B:
IF EXIST B:FILE.TWO ECHO FILE.TWO is on A: and B:
In the case of the example above, the first two would have a positive response and the last
would not print anything. Study it carefully before going on!
Another example:
Files on drive A:
COMMAND.COM
FILE.ONE
FILE.TWO
Files on drive B:
COMMAND.COM
FILE.ONE
FILE.LTR
OK, told you to study the example. Let's see if you remember. What is the oneline batch file
command to find and report out all files starting with an "F" on drive A:?
FOR %%Z IN (A:F*.*) DO ECHO File %%Z is on drive A:
In this case you see that the A: disk is checked for any file starting with the letter "F" and
that name is substituted in the variable %%Z. The appropriate message is then printed.
This is not an easy concept. It will require some study and practice to master.
Let's now look at using DOS commands in (Set).
The (Set) can contain DOS commands instead of filenames and these commands will then
be executed in sequence (to include running large programs under the control of the FOR..IN..DO
loop).
Let's say you want to sequentially:
•  Clear the screen,
•  Show the DOS version number,
•  then Show a disk directory with pause on.
You could do all that in a one line batch file with the following command in it:
FOR %%T IN (CLS Ver Dir/P) DO %%T 


When using DOS commands in (Set) you must use the space as a delimiter and cannot have
the  asterisk  (*)  or  question  mark  (?)  in  any  command.  Use  a  colon  (:)  instead  of  a  space  when
passing parameters to programs (i.e., DBASE:FILE instead of DBASE FILE). [The colon trick does
not always work; you have to experiment.]
It is possible to issue the FOR..IN..DO command at the DOS prompt by dropping one of the
percentage signs (%) on the variable.