Windows:如何确定文件自给定日期以来是否已被修改
我有一个实用程序,它会处理目录中的一组文件 - 该过程相对较慢(并且有很多文件),因此我尝试通过仅处理具有“最后一个”的文件来优化该过程修改”晚于上次处理日期。
通常这很有效,但我发现复制文件不会更改上次修改日期,因此存在各种涉及复制文件的场景,其中某些已更改的文件被进程跳过,例如
- : 9:00 处理目录。
- 然后从该目录复制文件并进行修改,使其最后修改日期为 9:30。
- 然后在 10:00 再次处理该目录。
- 然后在 10:30 将修改后的文件复制回该目录。
- 最后该目录为在 11:00 再次处理
由于给定文件的修改日期是 9:30,并且该目录最后一次处理是在 10:00,因此在不应该跳过的情况下会跳过该文件。
不幸的是,在某些情况下(例如在具有源代码控制的协作环境中等),上述情况往往会经常发生。显然我的逻辑是有缺陷的 - 我真正需要的是“最后修改或复制”日期。这样的事存在吗?
如果做不到这一点,是否有另一种方法可以以合理的可靠性快速确定给定文件是否已更改?
I have a utility which goes through a processes a set of files in a directory - the process is relatively slow (and there are a lot of files) and so I've tried to optimise the process by only processes files that have a "last modified" later than the last processing date.
Usually this works well however I've found that as copying a file doesn't change the last modified date, and so there are various scenarios involving copying files in which certain files that have changed are skipped by the process, for example:
- The user processes the directory at 9:00.
- A file is then copied from this directory and modified so that it has a last modified date of 9:30
- The directory is then processed again at 10:00
- The modified file is then copied back into the directory at 10:30
- Finally the directory is processed again at 11:00
As the modified date of the given file is 9:30, and the directory was last processed at 10:00 the file is skipped when it shouldn't be.
Unfortunately the above tends to happen far too often in certain situations (such as in a collaborative environment with source control etc...). Clearly my logic is flawed - what I really need is a "last modified or copied" date. does such a thing exist?
Failing that, is there another way to quickly determine with reasonable reliability if a given file has changed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可能需要考虑使用 FileSystemWatcher 类。此类允许您监视目录的更改,并在修改某些内容时触发事件。然后,您的代码可以处理该事件并处理该文件。
来自 MSDN:
You might want to look at using the FileSystemWatcher class. This class lets you monitor a directory for changes and will fire an event when something is modified. Your code can then handle the event and process the file.
From MSDN:
您是否考虑过对文件运行 MD5 校验和并将其存储起来以供以后比较?如果您总是处理某个目录,这可能是可行的。
Have you thought of running MD5 checksums on the files and storing them later for comparison? If your always processing a certain directory, this might be feasible.
您可以使用
FileInfo
类获取所需的更改信息(您可能已经在使用)。您需要检查文件的两个属性,即LastWriteTime
和CreationTime
。如果其中任何一个早于您的上次处理日期,您需要复制该文件。人们普遍认为CreationTime
总是小于LastWriteTime
。它不是。如果将一个文件复制到另一个文件,新文件将保留源文件的LastWriteTime
,但CreationTime
将是复制的时间。You can use the
FileInfo
class to get the required change information (which you might be already using). You need to check two properties of a file, which areLastWriteTime
andCreationTime
. If either of them is higher than your last processing date, you need to copy the file. It is a common misconception thatCreationTime
is always less thanLastWriteTime
. It's not. If a file is copied to another file, the new file retains theLastWriteTime
of the source but theCreationTime
will be the time of the copy.您是否考虑过添加一个进程来监视您的目录?使用文件系统观察器?然后,您不再使用批处理过程和实时系统来监视文件。
Have you considered adding a process to watch your directory instead? Using a FileSystemWatcher? Then you move from using a batch process and a real time system for monitoring your files.
正如您所观察到的,将文件复制到现有目标文件会保留现有文件的 CreationTime,并将 LastWriteTime 设置为源文件的 LastWriteTime,而不是执行复制时的当前系统时间。两种可能的解决方案:
As you've observed, copying a file to an existing destination file keeps the existing file's CreationTime, and sets LastWriteTime to the source file's LastWriteTime, rather than current system time when doing the copy. Two possible solutions: