在这一点不`forfiles`枚举目录(树)? [英] At which point does `forfiles` enumerate a directory (tree)?

查看:106
本文介绍了在这一点不`forfiles`枚举目录(树)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

命令 FORFILES 旨在列举一个目录,在每个项目申请(a)某些指令(S)。随着 / S 同样可以完成一个完整的目录树。

The command forfiles is intended to enumerate a directory and apply (a) certain command(s) on each item. With the /S the same can be accomplished for a full directory tree.

什么时候枚举目录(树)的内容由命令(S)在 FORFILES 命令的身体会发生变化?

What happens when the content of the enumerated directory (tree) is changed by the command(s) in the body of the forfiles command?

咋办我们有目录 D:\\数据包含以下内容:

Supposed we have the directory D:\data with the following content:

file1.txt
file2.txt
file3.txt

FORFILES / P的输出D:\\数据/ M的* .txt/ CCMD / C回声@file在执行时说目录显然反映了上述列表中。

The output of forfiles /P "D:\data" /M "*.txt" /C "cmd /C echo @file" when executed in said directory will reflect the above list obviously.

但是,什么是 FORFILES 的输出,当体内的命令修改目录的内容?例如,在列表中的文件之一被删除,比方说 file3.txt 之前,它实际上是重复?或者,如果一个新文件被创建,如 file4.txt ,循环完成前?

However, what is the output of forfiles when a command in the 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?

如何在这种情况下确实 FORFILES / S 的行为?应该有几个子目录 SUB1 SUB2 SUB3 ,每个包含的文件上面的列表; FORFILES / S 正在通过 SUB2 SUB1 迭代有已经过处理,但 SUB3 尚未;内容 SUB1 SUB3 在这一点上被改变(当目前通过 SUB2 <走/ code>提到);究竟会接着列举?我猜,内容的变化 SUB1 将不被认可,但对于 SUB3

How does forfiles /S behave in such a situation? Supposed there are several sub-directories sub1, sub2, sub3, each containing the above list of files; forfiles /S 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?

我主要兴趣在 FORFILES ,因为Windows Vista中的行为。

I am mainly interested in the behaviour of forfiles since Windows Vista.

注意:结果
我已经张贴 有关命令一个非常类似的问题。然而,由于 FORFILES 不是一个内置的命令,并得到了一个完全不同的语法,我决定发布一个单独的问题,而不是延长了另一个的范围之内。

Note:
I already posted a very similar question about the for command. However, since forfiles is not a built-in command and has got a completely different syntax I decided to post a separate question instead of extending the scope of the other one.

推荐答案

FORFILES 将无法继续与一个错误重命名文件夹的枚举:该系统找不到指定的文件。一旦你尝试使用@ -variable解析为不存在的文件。未来将有一个已删除的文件没有错误,它会看到一个新添加的文件,如果它的名字遵循枚举当前使用的顺序当前处理的一(我已经按升序排列默认字母升序进行了测试)。所以很显然,它不前的命令的执行生成文件的完整列表,但一个自定义命令完成之后列举其中之一。

forfiles will fail to continue enumeration of a renamed folder with an ERROR: The system cannot find the file specified. once you try to use a @-variable which resolves to a nonexistent file. There'll be no error for a deleted file and it will see a newly added file if its name follows the currently processed one in currently used order of enumeration (I've tested it with default alphabetic sort in ascending order). So evidently it doesn't build the entire list of files prior to execution of the command but enumerates them one by one after the custom command completes.

根据需要用 FORFILES 做可靠的解决方案将被解析的输出DIR / S / B 的Robocopy 。因此,你可以确保名单之前的任何变化产生的。

Depending on what exactly you need to do with forfiles the reliable solutions would be parsing the output of dir /s /b or of robocopy in a list-only mode. Thus you can ensure the list is generated prior to any change.


  • FOR / Fdelims =%%一个在('DIRD:\\ DATA \\ * .TXT/ S / B')做....... 结果
    适用于简单枚举

  • for /f "delims=" %%a in ('dir "d:\data\*.txt" /s /b') do .......
    Suitable for simple enumerations

FOR / F令牌= *%%一个在('的Robocopy / L / NJH / NJS / NDL ........')做... 结果
适用于更复杂的场景,如限制日期时间跨度,可能需要使用非简单的个案 / V 额外的解析和

这篇关于在这一点不`forfiles`枚举目录(树)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆