Search code examples
c#renamefileinfo

How can I determine when a file was most recently renamed?


I have a program that compares files in two folders. I want to detect if a file has been renamed, determine the newest file (most recently renamed), and update the name on the old file to match.

To accomplish this, I would check to see if the newest file is bit by bit identical to the old one, and if it is, simply rename the old file to match the new one.

The problem is, I have nothing to key on to tell me which file was most recently renamed.

I would love some property like FileInfo.LastModified, but for files that have been renamed.

I've already looked at solutions like FileSystemWatcher, and that is not really what I'm looking for. I would like to be able to run my synchronizer whenever I want, without having to worry about some dedicated process tracking a folder's state.

Any ideas?


Solution

  • A: At least on NTFS, you can attach alternate data streams to a file. On your first sync, you can just attach a GUID in an ADS to the source files to tag them.

    B: If you don't have write access to the source, store hashes of the files you synced in your target repository. When the source changes, you only have to hash the source files and only compare bit-by-bit if the hashes collide. Depending on the quality and speed of your hash function, this will save you a lot of time.