关于C ++:如何查看目录的子文件是否已更改

关于C ++:如何查看目录的子文件是否已更改

How to see if a subfile of a directory has changed

在Windows中,是否有一种简单的方法来判断文件夹是否具有已更改的子文件?

我已验证,并且子文件更改时,文件夹上的上次修改日期不会更新。

我可以设置一个注册表项来修改此行为吗?

如果有关系,我正在使用NTFS卷。

我最终希望在C ++程序中具有此功能。

由于文件夹太大,因此无法递归扫描整个目录。

更新:我真的需要一种方法来在更改发生时不运行任何进程来执行此操作。 因此,安装文件系统监视程序对我而言不是最佳选择。

Update2:存档位也将不起作用,因为它与上次修改日期存在相同的问题。 将设置文件的存档位,但不会设置文件夹。


本文应该有所帮助。基本上,您创建一个或多个通知对象,例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
HANDLE dwChangeHandles[2];
dwChangeHandles[0] = FindFirstChangeNotification(
      lpDir,                          // directory to watch
      FALSE,                          // do not watch subtree
      FILE_NOTIFY_CHANGE_FILE_NAME);  // watch file name changes

   if (dwChangeHandles[0] == INVALID_HANDLE_VALUE)
   {
     printf("\
ERROR: FindFirstChangeNotification function failed.\
"
);
     ExitProcess(GetLastError());
   }

// Watch the subtree for directory creation and deletion.  
   dwChangeHandles[1] = FindFirstChangeNotification(
      lpDrive,                       // directory to watch
      TRUE,                          // watch the subtree
      FILE_NOTIFY_CHANGE_DIR_NAME);  // watch dir name changes

   if (dwChangeHandles[1] == INVALID_HANDLE_VALUE)
   {
     printf("\
ERROR: FindFirstChangeNotification function failed.\
"
);
     ExitProcess(GetLastError());
   }

然后您等待通知:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
 while (TRUE)
   {
   // Wait for notification.
      printf("\
Waiting for notification...\
"
);

      DWORD dwWaitStatus = WaitForMultipleObjects(2, dwChangeHandles,
         FALSE, INFINITE);

      switch (dwWaitStatus)
      {
         case WAIT_OBJECT_0:

         // A file was created, renamed, or deleted in the directory.
         // Restart the notification.
             if ( FindNextChangeNotification(dwChangeHandles[0]) == FALSE )
             {
               printf("\
ERROR: FindNextChangeNotification function failed.\
"
);
               ExitProcess(GetLastError());
             }
             break;

         case WAIT_OBJECT_0 + 1:

         // Restart the notification.
             if (FindNextChangeNotification(dwChangeHandles[1]) == FALSE )
             {
               printf("\
ERROR: FindNextChangeNotification function failed.\
"
);
               ExitProcess(GetLastError());
             }
             break;

         case WAIT_TIMEOUT:

         // A time-out occurred. This would happen if some value other
         // than INFINITE is used in the Wait call and no changes occur.
         // In a single-threaded environment, you might not want an
         // INFINITE wait.

            printf("\
No changes in the time-out period.\
"
);
            break;

         default:
            printf("\
ERROR: Unhandled dwWaitStatus.\
"
);
            ExitProcess(GetLastError());
            break;
      }
   }
}


也许这是矫kill过正,但MS的IFS套件或OSR的FDDK可能是替代方案。通过简单监视文件系统的所有更改来创建自己的文件系统过滤器驱动程序。


ReadDirectoryChangesW

此CodeProject文章中的一些出色的示例代码


也许您可以将NTFS 5 Change Journal与DeviceIoControl结合使用,如此处所述


如果更改发生后您无法运行某个进程,那么除了扫描文件系统并检查修改日期/时间之外,您无能为力。但是,这需要您存储每个文件的最后日期/时间并进行比较。

您可以使用存档位来加快速度(尽管它可能会使备份软件混乱,因此请谨慎操作)。

An archive bit is a file attribute
present in many computer file systems,
notably FAT, FAT32, and NTFS. The
purpose of an archive bit is to track
incremental changes to files for the
purpose of backup, also called
archiving.

As the archive bit is a binary bit, it
is either 1 or 0, or in this case more
frequently called set (1) and clear
(0). The operating system sets the
archive bit any time a file is
created, moved, renamed, or otherwise
modified in any way. The archive bit
therefore represents one of two
states:"changed" and"not changed"
since the last backup.

Archive bits are not affected by
simply reading a file. When a file is
copied, the original file's archive
bit is unaffected, however the copy's
archive bit will be set at the time
the copy is made.

因此,过程将是:

  • 清除所有文件上的存档位
  • 让文件系统随时间变化
  • 扫描所有文件-设置了存档位的所有文件均已更改
  • 这将消除程序保持状态的需要,并且由于您仅遍历目录条目(存储该位的位置)并且它们是集群的,因此它应该非常非常快。

    但是,如果您可以在更改期间运行一个进程,则需要查看FileSystemWatcher类。这是一个如何使用它的示例。

    它也存在于.NET中(供以后这类问题的搜索者使用)

    也许您可以让进程在计算机上运行以监视更改并创建文件以供以后阅读。

    -亚当


    在双重帖子中有人提到:WMI事件接收器

    仍然在寻找更好的答案。


    绝非易事-如果您有正在运行的应用程序,则可以按照其他答案的建议使用Win32文件更改通知api(FindFirstChangeNotification)。警告:大约2000趋势科技实时病毒扫描程序会将更改分组在一起,从而在请求文件系统更改列表时必须使用非常大的缓冲区。

    如果没有正在运行的应用程序,则可以打开ntfs日记功能并扫描日记以查找更改http://msdn.microsoft.com/zh-cn/library/aa363798(VS.85).aspx,但这可以是当更改的数量大于文件的数量时,速度比扫描整个目录要慢。


    如果您不反对使用.NET,则FileSystemWatcher类将为您轻松解决此问题。


    推荐阅读

      linux设置路由器命令?

      linux设置路由器命令?,网络,信息,代码,地址,电脑,工作,环境,系统,密码,路由,l

      改文件linux命令行?

      改文件linux命令行?,地址,系统,工作,信息,数字,文件,命令,设备,密码,权限,lin

      拷贝文件夹linux命令?

      拷贝文件夹linux命令?,系统,地址,服务,信息,密码,情况,单位,项目,命令,文件,l

      打包文件命令linux?

      打包文件命令linux?,系统,时间,工具,名称,命令,文件,目录,格式,表示,详细信

      linux设置根命令过短?

      linux设置根命令过短?,系统,工作,软件,地址,命令,代码,情况,管理,基础,位置,3

      linux设置路由器命令?

      linux设置路由器命令?,网络,信息,代码,地址,电脑,工作,环境,系统,密码,路由,l

      linux中文件编辑命令?

      linux中文件编辑命令?,系统,工作,工具,信息,地址,发行,命令,第一,检测,基础,l

      linux修改端口号命令?

      linux修改端口号命令?,代码,服务,系统,端口,工作,邮箱,文件,编辑,命令,后果,

      linux文件格式的命令?

      linux文件格式的命令?,设备,命令,系统,文件,标准,代码,情况,电脑,平台,数据,l

      linux修改锁屏命令?

      linux修改锁屏命令?,时间,系统,密码,名称,软件,电脑,工具,命令,终端,快捷键,l

      linux命令行编译文件?

      linux命令行编译文件?,代码,系统,项目,工具,网上,手机,电脑,地址,官网,文件,L

      linux下文件写入命令?

      linux下文件写入命令?,系统,信息,文件,名字,电脑,软件,目录,命令,内容,指令,l

      linux命令备份文件夹?

      linux命令备份文件夹?,设备,系统,文件,命令,备份,情况,做好,名字,灵活,数据,l

      linux命令deb文件?

      linux命令deb文件?,软件,位置,第一,系统,中心,工具,信息,发行,网站,管理系统

      linux文件夹删除命令?

      linux文件夹删除命令?,系统,命令,不了,档案,名称,通用,文件夹,文件,目录,指

      linux文件类基本命令?

      linux文件类基本命令?,系统,设备,工具,工作,基础,命令,文件,发行,管理,网络,L

      linux建个文件夹命令?

      linux建个文件夹命令?,系统,名字,命令,文件,文件夹,环境,不了,名称,数据,密

      linux设日期时间命令?

      linux设日期时间命令?,时间,系统,命令,国家,环境,数据,电脑,标准,信息,大陆,l

      linux命令ip修改?

      linux命令ip修改?,地址,系统,代码,密码,网络,信息,服务,设备,工具,命令,linux

      linux命令添加文件?

      linux命令添加文件?,工作,简介,数据,系统,文件,命令,操作,文件名,内容,终端,l