Click to See Complete Forum and Search --> : How to detect when a file is created


xargon
September 9th, 2003, 10:30 AM
Hi everyone,

I want to be notified when a file is created in a folder. I use FindFirstChangeNotification(....) and listen for the FILE_NOTIFY_CHANGE_FILE_NAME event. However, it fires this event for rename and delete also. How can I know that a file has been created new.

Thanks,

Xargon

mistersulu
September 10th, 2003, 03:10 PM
xargon,

EDIT: nevermind, read the post wrong.

sulu

mistersulu
September 10th, 2003, 04:12 PM
xargon,

ok here is something:


typedef struct _DirChangeParams
{
char path[MAX_PATH];
BOOL result;
} DirChangeParams;

BOOL GetFileCount(char *strDir, DWORD *pCount)
{
char s[MAX_PATH];
HANDLE hFind;
WIN32_FIND_DATA fd;

*pCount = 0;
sprintf(s, "%s\\*.*", strDir);
if ((hFind = FindFirstFile(s, &fd)) == NULL) return FALSE;
while (TRUE)
{
if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) *pCount = *pCount + 1;
if (!FindNextFile(hFind, &fd)) break;
}
FindClose(hFind);
return TRUE;
}

DWORD WINAPI DirChangeThreadProc(void *pParam)
{
DWORD origFileCount, newFileCount;

while (TRUE)
{
if (!GetFileCount(((DirChangeParams *) pParam)->path, &origFileCount))
{
((DirChangeParams *) pParam)->result = FALSE;
return 0;
}
Sleep(50);
if (!GetFileCount(((DirChangeParams *) pParam)->path, &newFileCount))
{
((DirChangeParams *) pParam)->result = FALSE;
return 0;
}
if (newFileCount > origFileCount) break;
}
((DirChangeParams *) pParam)->result = TRUE;
return 0;
}

BOOL WaitForNewFile(char *strDir)
{
DirChangeParams dcp;

strcpy(dcp.path, strDir);
HANDLE hDirChangeThread = CreateThread(NULL, 0, DirChangeThreadProc, &dcp, 0, 0);
WaitForSingleObject(hDirChangeThread, INFINITE);
return dcp.result;
}


sulu