Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 395
Ty Norton Avatar asked Feb 22 '10 22:02

Ty Norton


People also ask

Can you see when a file was renamed?

There's no way to tell for sure whether a file has been renamed. When a file is renamed, its inode number doesn't change. (This may not be true for “exotic” filesystems, such as network filesystems, but it's true for all “native” Unix filesystems.)

How does Git know when a file is renamed?

In Git's case, it will try to auto-detect renames or moves on git add or git commit ; if a file is deleted and a new file is created, and those files have a majority of lines in common, Git will automatically detect that the file was moved and git mv isn't necessary.

How do you find out when a file was added to a folder?

It is possible to list all of the files in a folder (including those in subfolders), by typing an asterisk ( * ) in the search bar. From there you can click the Sort by button in the View ribbon panel, and click Date Modified and Descending .

How do you undo a renamed file?

If you need to change the extension as well, select the entire file name and change it. If you renamed the wrong file, or named your file improperly, you can undo the rename. To revert the action, immediately click the menu button in the toolbar and select Undo Rename, or press Ctrl + Z , to restore the former name.


2 Answers

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.

like image 77
Andras Vass Avatar answered Sep 24 '22 11:09

Andras Vass


If you are running on an NTFS drive you can enable the change journal which you can then query for things like rename events. However you need to be an admin to enable it to begin with and it will use disk space. Unfortunately I don't know of any specific C# implementations of reading the journal.

like image 35
tyranid Avatar answered Sep 23 '22 11:09

tyranid