简体   繁体   English

ReadDirectoryChangesW没有做任何事情c ++

[英]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 我试图找到发生在名为C:\\ Perl的文件夹中的所有更改

After ReadDirectoryChangesW, it just gets stuck there. 在ReadDirectoryChangesW之后,它就会卡在那里。 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
{
    // Get 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. 如果是这样,这不是您正在寻找的API。 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. 如果你想注意删除,你必须跟踪你上次发现的内容并检查它是否仍然存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM