Under 98 there are no parameters like /F, so how would you do it?
Executes a specified command for each file in a set of files.
FOR %variable IN (set) DO command
%variable specifies a replaceable parameter.
(set) specifies a set of one or more files. Wildcards may be used.
command specifies the command to execute for each file.
command-parameters
specifies parameters or switches for the specified command.
If you use the FOR command in a batch program, specify %%variable instead of
%variable. Variable names are case-sensitive, so %i is different from %I.
If command extensions are enabled, the following additional forms of the FOR command
are supported:
FOR /D %variable IN (set) DO command
If set contains wildcards, then it specifies matching directory
names instead of file names.
FOR /R path] %variable IN (set) DO command
Walks the directory tree rooted at path and executes the
FOR statement in each directory of the tree. If no directory specification
is given after /R, the current directory is assumed.
If set is just a single period (.) character, it will just enumerate the tree.
FOR /L %variable IN (start,step,end) DO command
set is a sequence of numbers from start to end, by step amount.
So (1,1,5) would generate the sequence (1 2 3 4 5) and
(5,-1,1) would generate the sequence (5 4 3 2 1)
FOR /F %variable IN (file-set) DO command
FOR /F %variable IN ("string") DO command
FOR /F %variable IN ('command') DO command
or, if usebackq is used:
FOR /F %variable IN (file-set) DO command
FOR /F %variable IN ('string') DO command
FOR /F %variable IN (`command`) DO command
filenameset may be one or more file names. Each file is opened,
read, and processed before moving on to the next file in the file name set.
File processing consists of reading the file, breaking it into separate text lines,
then parsing each line into zero or more tokens.
The body of the For loop is then called with the variable value(s) set
to the found token string(s). By default, /F passes the first blank-separated
token from each line of each file. Blank lines are skipped.
You can override the default parsing behavior by specifying the "options"
parameter. This is a quoted string containing one or more keywords
to specify different parsing options. The keywords are:
eol=c - specifies an end-of-line comment character
(just one)
skip=n - specifies the number of lines to skip
at the beginning of the file.
delims=xxx - specifies a delimiter set. This replaces
the default delimiter set of space and tab.
tokens=x,y,m-n - specifies which tokens from each line are to be
passed to the for body for each iteration.
This causes additional variable names to be allocated.
The m-n form is a range,
specifying the mth through the nth tokens.
If the last character in the tokens=
string is an asterisk, then an additional variable is allocated
to receive the remaining text on the line
after the last token parsed.
usebackq - specifies that the new semantics are enabled.
Where a backquoted string is executed as a command,
and a single-quoted string is a literal string.
It also allows the use of double quotes to
quote file names in
filenameset.
Here is an example:
FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do @echo %i %j %k
This parses each line in myfile.txt, ignoring lines that begin with a semicolon,
and passes the 2nd and 3rd tokens from each line to the for body,
where the tokens are delimited by commas and/or spaces. Note that the
for body statements refer to %i to get the second token, %j to get
the third token, and %k to get all remaining tokens after the third.
If file names contain spaces, you need to enclose the file name in double quotes.
To use double quotes in that way, you must use the usebackq option.
Otherwise, double quotes are interpreted as defining a literal string.
%i is explicitly declared in the for statement, and %j is implicitly declared
through the tokens= option. You can specify up to 26 tokens via the tokens= line,
provided that it does not attempt to declare a variable higher than the letter
'z' or 'Z'. Keep in mind that FOR variable names are global, and at any one time
you cannot have more than 52 FOR variables in use.
You can also use the FOR /F command to parse a literal string immediately,
by making the filenameset between the parentheses a quoted string.
It will be treated as a single line of input from a file and parsed.
Finally, you can use the FOR /F command to parse the output of a command.
You do this by making the filenameset between the parentheses a single-quoted string.
It will be treated as a command line and passed to a child CMD.EXE, and its
output will be captured into memory and parsed as if it were a file.
So the following example:
FOR /F "delims==" %i IN ('set') DO @echo %i
would enumerate the names of the environment variables in the current environment.
In addition, substitution of FOR variable references has been enhanced.
You can now use the following syntax:
%~I - expands %I and removes any surrounding double quotes (")
%~fI - expands %I to a fully qualified path name
%~dI - expands %I to a drive letter only
%~pI - expands %I to a path only
%~nI - expands %I to a file name only
%~xI - expands %I to a file extension only
%~sI - expanded path contains short names only
%~aI - expands %I to the file attributes of the file
%~tI - expands %I to the date/time of the file
%~zI - expands %I to the size of the file
%~$PATH:I - searches the directories listed in the PATH environment variable
and expands %I to
the fully qualified name of the first one found.
If the environment variable name
is not defined,
or the search does not find the file,
then this modifier expands to
the empty string.
The modifiers can be combined to get compound results:
%~dpI - expands %I to drive letter and path only
%~nxI - expands %I to file name and extension only
%~fsI - expands %I to a full path name with short names only
%~dp$PATH:i - searches the directories listed in the PATH environment variable
for %I and expands the first one found to the drive letter and
path.
%~ftzaI - expands %I to an output line like DIR
In the above examples %I and PATH can be replaced by other valid values. The %~ syntax
is terminated by a valid FOR variable name. Using uppercase variable names like %I
may improve readability and avoid confusion with the modifiers, since they are not
case sensitive.
[ Last edited by panner on 2006-1-3 at 17:45 ]