Code:#include "stdafx.h" #include <windows.h> int main() { LPCTSTR szMonDirPath = TEXT("C:\\temp"); HANDLE hDir = CreateFile( szMonDirPath, // pointer to the file path FILE_LIST_DIRECTORY, // access (read/write) mode FILE_SHARE_READ|FILE_SHARE_WRITE, // share mode NULL, // security descriptor OPEN_EXISTING, // how to create FILE_FLAG_BACKUP_SEMANTICS, // FILE_FLAG_OVERLAPPED // file attributes NULL // file with attributes to copy ); FILE_NOTIFY_INFORMATION Buffer[1024] = {0}; //The larger the value, the larger lenght filename can be logged DWORD BytesReturned; printf("This is FolderMon application :- \nCurrently Monitoring %S => \n\n", szMonDirPath); char szFilename[MAX_PATH] = {0}; //var used to get the filename to print char szTempFilename[MAX_PATH] = {0}; //var used to get the filename to print int iCnt=1, iLen=0, iTempLen=0, iTrack=0, iFilenameEndLen=0; char *pdest = NULL; while (true) { int ret = ReadDirectoryChangesW( hDir, // handle to directory &Buffer, // read results buffer sizeof(Buffer), // length of buffer TRUE, // monitoring option // filter conditions FILE_NOTIFY_CHANGE_SECURITY| FILE_NOTIFY_CHANGE_SIZE| // in file or subdir FILE_NOTIFY_CHANGE_ATTRIBUTES| FILE_NOTIFY_CHANGE_DIR_NAME| // creating, deleting a directory or sub FILE_NOTIFY_CHANGE_FILE_NAME, // renaming,creating,deleting a file &BytesReturned, // bytes returned NULL, // overlapped buffer NULL // completion routine ); if(!ret) { printf("\nFailed function ReadDirectoryChangesW()"); continue; } if(MAX_PATH == wcstombs(szFilename, Buffer->FileName, MAX_PATH)) { printf("\nFailed function wcstombs()"); if(!memset(Buffer, 0x00, sizeof(Buffer))) { printf("\nFailed function memset()"); } continue; } iLen=0, iTempLen=0, iTrack=0, iFilenameEndLen=0; pdest = NULL; if(iCnt>1) { iLen = strlen(szFilename); iTempLen = strlen(szTempFilename); if(iLen > iTempLen) { pdest = strstr(szFilename, szTempFilename); iTrack = 1; } else { pdest = strstr( szTempFilename, szFilename); iTrack = 2; } if(pdest) { switch(iTrack) { case 1: szFilename[iTempLen] = '\0'; break; case 2: szFilename[iLen] = '\0'; break; } } } if(!memcmp(szTempFilename, szFilename, strlen(szFilename))) { if(!memset(Buffer, 0x00, sizeof(Buffer))) { printf("\nFailed function memset()"); } continue; } if(memcpy(szTempFilename, szFilename, MAX_PATH) == NULL) { printf("\nFailed memcpy()"); } DWORD dwAction = Buffer[0].Action; if(!memset(Buffer, 0x00, sizeof(Buffer))) { printf("\nFailed function memset()"); continue; } switch(dwAction) { case FILE_ACTION_ADDED: //printf("\nfile added : %s", szFilename); printf("\t%s", szFilename); break; case FILE_ACTION_REMOVED: printf("file removed : %s\n", szFilename); break; case FILE_ACTION_MODIFIED: //printf("\nfile added : %s", szFilename); printf("\t%s", szFilename); break; case FILE_ACTION_RENAMED_OLD_NAME: printf("file rename - oldname : %s", szFilename); break; default: printf("No file operation event matched"); break; } iCnt++; memset(szFilename, 0x00, MAX_PATH); }//while CloseHandle( hDir ); return 0; }