为什么/ F通常与FOR循环一起使用,即使在处理非文本文件时也是如此?

时间:2011-01-17 19:08:18

标签: batch-file for-loop

我一直在寻找一个批处理脚本来识别目录中的最新文件。我发现的例子都使用FOR / F.当我阅读关于FOR的帮助文档时,它指出/ F打开,读取和处理每个文件。

为什么在这种情况下使用/ F?我已经将它用于大型二进制文件,并且脚本似乎没有减速,所以我不认为每个文件实际上都是打开的等等。

我尝试使用FOR without / F来做同样的工作但没有任何运气。这有什么理由吗?

例如:

FOR %%I IN ('dir "*.AVI" /B /O:D') DO set newestAvi=%%I 

似乎不起作用。出于某种原因,newestAvi最后等于“/ O:D”。

如果我使用

FOR /F "delims=|" %%I IN ('dir "*.AVI" /B /O:D') DO set newestAvi=%%I
然后事情有效。

谢谢,

戴夫

1 个答案:

答案 0 :(得分:2)

我认为帮助文件的相关位是

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 back quoted 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
example:

因此,使用/ F您的命令将从

获取输出
dir "*.AVI" /B /O:D

并将每行解析为命令

newestAvi=%%I

成为

newestAvi=FileName.AVI

对于当前目录中的每个文件。分配的最后一个值是for命令执行结束时留下的值。