在这一点不`for`或`为/ R`枚举该目录(树)? [英] At which point does `for` or `for /R` enumerate the directory (tree)?
问题描述
命令为
可用于枚举目录,并在每个项目申请(a)某些指令(S)。随着 / R
同样可以完成一个完整的目录树。
The command for
can be used to enumerate a directory and apply (a) certain command(s) on each item. With the /R
the same can be accomplished for a full directory tree.
什么时候枚举目录(树)的内容由命令(S)在为
命令的身体会发生变化?
What happens when the content of the enumerated directory (tree) is changed by the command(s) in the body of the for
command?
咋办我们有目录 D:\\数据
包含以下内容:
Supposed we have the directory D:\data
with the following content:
file1.txt
file2.txt
file3.txt
的输出%F中(的* .txt)也呼应%F
时表示,目录将明显地反映出上述名单。执行
The output of for %F in ("*.txt") do echo %F
when executed in said directory will reflect the above list obviously.
然而,什么是为
循环的输出时,在为
身体命令修改的内容目录?例如,在列表中的文件之一被删除,比方说 file3.txt
之前,它实际上是重复?或者,如果一个新文件被创建,如 file4.txt
,循环完成前?
However, what is the output of the for
loop when a command in the for
body modifies the content of the directory? For instance, one of the files in the list is deleted, let's say file3.txt
, before it is actually iterated? Or if a new file is created, like file4.txt
, before completion of the loop?
如何为/ R
在这方面的表现?应该有几个子目录 SUB1
, SUB2
, SUB3
,每个包含的文件上面的列表; 为/ R
正在通过 SUB2
, SUB1
迭代有已经过处理,但 SUB3
尚未;内容 SUB1
和 SUB3
在这一点上被改变(当目前通过 SUB2 <走/ code>提到);究竟会接着列举?我猜,内容的变化
SUB1
将不被认可,但对于 SUB3
?
How does for /R
behave in that context? Supposed there are several sub-directories sub1
, sub2
, sub3
, each containing the above list of files; for /R
is currently iterating through sub2
, sub1
has already been processed, but sub3
not yet; the contents of sub1
and sub3
are changed at that point (when currently walking through sub2
as mentioned); what will be enumerated then? I guess, the change of the content of sub1
won't be recognised, but what about sub3
?
最后,有没有在行为的差异
或为/ R
时,在命令提示符下执行或从一个批处理文件?以及是否有在不同的Windows版本的差异?
Finally, is there a difference in behaviour of for
or for /R
when being executed in the command prompt or from a batch file? And are there differences in different Windows versions?
注意:结果
另请参见我的类似的问题有关 FORFILES
命令。
推荐答案
这是一个非常好的问题!
This is an excellent question!
让我们集中于平原为
命令了一会儿。有相关使用时,此命令的已知错误的重命名文件的。例如:
Let's concentrate on plain for
command for a moment. There is a known bug related to this command when it is used to rename files. For example:
set i=0
for %%a in (*.txt) do (
set /A i+=1
ren "%%a" !i!.txt
)
在这种情况下,经常是某些文件被重命名两次,并且在某些情况下甚至三次。的问题是,这种行为取决于一系列不记录,像原来的文件和其他几个点的列表中的第一个重命名的文件的位置的因素。同样,如果之前它是由为
,一个找不到文件消息处理的文件被删除是的一般的发行(虽然不是所有的时间)。如果一个新文件后,在目录中的创建的的为
开始执行的,那么它可能会或可能不会由为
一系列的因素(再次)根据。通常的方式,以避免与重命名问题很给力为
命令首先读取文件的整个列表和然后的进程列表:
In this case is frequently that certain files be renamed twice, and even three times in certain cases. The problem is that this behavior depends on a series of factors that are not documented, like the position of the first renamed file inside the list of original files and several other points. Similarly, if a file is deleted before it is processed by the for
, a "File not found" message is usually issued (although not ALL times). If a new file is created in the directory after the for
started execution, then it may or may not be processed by the for
depending (again) on a series of factors. The usual way to avoid the problem with rename is to force for
command to first read the whole list of files and then process the list:
for /F "delims=" %%a in ('dir /B *.txt') do (
set /A i+=1
ren "%%a" !i!.txt
)
本办法,不重要的,可以上的文件在磁盘进行的更改:在 FOR / F
命令总是处理的原始的文件列表。
This way, don't matters the changes that can be made on the files in the disk: the for /F
command will always process the original file list.
一个类似的问题与为/ R
命令发生,但是在这种情况下,出现问题的可能性较大,因为有多个目录动态变化可以进行。还是那句话:确切的行为通过取决于一系列的未知因素,避免它们的方式是FOR / F ...在('DIR / S / B')
。但是,如果你的真正的兴趣在这一点上,我鼓励你做关于这一主题的一系列的测试(与后的结果)。 ;)
A similar problem happen with for /R
command, but in this case the possibility of problems is greater because there are more directories where dynamic changes can be made. Again: the exact behavior depends on a series of unknown factors and the way to avoid them is via for /F ... in ('dir /S /B')
. However, if you are really interested in this point, I encourage you to made a series of tests on the subject (and post the results). ;)
这篇关于在这一点不`for`或`为/ R`枚举该目录(树)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!