How can I tell that a directory is weird and should be excluded from the user interface?

Raymond Chen

Last time, we looked at a customer who wanted to know how to tell whether a given folder was a Recycle Bin folder or not. We answered the question as stated, but made the mistake of not looking at the problem the customer was trying to solve.

I need to know which folders are Recycle Bin folders so I can skip over them when searching the drive for content.

Ah, the real question isn’t “How can I tell whether a directory is a Recycle Bin folder?” but rather “How can I tell whether a directory contains weird stuff that we shouldn’t be showing to the user (of which Recycle Bin files are just one example)?”

The way to mark a folder as containing stuff that you shouldn’t bother the user with is to set both the FILE_ATTRIBUTE_HIDDEN and FILE_ATTRIBUTE_SYSTEM attributes. In addition to Recycle Bin directories, this also prevents you from searching weird things like System Restore points. (Though for some reason it doesn’t stop you from searching Temporary Internet Files; I don’t know whether that is a bug or a feature.)

Therefore, the answer to “How can I tell whether a directory contains weird stuff that we shouldn’t be showing to the user?” is to test for the hidden and system attributes.

BOOL IsWeirdDirectory(LPCTSTR pszDir)
{
  DWORD dwAttr = GetFileAttributes(pszDir);
  return dwAttr != INVALID_FILE_ATTRIBUTES &&
         ((dwAttr & (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN))
                 == (FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_HIDDEN));
}

0 comments

Discussion is closed.

Feedback usabilla icon