*Note: The question suggested as a duplicate discusses `CreateFile`, `ERROR_FILE_NOT_FOUND`, 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.*

I have the following method to delete a directory. I've been using it for a while without problems.

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).

Everything seems to work great except for one area where it is not deleting all the files in a directory and so `RemoveDirectory()` fails. (I have added each file to a list and verified that list did not contain the files that were not deleted.)

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.

```
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 && (dwAttributes & FILE_ATTRIBUTE_READONLY))
        ::SetFileAttributes(sPath, dwAttributes & (DWORD)~FILE_ATTRIBUTE_READONLY);
    if (!::RemoveDirectory(sPath))
    {
        LogErrorV(::GetLastError(), _T("ERROR DELETING DIRECTORY : '%s'"), lpszName);
        return false;
    }
    return true;
}

```

A couple of notes on the code: `ConvertToExtendedLengthPath()` adds a prefix so that paths longer than `MAX_PATH` will be allowed; however, although these names are fairly long, none of them exceed `MAX_PATH`. (In this case, the method just returns the input value.)

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.

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.

Has anyone seen any case where `FindFirstFile`/`FindNextFile` will miss a few files? Or where accessing files over a network share could interfere with the behavior of these functions?