FindFirstFile/FindNextFile finds folder that has been deleted with SHFileOperation

Viewed 599

I am using FindFirstFile and FindNextFile to show a list of files in a given folder in my application.

On occasion, I'm seeing cases where my application deletes a folder using SHFileOperation, but the folder can still be found by FindFirstFile. I've seen this problem reported previously on StackOverflow here: File deleted with remove function still shows up in FindFirstFile/FindNextFile. However, in my case I'm not using remove and the folder remains visible even after clearing the recycle bin. In addition, the folder is NOT visible in Windows file explorer. This makes me wonder what the file explorer is filtering on to ignore the file.

I tried looking at file attributes but only found that the deleted folder had FILE_ATTRIBUTE_DIRECTORY and FILE_ATTRIBUTE_VIRTUAL set. The FILE_ATTRIBUTE_VIRTUAL attribute is a bit mysterious (MSDN says it's reserved for system uses), so I tried ignoring folders that had that attribute set. However, it turns out that there are legitimate existing/active folders that have this attribute set, so I'm unable to use it as a filter.

Why is this folder showing up? How can I ignore it and not ignore existing (non-deleted) folders?

1 Answers

I found this remark from the MSDN page,

FindFirstFile function

Be aware that some other thread or process could create or delete a file with this name between the time you query for the result and the time you act on the information. If this is a potential concern for your application, one possible solution is to use the CreateFile function with CREATE_NEW (which fails if the file exists) or OPEN_EXISTING (which fails if the file does not exist).

It says you should double check for file is really there, using CreateFile API.

Related