开发者

ReadDirectoryChangesW doesn't do anything c++

Am I doing this right?

I am trying to find all the changes that took place in a folder called C:\Perl

After ReadDirectoryChangesW, it just gets stuck there. It doesn't move forward. Am I missing something obvious?

I am trying to achieve: How can I detect only deleted, changed, and created files on a volume?

Once everyday, I want to run the backup program, which will backup only the files that were changed under a specific folder.

int _tmain(int argc, _TCHAR* argv[])
{
TCHAR szBuffer[640] = {0};  
DWORD dwOffset = 0;
FILE_NOTIFY_INFORMATION* pInfo = NULL;
DWORD dwBytes;
HANDLE hFolder = CreateFile(L"C:\\Perl", FILE_LIST_DIRECTORY, FILE_SHARE_READ|FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
cout<<"Hello"<<endl;
ReadDirectoryChangesW(hFolder, szBuffer, sizeof(szBuffer) / sizeof(TCHAR), FALSE, FILE_NOTIFY_CHANGE_FILE_NAME, &dwBytes, NULL, NULL);
cout<<"Done"<<endl;
do
{
    // Ge开发者_StackOverflowt a pointer to the first change record...
    pInfo = (FILE_NOTIFY_INFORMATION*) &szBuffer[dwOffset];

    // ReadDirectoryChangesW processes filenames in Unicode. We will convert them to a TCHAR format...
    TCHAR szFileName[MAX_PATH] = {0};

    wcout<<pInfo->FileName<<"\t"<<pInfo->Action ;
    //WideCharToMultiByte(CP_ACP, NULL, pInfo->FileName, pInfo->FileNameLength, szFileName, sizeof(szFileName) / sizeof(TCHAR), NULL, NULL);

    // Perform your tests here...
    if (pInfo->Action == FILE_ACTION_ADDED)
    {
    }

    // More than one change may happen at the same time. Load the next change and continue...
    dwOffset += pInfo->NextEntryOffset;
}
while (pInfo->NextEntryOffset != 0);

}


You are calling it in synchronous mode, so it's not returning until there's a change to report. This is by design.

The Remarks section in the documentation explains how to call it asynchronously.

It sounds like what you want is to see if something changed since some point in time. If so, this is not the API you're looking for. You could iterate the contents and check the creation and modification timestamps on each file. If you want to notice deletions, you'll have to keep track of what you found last time and check to see if it's still there this time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜