for 循环命令中的 %f 是什么?

What is %f in the for loop command?

来自此处的示例。 https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/for#additional-references

for %f in (*.doc *.txt) do type %f

In the preceding example, each file that has the .doc or .txt extension in the current directory is substituted for the %f variable until the contents of every file are displayed. To use this command in a batch file, replace every occurrence of %f with %%f. Otherwise, the variable is ignored and an error message is displayed.

这些与本例中的变量不同吗?

https://www.tutorialspoint.com/batch_script/batch_script_variables.htm

set message=Hello World 
echo %message%

这些叫什么?我该如何操纵它们?

在批处理脚本俚语中它被称为for loop token,上面的例子只能在命令提示符下工作。对于批处理文件,您需要双 %.

这些标记在 for 循环的每次迭代中都会更改它们的值。示例(这可以在命令提示符中执行):

for %a in (1 2) do @echo %a

这将有两次迭代 - 如果 %%a 标记,第一个值为 1,第二个为 2。

您可以使用 for 循环来读取文件(使用 /f 开关且没有引号)、迭代文件(没有开关)或目录(使用 /d 开关)、遍历字符串(同样没有开关但使用 wild字符串中的卡片是不可能的)来读取文件(使用 /f 并且没有引号),或者处理字符串(再次使用 /f)。

您还可以使用 "delims" 选项拆分每次迭代的值,然后您将需要更多连续的字母:

for /f "tokens=1,2 delims=-" %a in ("1-2-3") do @echo %a:%b

这会将引号中的字符串拆分为 -,并将获取可通过 %a%b 标记访问的第一部分和第二部分。

关于 for 循环的更多信息:

https://ss64.com/nt/for.html

https://ss64.com/nt/for2.html

https://ss64.com/nt/for_r.html

https://ss64.com/nt/for_d.html

https://ss64.com/nt/for_f.html

https://ss64.com/nt/for_cmd.html