In the Command Line window, enter the for/? Can be explained by the following parameters = =
Executes a specific command for each file in a set of files.
For%variable in (set) do command [Command-parameters]
%variable specifies a single-letter replaceable parameter.
(set) specifies one or a set of files. Wildcard characters can be used.
command specifies the commands to execute on each file.
Command-parameters
Specify parameters or command-line switches for specific commands.
When using the for command in a batch file, specify the variable using%%variable instead of%variable. Variable names are case-sensitive, so%i differs from%i.
If the command extension is enabled, the following additional for command formats are supported:
FOR/D%variable in (set) do command [Command-parameters]
If a wildcard is included in the set, the specified matches the directory name, not the file name.
FOR/R [[Drive:]path]%variable in (set) do command [Command-parameters]
Check the directory tree that is rooted in [Drive:]path, pointing to the for statement in each directory. If no directory is specified after/R, the current directory is used. If the set is only a single point (.) character, the directory tree is enumerated.
FOR/L%variable in (start,step,end) do command [Command-parameters]
The set represents a sequence of numbers in increments from start to finish. Therefore, (1,1,5) will produce sequences 1 2 3 4 5, (5,-1,1) will produce sequences (5 4 3 2 1).
for/f ["Options"]%variable in (file-set) do command [Command-parameters]
for/f ["Options"]%variable in ("string") do command [Command-parameters]
for/f ["Options"]%variable in (' Command ') do command [Command-parameters]
Or, if you have the USEBACKQ option:
for/f ["Options"]%variable in (file-set) do command [Command-parameters]
for/f ["Options"]%variable in ("string") do command [Command-parameters]
for/f ["Options"]%variable in (' Command ') do command [Command-parameters]
FileNameSet is one or more file names. Before continuing to the next file in FileNameSet, each file has been opened, read, and processed. Processing involves reading a file, dividing it into lines of text, and then parsing each line into 0 or more symbols. The For loop is then called with the found symbol string variable value. By default,/F separates the first blank symbol from each line in each file. Skips a blank line. You can override the default resolution action by specifying an optional options parameter. This quoted string includes one or more keywords that specify different parsing options. These keywords are:
Eol=c-refers to the end of a line comment character (just one)
Skip=n-refers to the number of rows that are ignored at the beginning of the file.
Delims=xxx-refers to the delimiter set. This replaces the space and the default delimiter set for the jump bar.
Tokens=x,y,m-n-Indicates which symbol per line is passed to the for itself for each iteration. This causes the allocation of additional variable names. The m-n format is a range. Specify MTH with the nth symbol. If the last character in the symbol string is asterisk, the extra variable is assigned and accepts the retained text of the row after the last symbol is parsed.
USEBACKQ-Specifies that the new syntax is used in the following class situation:
A string that executes a post-quote as a command and a single-quote character is a literal string command and allows the file name to be amplified using double quotation marks in FileNameSet.
Some examples may help:
For/f "eol=; tokens=2,3* delims=, "%i in (myfile.txt) do @echo%i%j%k
Each row in the myfile.txt is parsed, ignoring those lines that begin with a semicolon, passing the second and third symbols in each row to the for program body, and delimited with commas and/or spaces. Note that this for program body statement references%i to get the second symbol, referencing%j to get the third symbol, referencing%k to get all the remaining symbols after the third symbol. For filenames with spaces, you need to enclose the file name in double quotation marks. In order to use double quotes in this way, you also need to use the USEBACKQ option, otherwise the double quotes will be interpreted as defining a string to parse.
%i is specifically described in the For statement, and%j and%k are specifically explained by the tokens= option. You can specify up to 26 symbols by tokens= a line, as long as you do not attempt to describe a variable that is higher than the letter ' z ' or ' z '. Keep in mind that the for variable is single-letter, case-sensitive, and global, and cannot have more than 52 in use at a time.
You can also use for/f parsing logic on adjacent strings by using single quotes to enclose the filenameset between parentheses. In this way, the string is treated as a single input line in a file. Finally, you can use the for/f command to parse the output of the command. The method is to change the filenameset between parentheses into an enclosing string. The string is passed to a sub-CMD as a command line. EXE, its output is captured in memory and is parsed as a file. Therefore, the following example:
for/f "Usebackq delims=="%i in (' Set ') do @echo%i
Enumerates the environment variable names in the current environment.
In addition, the substitution of the for variable reference has been enhanced. You can now use the following option syntax:
~i-Remove any quotation marks ("), expand%I
%~fi-Will%I Extend to a fully qualified path name
%~di-will only%I Extend to a drive letter
%~PI-will only%I Extend to a path
%~ni-will only%I Extend to a file name
%~xi-will only%I Expand to a file name extension
%~si-The expanded path contains only short names
%~ai-Will%I File attributes to expand to a file
%~ti-Will%I Date/time of extension to file
%~zi-Will%I Expand to File Size
%~ $PATH: I-Find the directory that is listed in the PATH environment variable and%I Expands to the first fully qualified name found. If the environment variable name is not defined or the file is not found, the key combination is expanded to an empty string
You can combine modifiers to get multiple results:
%~DPI-will only%I Expand to a drive letter and path
%~nxi-will only%I Expand to a file name and extension
%~FSI-will only%I Extend to a full pathname with a short name
%~DP$PATH:I-finds the directory that is listed in the PATH environment variable and will%I Expands to the first drive letter and path found.
%~ftzai-Will%I Extended to DIR with similar output line
In the above example,%I And PATH can be replaced by other valid values. The%~ syntax is terminated with a valid for variable name. Choose similar%I Uppercase variable names are easier to read and avoid being confused with key combinations that are not case-sensitive.
CMD extension path