title: FindFirstFile/FindNextFile not returning all files in folder
body: <p><em>Note: The question suggested as a duplicate discusses <code>CreateFile</code>, <code>ERROR_FILE_NOT_FOUND</code>, having existing handles on a file, and flagging a file to be deleted later. While it is a similar topic, none of those issues relate to my case.</em></p>
  
  <p>I have the following method to delete a directory. I've been using it for a while without problems.</p>
  
  <p>But recently, I've really been putting it through its paces with a large task deleting directories and files over a network path (just a USB drive connected to my router).</p>
  
  <p>Everything seems to work great except for one area where it is not deleting all the files in a directory and so <code>RemoveDirectory()</code> fails. (I have added each file to a list and verified that list did not contain the files that were not deleted.)</p>
  
  <p>All files are named very similarly and there is nothing unusual about the name of the files that are not returned or deleted. If I run the program again, it will delete the remaining files and then have the same error a little later on another directory.</p>
  
  <pre><code>bool CBackupWorker::DeleteDirectory(LPCTSTR lpszName)
  {
      if (!DirectoryExists(lpszName))
      {
          ASSERT(false);  // Unexpected
          return true;
      }
  
      CFileFind find;
      BOOL bContinue = find.FindFile(AppendPath(lpszName, _T("*")));
      while (bContinue)
      {
          bContinue = find.FindNextFile();
          if (find.IsDirectory())
          {
              if (!find.IsDots())
              {
                  if (!DeleteDirectory(find.GetFilePath()))
                      return false;
              }
          }
          else
          {
              if (find.IsReadOnly())
                  ClearReadOnlyAttribute(find);
              if (!::DeleteFile(ConvertToExtendedLengthPath(find.GetFilePath())))
              {
                  LogErrorV(::GetLastError(), _T("ERROR DELETING FILE : '%s'"), (LPCTSTR)find.GetFilePath());
                  return false;
              }
          }
      }
  
      CString sPath = ConvertToExtendedLengthPath(lpszName);
      DWORD dwAttributes = ::GetFileAttributes(sPath);
      if (dwAttributes != INVALID_FILE_ATTRIBUTES &amp;&amp; (dwAttributes &amp; FILE_ATTRIBUTE_READONLY))
          ::SetFileAttributes(sPath, dwAttributes &amp; (DWORD)~FILE_ATTRIBUTE_READONLY);
      if (!::RemoveDirectory(sPath))
      {
          LogErrorV(::GetLastError(), _T("ERROR DELETING DIRECTORY : '%s'"), lpszName);
          return false;
      }
      return true;
  }
  </code></pre>
  
  <p>A couple of notes on the code: <code>ConvertToExtendedLengthPath()</code> adds a prefix so that paths longer than <code>MAX_PATH</code> will be allowed; however, although these names are fairly long, none of them exceed <code>MAX_PATH</code>. (In this case, the method just returns the input value.)</p>
  
  <p>Also, I'm removing the read-only attribute on files and directories that have this attribute. But again, I've confirmed this is not coming into play on any of the files I'm currently working with.</p>
  
  <p>Finally, this is not a case where the files I'm working with are changing. I am the only one with access to this external drive.</p>
  
  <p>Has anyone seen any case where <code>FindFirstFile</code>/<code>FindNextFile</code> will miss a few files? Or where accessing files over a network share could interfere with the behavior of these functions?</p>