0

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
{
    // 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);

}

Community
  • 1
  • 1
roymustang86
  • 8,054
  • 22
  • 70
  • 101

1 Answers1

3

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.

Adrian McCarthy
  • 45,555
  • 16
  • 123
  • 175
  • Yeah, for change in file contents, I just check the archive bit. I want to avoid doing that because it takes too long to go to each file and check if it was modified. Most anti virus and backup applications use change journals, which have records of every change that took place on a given volume. Would you happen to know of any alternative to that? – roymustang86 Sep 15 '11 at 20:49
  • Introduction to ReadDirectoryChanges has the link for what you want: http://msdn.microsoft.com/en-us/library/aa363798(v=vs.85).aspx – Adrian McCarthy Sep 15 '11 at 21:58
  • If the amount of files and directories you want to monitor is significant, like an entire volume for example, you may want to work with the NTFS change journals instead. I'd favor the windows API over manually scanning all files and checking timestamps and attributes. – AJG85 Sep 15 '11 at 22:54
  • @Adrian - I am confused, in the answer you just said that the API was not what I am looking for. But, in the comments you say that exact same API will do what I want??? Can you please be a little clear? – roymustang86 Sep 16 '11 at 13:44
  • @AJG85- With every scan of ReadDirectoryChanges, will it automatically update the USN journal record? and just scan the changes since last scan? – roymustang86 Sep 16 '11 at 14:14
  • @roymustang86: No, that's not what I said. I said the documentation for the API you were considering (ReadDirectoryChanges) had a pointer to the API you should be using for your use case (Change Journals). I think you're confused by the fact that the Change Journals documentation also makes a passing reference back to ReadDirectoryChanges. Read beyond that reference. – Adrian McCarthy Sep 16 '11 at 15:57
  • Thanks Adrian, but I have tried change journals, and that is mentioned in the other question I have posted. I ran into this problem where I could not get the filepath, and the change journal has a lot of records to temporary files being created and destroyed on a volume. If you have used change journals before, or know of a working example in C++, that would be helpful. – roymustang86 Sep 16 '11 at 18:40