简体   繁体   English

如何确定文件最近何时重命名?

[英]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. 我会喜欢FileInfo.LastModified等属性,但是对于已重命名的文件。

I've already looked at solutions like FileSystemWatcher, and that is not really what I'm looking for. 我已经看过像FileSystemWatcher这样的解决方案了,这不是我想要的。 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? 有任何想法吗?

If you are running on an NTFS drive you can enable the change journal which you can then query for things like rename events. 如果您在NTFS驱动器上运行,则可以启用更改日志 ,然后可以查询重命名事件等内容。 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. 不幸的是,我不知道任何特定的C#实现阅读期刊。

A: At least on NTFS, you can attach alternate data streams to a file . 答:至少在NTFS上,您可以将备用数据流附加到文件中 On your first sync, you can just attach a GUID in an ADS to the source files to tag them. 在第一次同步时,您只需将ADS中的GUID附加到源文件即可标记它们。

B: If you don't have write access to the source, store hashes of the files you synced in your target repository. B:如果您没有对源的写入权限,请存储您在目标存储库中同步的文件的哈希值。 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. 根据哈希函数的质量和速度,这将为您节省大量时间。

You could possibly create a config file that holds a list of all expected names within the folder, and then, if a file in the folder is not a member of the expected list of names, determine that the file has then been renamed. 您可以创建一个配置文件,其中包含该文件夹中所有预期名称的列表,然后,如果该文件夹中的文件不是预期的名称列表的成员,则确定该文件已被重命名。 This would, however, add another layer of work considering you'd have to change the list every time you wish to add a new file to the folder. 但是,考虑到每次要将新文件添加到文件夹时都必须更改列表,这将增加另一层工作。

Filesystems generally do not track this. 文件系统通常不跟踪这个。

Since you seem to be on Windows, you can use GetFileInformationByHandle() . 由于您似乎在Windows上,因此可以使用GetFileInformationByHandle() (Sorry, I don't know the C# equivalent.) You can use the "file index" fields in the struct returned to see if files have the same index as something you've seen before. (抱歉,我不知道C#等价物。)您可以使用返回的结构中的“文件索引”字段来查看文件是否与您之前看到的文件具有相同的索引。 Keep in mind that hardlinks will also have the same index. 请记住,硬链接也将具有相同的索引。

Alternatively you could hash file contents somehow. 或者,你可以以某种方式散列文件内容。

I don't know precisely what you're trying to do, so I can't tell you whether either of these points makes sense. 我不确切地知道你要做什么,所以我不能告诉你这些要点中的任何一个是否有意义。 It could be that the most reasonable answer is, "no, you can't do that." 可能最合理的答案是,“不,你做不到。”

我将在(2)目录中的CRC(例如CRC)示例中存储最后更新时间的2个目录中的文件,其中包含CRC值,文件名等。之后,通过CRC查找列表进行交互,然后使用决定做什么的日期值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何确定FTP文件夹中的文件最近被覆盖的时间? - How can I tell when a file has been recently overwritten inside an FTP folder? * .rdl文件是机密文件时,如何确定子报表的来源? - How can I determine the source of the subreport when the *.rdl file is secretive? 如何确定哪个值在我的收藏中出现最多? - How can I determine which value occurs the most in my collection? 如何在SQL中访问最近插入的记录 - How to access most recently inserted record in SQL 如何检测存储文件是否在 UWP 应用中被重命名或删除? - How can I detect if a StorageFile was renamed or deleted in a UWP app? 如何确定特定文件实际上是MP3文件? - How can I determine that a particular file is in fact an MP3 file? 如何让ComboBox保留最近输入的值? - How can I have a ComboBox retain the recently entered values? 如何直接显示最近上传的视频? - How can I directly show a recently uploaded video? 使用mkbundle2创建文件时,如何确定在Mono C#环境中执行的文件名? - How can I determine the file name that is being executed in a mono C# environment when the file was created with mkbundle2? 如何确定Visual Studio中C#文件的类型? - How can I determine the type of C# file in Visual Studio?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM