[英]How to scan all files in a folder, including subfolders in VC++ 2008?
[英]How to monitor a folder with all subfolders and files inside?
我有一個名為“Datas”的文件夾。 此文件夾有一個名為“收件箱”的子文件夾,其中有多個“.txt”文件。 可以修改此“Datas”文件夾,最后會有多個帶有“收件箱”子文件夾和“.txt”文件的子文件夾。 我需要監視“數據”文件夾和“收件箱”文件夾中的“.txt”文件。 我怎樣才能做到這一點?
INotify只是監視文件夾並在創建子文件夾時彈出事件。 如何在創建“.txt”文件時彈出事件(在哪個文件夾中)?
我需要C或C ++代碼,但我被卡住了。 我不知道如何解決這個問題。
從inotify手冊頁:
IN_CREATE File/directory created in watched directory (*).
可以通過捕獲此事件來完成。
再次從聯機幫助頁:
Limitations and caveats
Inotify monitoring of directories is not recursive: to monitor subdirectories under a directory, additional watches must be created. This can take a significant
amount time for large directory trees.
因此,您需要自己完成遞歸部分。 您可以從這里查看示例開始 。 您還應該查看項目notify-tools
注釋中詢問的示例 :它監視/tmp/inotify1
& /tmp/inotify2
以查找創建的新文件並顯示事件
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/inotify.h>
#define EVENT_SIZE ( sizeof (struct inotify_event) )
#define BUF_LEN ( 1024 * ( EVENT_SIZE + 16 ) )
int main( int argc, char **argv )
{
int length, i = 0;
int fd;
int wd[2];
char buffer[BUF_LEN];
fd = inotify_init();
if ( fd < 0 ) {
perror( "inotify_init" );
}
wd[0] = inotify_add_watch( fd, "/tmp/inotify1", IN_CREATE);
wd[1] = inotify_add_watch (fd, "/tmp/inotify2", IN_CREATE);
while (1){
struct inotify_event *event;
length = read( fd, buffer, BUF_LEN );
if ( length < 0 ) {
perror( "read" );
}
event = ( struct inotify_event * ) &buffer[ i ];
if ( event->len ) {
if (event->wd == wd[0]) printf("%s\n", "In /tmp/inotify1: ");
else printf("%s\n", "In /tmp/inotify2: ");
if ( event->mask & IN_CREATE ) {
if ( event->mask & IN_ISDIR ) {
printf( "The directory %s was created.\n", event->name );
}
else {
printf( "The file %s was created.\n", event->name );
}
}
}
}
( void ) inotify_rm_watch( fd, wd[0] );
( void ) inotify_rm_watch( fd, wd[1]);
( void ) close( fd );
exit( 0 );
}
測試運行:
shadyabhi@archlinux ~ $ ./a.out
In /tmp/inotify1:
The file abhijeet was created.
In /tmp/inotify2:
The file rastogi was created.
^C
shadyabhi@archlinux ~ $
還有fanotify
。 有了它,您可以在整個裝載點上設置手表。 在這里查看示例代碼。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.