September 19th, 2008

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

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));
}

Topics
Code

Author

Raymond has been involved in the evolution of Windows for more than 30 years. In 2003, he began a Web site known as The Old New Thing which has grown in popularity far beyond his wildest imagination, a development which still gives him the heebie-jeebies. The Web site spawned a book, coincidentally also titled The Old New Thing (Addison Wesley 2007). He occasionally appears on the Windows Dev Docs Twitter account to tell stories which convey no useful information.

0 comments

Discussion are closed.