Windows: how to determine if a file has been modified since the specified date

I have a utility that processes a set of files in a directory - the process is relatively slow (and there are many files), and therefore I tried to optimize the process only with process files that have the "last change" later than the last processing date.

This usually works well, however, I found that since copying a file does not change the last modified date, and therefore there are various scenarios related to copying files in which certain files that have been modified are skipped by the process, for example

  • The user processes the directory at 9:00.
  • The file is then copied from this directory and changed so that it has the last modified date of 9:30
  • Then the directory is processed again at 10:00
  • The modified file is then copied back to the directory at 10:30
  • Finally, the directory is processed again at 11:00.

Since the modified date of this file is 9:30, and the last time the directory is processed at 10:00, the file is skipped when it should not be.

Unfortunately, the above often happens too often in certain situations (for example, in a collaborative environment with source control, etc.). It is clear that my logic is wrong - I really need the date of the last change or copy. does such a thing exist?

Otherwise, is there another way to quickly determine with reasonable reliability if this file has changed?

+5
source share
5 answers

MD5 ? , .

+4

, FileSystemWatcher. , - . .

MSDN:

// Create a new FileSystemWatcher and set its properties.
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = args[1];
/* Watch for changes in LastAccess and LastWrite times, and
   the renaming of files or directories. */
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
   | NotifyFilters.FileName | NotifyFilters.DirectoryName;
// Only watch text files.
watcher.Filter = "*.txt";

// Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
+9

FileInfo ( ). : LastWriteTime CreationTime. , . , CreationTime LastWriteTime. . , LastWriteTime , CreationTime .

+3

? FileSystemWatcher? .

0

As you noticed, copying a file to an existing destination file stores the existing CreationTime file and sets LastWriteTime to the original LastWriteTime file, and not at the current system time when copying. Two possible solutions:

  • Make a deletion and copy copy that provides the intended use. CreationTime will be the current system time.
  • Also check the rchived attribute for file A and clear it during processing. When copying source-> dest, the attribute dest + A will be set.
0
source

All Articles