Runs a specified command for each file in a set of files.
To 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:
If set contains wildcards, then specifies to match against directory names instead of file names.
Walks the directory tree rooted at [drive:]path, executing the FOR statement in each directory of the tree.
If no directory specification is specified after /R then the current directory is assumed.
If set is just a single period (.) character then it will just enumerate the directory tree.
The 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.
step equals 0, and end is greater than start, then the loop will continue forever:FOR /L %A IN (0,0,1) DO command [command-parameters]Do Forever (continuous) loops in "real" scripting languages.Ctrl+C and Y to confirm.EXIT /B or just EXIT:FOR /L %A IN (0,1,2147483647) DO (ECHO.%A & IF %A EQU 4 EXIT /B %A)FOR loop, the entire range (0..2147483647 in the example) will be stepped through, even if the loop is "broken" halfway and nothing will be done inside the loop anymore.or, with usebackq (not available in Windows NT 4, requires Windows 2000 or later):
filenameset is one or more file names.
Each file is opened, read and processed before going on to the next file in filenameset.
Processing consists of reading in the file, breaking it up into individual lines of text and 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 optional "options" parameter.
This is a quoted string which contains one or more keywords to specify different parsing parameters.
The keywords are:
eol character is the semicolon (;).FOR /F loops skip lines starting with semicolons unless a different eol character is specified (try "eol=").
tokens= string is an asterisk (*), then an additional variable is allocated and receives the remaining text on the line after the last token parsed.
Some examples might help:
would parse each line in myfile.txt, ignoring lines that begin with a semicolon, passing the 2nd and 3rd token from each line to the for body, with tokens delimited by commas and/or spaces.
Notice the for body statements reference %i to get the 2nd token, %j to get the 3rd token, and %k to get all remaining tokens after the 3rd.
For file names that contain spaces, you need to quote the filenames with double quotes.
In order to use double quotes in this manner, you also need to use the usebackq option (not available in Windows NT 4), otherwise the double quotes will be interpreted as defining a literal string to parse.
%i is explicitly declared in the for statement and the %j and %k are implicitly declared via the tokens= option.
You can specify up to 26 tokens via the tokens= line, provided it does not cause an attempt to declare a variable higher than the letter 'z'.
Remember, FOR variable names are global, and you can't have more than 52 (26 for NT 4) total active at any one time.
You can also use the FOR /F parsing logic on an immediate string, by making the filenameset between the parenthesis 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 parenthesis a single quoted (Windows NT 4 and later) or back quoted (Windows 2000 and later) string.
It will be treated as a command line, which is passed to a child CMD.EXE and the output is captured into memory and parsed as if it was a file.
So the following examples:
would enumerate the environment variable names in the current environment by parsing the output of the SET command.
In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:
%i removing any surrounding doublequotes (")
%i to a fully qualified path name
%i to a drive letter only
%i to a path only
%i to a file name only
%i to a file extension only
%i to file attributes of file
%i to date/time of file
%i to size of file
PATH environment variable and expands %i to the fully qualified name of the first one found.%~i and %~ai and %~ti and %~zi are not available in Windows NT 4, they require Windows 2000 or later.
The modifiers can be combined to get compound results:
%i to a drive letter and path only
%i to a file name and extension only
%i to a full path name with short names only
PATH environment variable for %i and expands to the drive letter and path of the first one found.
%i to a DIR like output line
In the above examples %i and PATH can be replaced by other valid values.
Just be careful to pick your FOR variable letters to not conflict with any of the format specifier letters if you plan on using the enhanced substitution logic.
The %~ syntax is terminated by a valid FOR variable name.
Picking upper case variable names like %I makes it more readable and avoids confusion with the modifiers, which are not case sensitive.
page last modified: 2022年03月09日; loaded in 0.0035 seconds
SETLOCAL ENABLEEXTENSIONS within your NT shell scripts (batch files) or execute those scripts using CMD /X.SETLOCAL DISABLEEXTENSIONS or CMD /Y.SETLOCAL page if you intend to use SETLOCAL extension switches.