home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Media Share 9
/
MEDIASHARE_09.ISO
/
private
/
mpc93mar.zip
/
FORINDO.DAT
< prev
next >
Wrap
Text File
|
1993-02-16
|
6KB
|
146 lines
FOR...IN...DO [FORINDO]
By: Norman T. Welford, October 1992 LILYPAD, FROG Computer
FOR...IN...DO is a command that Microsoft describes as "Runs a
specified command for each file in a set of files". It can be used in
a batch file, or at the DOS prompt. As I found this explanation of its
operation not too clear, I put together this collection of examples to
give a fuller understanding of this command.
[REMEMBER: Even when these commands take two-or-three lines in this
column, they're entered -- direct or BAT -- ALL of each on ONE LINE!}
The form of this command is, when used in a batch file:
FOR %%variable IN (set) DO command [%%variable]
When used at the DOS prompt use only single % signs.
The variable can be any single letter; for convenience I will use V.
The set is a number of items enclosed in parentheses, with each item
separated by a delimiter such as a space, a comma, an equals sign, a
semicolon or a forward slash. The items can be filenames, pathnames,
character strings, metacharacters (such as replaceable parameters %0
through %9) or commands. In batch files environmental variables can be
used. If more than one, each must be separated by a space, a comma or
a semicolon.
Wildcard characters can be used in filenames. It is important to
remember that DOS will try to interpret each member of the set as a
filename of a file in the default directory.
Some examples to show how FOR works.
When FOR is invoked, for each item in the set the command after DO is
actioned. A simple test can be made at the DOS prompt with:
FOR %V IN (1 2 3 4) DO ECHO Hello
"Hello" will be echoed four times.
If instead you use:
FOR %V IN (1 2 3 4) DO ECHO %V
it will echo "1", "2", "3' and "4".
If instead you use:
FOR %V IN (* ? /) DO ECHO %V
it may echo "*" and "?" but will ignore the "/". However as
* and ? are wildcard file names, if there are in the
default directory any file names without extensions, it
will echo each file name in response to "*", and each
single character file name in response to "?".
Should you want for some reason to echo the actual symbols it can be
done by:
FOR %V IN (/* /? //) DO ECHO %V
here the "/" acts to cue a literal character to come.
To make directory listings of all BAT and COM files you might use:
FOR %V IN (*.BAT *.COM) DO DIR %V
But this would give a one entry directory for each BAT and COM file.
Instead use:
FOR %V IN (BAT COM) DO DIR *.%V
and you will get two directory listings one for BAT and one for COM
files.
One use is in a batch file to test user input obtained as a
replaceable parameter:
FOR %%V IN (H HELP h help) DO IF "%1" == "%%V" GOTO HELP
This will go to HELP for any one of the four items in the set.
Due to the peculiar properties of the "/" it could also be written as:
FOR %%V IN (/HHELP/hhelp) DO IF "%1" == "%%V" GOTO HELP
In a batch file you can also use environmental variables:
FOR %%V IN (%PATH%) DO DIR %%V\*.BAK
would do a DIR of BAK files for each directory in your PATH.
There is one caution: be very careful when using an environment
variable such as PATH that it is not TOO LONG. The DOS command will be
expanded in the batch file to include all the contents of the variable
and thus may exceed the 127 character limit for a DOS command. Strange
things may happen as DOS truncates the command you gave it.
One use I have made of FOR is in a batch file to backup the multiple
subdirectories for my WordStar data files:
FOR %%V IN (SUBD1 SUBD2 SUBD3) DO XCOPY C:\WS\%%V\*.* B:\%%V\ /M
As I have seven subdirectories this makes the batch file shorter and
requires less typing.
FOR...IN...DO commands can be nested if you load a secondary copy of
COMMAND.COM immediately after the DO so that the command becomes
FOR...IN...DO COMMAND/C. You cannot substitute CALL for COMMAND/C
DUPS.BAT is a batch file can be used to make a list of duplicate files:
CD %2
FOR %%V IN (%1) DO IF EXIST \%3\%%V ECHO %%V
Invoke with DUPS.BAT *.* SUBD1 SUBD2. Use full path for
subdirectories. Duplicates are echoed to the screen.
Thinking it would be nice to have a printed list, I tried:
FOR %%V IN (%1) DO IF EXIST \%3\%%V ECHO %%V > PRN
This will print the first duplicate found but just ECHO the rest.
[ED Note: Ah -- but that has to do with the nature of the IF EXIST
function. NEVER follow an IF... with something you want to happen
consistently. Instead of ENDING with the output re-direction, type the
line like this:
FOR %%V IN (%1) DO >PRN IF EXIST \%3\%%V ECHO %%V
And ALL the output will go to the printer! -- RWC]
Just for fun try this. Create X.X with ECHO X > X.X, and then run:
FOR %V IN (*.X) DO COPY %V X%V
and see what you get.
For more information on redirection see The Waite Group's "Tricks of
the MS- DOS Masters" Second edition page 113.