title: C++ Monitoring filechange with ReadDirectoryChangesW not triggering all Actions?
body: <p>I have an application which has to monitor any changes in a particular directory. I use the following code, however when I rename a file for instance, the Action <code>FILE_ACTION_RENAMED_NEW_NAME</code> is not triggered. Actually, the <code>UNDISCOVERED ACTION</code> get's triggered which results in unexpected behavior? What am I doing wrong here?</p>
  
  <pre><code>char Dir[] = "DIRPATH";
  HANDLE hDir = CreateFile(
      Dir,
      FILE_LIST_DIRECTORY,
      FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE,
      NULL,
      OPEN_EXISTING,
      FILE_FLAG_BACKUP_SEMANTICS,
      NULL);
  int nCounter = 0;
  FILE_NOTIFY_INFORMATION strFileNotifyInfo[1024];
  FILE_NOTIFY_INFORMATION *fni = NULL;
  
  while (TRUE)
  {
      //strFileNotifyInfo = NULL;
      DWORD dwBytesReturned = 0;
      if (ReadDirectoryChangesW(hDir, (LPVOID)&amp;strFileNotifyInfo, sizeof(strFileNotifyInfo), TRUE, FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME | FILE_NOTIFY_CHANGE_LAST_WRITE, &amp;dwBytesReturned, NULL, NULL) == 0)
      {
          Exit(GetLastErrorAsString());
      }
      else
      {
          char fileName[MAX_PATH] = ""; 
          DWORD offset = 0;
          do {
              fni = (FILE_NOTIFY_INFORMATION*)(&amp;strFileNotifyInfo[offset]);
              int ret = ::WideCharToMultiByte(CP_ACP, 0, fni-&gt;FileName, fni-&gt;FileNameLength / sizeof(WCHAR), fileName, sizeof(fileName), NULL, NULL);
              string Test = Dir;
              Test += "\\";
              Test += fileName;
              switch (fni-&gt;Action) {
                  case FILE_ACTION_ADDED:
                      if (boost::filesystem::is_directory(Test)) {
                          cout &lt;&lt; "Directory added: " &lt;&lt; Dir &lt;&lt; "\\" &lt;&lt; fileName &lt;&lt; endl;
                          }
                      else {
                          cout &lt;&lt; "File added: " &lt;&lt; Dir &lt;&lt; "\\" &lt;&lt; fileName &lt;&lt; endl;
                          }
                      break;
                  case FILE_ACTION_MODIFIED:
                      if (boost::filesystem::is_directory(Test)) {
                          cout &lt;&lt; "Directory modified: " &lt;&lt; Dir &lt;&lt; "\\" &lt;&lt; fileName &lt;&lt; endl;
                          }
                      else {
                          cout &lt;&lt; "File modified: " &lt;&lt; Dir &lt;&lt; "\\" &lt;&lt; fileName &lt;&lt; endl;
                          }
                      break;
                  case FILE_ACTION_REMOVED:
                      if (boost::filesystem::is_directory(Test)) {
                          cout &lt;&lt; "Directory removed: " &lt;&lt; Dir &lt;&lt; "\\" &lt;&lt; fileName &lt;&lt; endl;
                          }
                      else {
                          cout &lt;&lt; "File removed: " &lt;&lt; Dir &lt;&lt; "\\" &lt;&lt; fileName &lt;&lt; endl;
                          }
                      break;
                  case FILE_ACTION_RENAMED_NEW_NAME:
                      if (boost::filesystem::is_directory(Test)) {
                          cout &lt;&lt; "Directory renamend (NEW): " &lt;&lt; Dir &lt;&lt; "\\" &lt;&lt; fileName &lt;&lt; endl;
                          }
                      else {
                          cout &lt;&lt; "File renamed (NEW): " &lt;&lt; Dir &lt;&lt; "\\" &lt;&lt; fileName &lt;&lt; endl;
                          }
                      break;
                  case FILE_ACTION_RENAMED_OLD_NAME:
                      if (boost::filesystem::is_directory(Test)) {
                          cout &lt;&lt; "Directory renamed (OLD): " &lt;&lt; Dir &lt;&lt; "\\" &lt;&lt; fileName &lt;&lt; endl;
                          }
                      else {
                          cout &lt;&lt; "File renamed (OLD): " &lt;&lt; Dir &lt;&lt; "\\" &lt;&lt; fileName &lt;&lt; endl;
                          }
                      break;
                  default:
                      if (boost::filesystem::is_directory(Test)) {
                          cout &lt;&lt; "Directory UNDISCOVERED ACTION: " &lt;&lt; Dir &lt;&lt; "\\" &lt;&lt; fileName &lt;&lt; endl;
                          }
                      else {
                          cout &lt;&lt; "File UNDISCOVERED ACTION: " &lt;&lt; Dir &lt;&lt; "\\" &lt;&lt; fileName &lt;&lt; endl;
                          }
                      break;
                  }
              ::memset(fileName, '\0', sizeof(fileName));
              offset += fni-&gt;NextEntryOffset;
              }
          while (fni-&gt;NextEntryOffset != 0);
          cout &lt;&lt; "Loop: " &lt;&lt; nCounter++ &lt;&lt; endl;
      }
  }
  </code></pre>
  
  <p>Some sample output on renaming a file from <code>file.txt</code> to <code>file2.txt</code> in the map named: <code>MAP</code>:</p>
  
  <pre><code>File renamed (OLD): DIRPATH\MAP\file.txt
  Directory UNDISCOVERED ACTION: DIRPATH
  Loop: 0
  Directory modified: DIRPATH\MAP
  Loop: 1
  </code></pre>