『第 233 楼』:
使用 LLM 解释/回答一下
对一组文件中的每一个文件执行某个特定命令。
FOR %variable IN (set) DO command [command-parameters]
%variable 指定一个单一字母可替换的参数。
(set) 指定一个或一组文件。可以使用通配符。
command 指定对每个文件执行的命令。
command-parameters
为特定命令指定参数或命令行开关。
在批处理文件中使用 FOR 命令时,指定变量请使用 %%variable
而不要用 %variable。变量名称是区分大小写的,所以 %i 不同于 %I.
如果命令扩展名被启用,下列额外的 FOR 命令格式会受到
支持:
FOR /D %variable IN (set) DO command [command-parameters]
如果集中包含通配符,则指定与目录名匹配,而不与文件
名匹配。
FOR /R [[drive:]path] %variable IN (set) DO command [command-parameters]
检查以 [drive:]path 为根的目录树,指向每个目录中的
FOR 语句。如果在 /R 后没有指定目录,则使用当前
目录。如果集仅为一个单点(.)字符,则枚举该目录树。
FOR /L %variable IN (start,step,end) DO command [command-parameters]
该集表示以增量形式从开始到结束的一个数字序列。
因此,(1,1,5) 将产生序列 1 2 3 4 5,(5,-1,1) 将产生
序列 (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]
或者,如果有 usebackq 选项:
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 为一个或多个文件名。继续到 filenameset 中的
下一个文件之前,每份文件都已被打开、读取并经过处理。
处理包括读取文件,将其分成一行行的文字,然后将每行
解析成零或更多的符号。然后用已找到的符号字符串变量值
调用 For 循环。以默认方式,/F 通过每个文件的每一行中分开
的第一个空白符号。跳过空白行。您可通过指定可选 "options"
参数替代默认解析操作。这个带引号的字符串包括一个或多个
指定不同解析选项的关键字。这些关键字为:
eol=c - 指一个行注释字符的结尾(就一个)
skip=n - 指在文件开始时忽略的行数。
delims=xxx - 指分隔符集。这个替换了空格和跳格键的
默认分隔符集。
tokens=x,y,m-n - 指每行的哪一个符号被传递到每个迭代
的 for 本身。这会导致额外变量名称的分配。m-n
格式为一个范围。通过 nth 符号指定 mth。如果
符号字符串中的最后一个字符星号,
那么额外的变量将在最后一个符号解析之后
分配并接受行的保留文本。
usebackq - 指定新语法已在下类情况中使用:
在作为命令执行一个后引号的字符串并且一个单
引号字符为文字字符串命令并允许在 filenameset
中使用双引号扩起文件名称。
某些范例可能有助:
FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do @echo %i %j %k
会分析 myfile.txt 中的每一行,忽略以分号打头的那些行,将
每行中的第二个和第三个符号传递给 for 程序体;用逗号和/或
空格定界符号。请注意,这个 for 程序体的语句引用 %i 来
取得第二个符号,引用 %j 来取得第三个符号,引用 %k
来取得第三个符号后的所有剩余符号。对于带有空格的文件
名,您需要用双引号将文件名括起来。为了用这种方式来使
用双引号,您还需要使用 usebackq 选项,否则,双引号会
被理解成是用作定义某个要分析的字符串的。
%i 专门在 for 语句中得到说明,%j 和 %k 是通过
tokens= 选项专门得到说明的。您可以通过 tokens= 一行
指定最多 26 个符号,只要不试图说明一个高于字母 'z' 或
'Z' 的变量。请记住,FOR 变量是单一字母、分大小写和全局的;而且,
同时不能有 52 个以上都在使用中。
您还可以在相邻字符串上使用 FOR /F 分析逻辑;方法是,
用单引号将括号之间的 filenameset 括起来。这样,该字符
串会被当作一个文件中的一个单一输入行。
最后,您可以用 FOR /F 命令来分析命令的输出。方法是,将
括号之间的 filenameset 变成一个反括字符串。该字符串会
被当作命令行,传递到一个子 CMD.EXE,其输出会被抓进
内存,并被当作文件分析。因此,以下例子:
FOR /F "usebackq delims==" %i IN (`set`) DO @echo %i
会枚举当前环境中的环境变量名称。
另外,FOR 变量参照的替换已被增强。您现在可以使用下列
选项语法:
~I - 删除任何引号("),扩充 %I
%~fI - 将 %I 扩充到一个完全合格的路径名
%~dI - 仅将 %I 扩充到一个驱动器号
%~pI - 仅将 %I 扩充到一个路径
%~nI - 仅将 %I 扩充到一个文件名
%~xI - 仅将 %I 扩充到一个文件扩展名
%~sI - 扩充的路径只含有短名
%~aI - 将 %I 扩充到文件的文件属性
%~tI - 将 %I 扩充到文件的日期/时间
%~zI - 将 %I 扩充到文件的大小
%~$PATH:I - 查找列在路径环境变量的目录,并将 %I 扩充
到找到的第一个完全合格的名称。如果环境变量名
未被定义,或者没有找到文件,此组合键会扩充到
空字符串
可以组合修饰符来得到多重结果:
%~dpI - 仅将 %I 扩充到一个驱动器号和路径
%~nxI - 仅将 %I 扩充到一个文件名和扩展名
%~fsI - 仅将 %I 扩充到一个带有短名的完整路径名
%~dp$PATH:i - 查找列在路径环境变量的目录,并将 %I 扩充
到找到的第一个驱动器号和路径。
%~ftzaI - 将 %I 扩充到类似输出线路的 DIR
在以上例子中,%I 和 PATH 可用其他有效数值代替。%~ 语法
用一个有效的 FOR 变量名终止。选取类似 %I 的大写变量名
比较易读,而且避免与不分大小写的组合键混淆。
Execute a specific command for each file in a group of files.
FOR %variable IN (set) DO command
%variable specifies a single alphabetic replaceable parameter.
(set) specifies one or a group of files. Wildcards can be used.
command specifies the command to be executed for each file.
command-parameters specifies parameters or command-line switches for the specific command.
When using the FOR command in a batch file, specify the variable using %%variable instead of %variable. Variable names are case-sensitive, so %i is different from %I.
If command extensions are enabled, the following additional FOR command formats are supported:
FOR /D %variable IN (set) DO command
If wildcards are included in the set, it specifies matching directory names instead of file names.
FOR /R path] %variable IN (set) DO command
Checks the directory tree rooted at path, directing the FOR statement to each directory. If no directory is specified after /R, the current directory is used. If the set is only a single dot (.), the directory tree is enumerated.
FOR /L %variable IN (start,step,end) DO command
The set represents a sequence of numbers from start to end in increments. Thus, (1,1,5) will generate the sequence 1 2 3 4 5, and (5,-1,1) will 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 the usebackq option 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 is one or more filenames. Each file is opened, read, and processed before continuing to the next file in filenameset. Processing includes reading the file, dividing it into lines of text, and then parsing each line into zero or more tokens. The For loop is then called with the found token string variable values. By default, /F separates by the first blank token in each line of each file. Blank lines are skipped. You can substitute the default parsing operation by specifying the optional "options" parameter. This quoted string includes one or more keywords specifying different parsing options. These keywords are:
eol=c - specifies the end of a line comment character (just one)
skip=n - specifies the number of lines to skip at the beginning of the file.
delims=xxx - specifies the delimiter set. This replaces the default delimiter set of space and tab.
tokens=x,y,m-n - specifies which tokens of each line are passed to each iteration of the for itself. This causes the assignment of additional variable names. The m-n format is a range. Specifies the mth through the nth token. If the last character in the token string is an asterisk, then additional variables are assigned and receive the remaining text of the line after the last token parsed.
usebackq - specifies that the new syntax is used in the following cases:
executing a backquoted string as a command and a single quote character as a literal string command and allowing file names to be enclosed in double quotes in filenameset.
Some examples may be helpful:
FOR /F "eol=; tokens=2,3* delims=, " %i in (myfile.txt) do @echo %i %j %k
will parse each line in myfile.txt, skipping lines that begin with a semicolon, passing the second and third tokens of each line to the for body; delimited by commas and/or spaces. Note that the for body statement references %i to get the second token, %j to get the third token, and %k to get all remaining tokens after the third token. For filenames with spaces, you need to enclose the filename in double quotes. 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 be parsed.
%i is specifically explained in the for statement, and %j and %k are specifically explained by the tokens= option. You can specify up to 26 tokens in a tokens= line, as long as you do not attempt to define a variable higher than 'z' or 'Z'. Remember that FOR variables are single alphabetic, case-sensitive, and global; and, there can be no more than 52 in use at the same time.
You can also use the FOR /F parsing logic on adjacent strings; by enclosing the filenameset between parentheses in single quotes. Thus, 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 a command. By making the filenameset between parentheses a backquoted string. The string is treated as a command line, passed to a sub CMD.EXE, whose output is captured in memory and treated as a file to parse. Thus, the following example:
FOR /F "usebackq delims==" %i IN (`set`) DO @echo %i
will enumerate the names of environment variables in the current environment.
In addition, substitution of FOR variable references has been enhanced. You can now use the following option syntax:
~I - removes any quotes (") and expands %I
%~fI - expands %I to a fully qualified path name
%~dI - expands %I to just a drive letter
%~pI - expands %I to just a path
%~nI - expands %I to just a file name
%~xI - expands %I to just a file extension
%~sI - expanded path contains only short names
%~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 first fully qualified name found. If the environment variable name is not defined or the file is not found, this combination expands to an empty string
You can combine modifiers to get multiple results:
%~dpI - expands %I to just a drive letter and path
%~nxI - expands %I to just a file name and extension
%~fsI - expands %I to a full path name with short names only
%~dp$PATH:i - searches the directories listed in the path environment variable and expands %I to the first drive letter and path found.
%~ftzaI - expands %I to a DIR-like output line
In the above examples, %I and PATH can be replaced with other valid values. The %~ syntax terminates with a valid FOR variable name. Choosing an uppercase variable name like %I is more readable and avoids confusion with case-insensitive combinations.
|