Welcome to MSDN Blogs Sign in | Join | Help

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));
}
Published Friday, September 19, 2008 7:00 AM by oldnewthing
Filed under:

Comments

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

Friday, September 19, 2008 11:42 AM by blah

I had a hunch that your line "Of course, now that I told you how to do it, I have to tell you that this is not how to do it." was misleadingly bogus. That was exactly how to do it, the way anybody would have interpreted 'it' in that context.

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

Friday, September 19, 2008 12:05 PM by John

What if somebody removes the Hidden and System attributes on the recycler folder?

The horror...the horror...

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

Friday, September 19, 2008 12:17 PM by RUF

> What if somebody removes the Hidden and System attributes on the recycler folder?

User will see it, as expected.

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

Friday, September 19, 2008 12:23 PM by John

> User will see it, as expected.

I disagree about it being expected, at least from a high-level perspective.  The question was how to skip over Recycle Bin folders.  If I remove the attributes, the folder is still a Recycle Bin folder (my deleted files still get moved there) yet it will not get skipped over using this method.

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

Friday, September 19, 2008 12:27 PM by John

Whoops.  I should have said the question was how to skip over "weird" folders.  Removing the attributes from the recycler folder does not remove the "weirdness" associated with the folder.

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

Friday, September 19, 2008 12:59 PM by Rick C

Yes, but you guys are now talking about doing weird things, that normal users probably don't normally do.  Why would you want to remove the hidden attribute from the Recycle Bin folder, especially if you know enough to know how to do it?  There's already several ways to see inside it without changing its attributes.

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

Friday, September 19, 2008 1:04 PM by Dan

Nope, but the hidden file attribute is INTENDED to hide folders that the typical user doesn't need to know or care about.  This fits the customer's definition of "weird" pretty well.

The system attribute is intended to mark system-critical files that should be hidden as allowing the user to do stuff to them could make their system unstable.  This might also be part of the "weird".

If the hidden file attribute is removed, we can't do much about that.  Basically we have here a simple and easy solution that will cover 98.9% of cases.  For 0.1% (hidden file attribute removed by a program without user consent) IMO the work that would have to be done would not be worth the effort to clean up someone else's mistake (I can't see it being intentional).  The last 1% would be if the attribute was intentionally removed by the user, in which case it's likely they did it on purpose and would expect and want the folder visible for whatever reason.

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

Friday, September 19, 2008 2:08 PM by Dave

Windows is sending some mixed messages about indexing. I have "Show hidden/system files" turned on in Explorer. If I right-click the Recycler folder and select Properties, then click Advanced, it has a check next to "For fast searching, allow Index Server to index this folder" even though both hidden and system are enabled for that folder.

Also, by default XP's "System Volume Information" folder that holds restore points is ACLed so that not even administrators can get into it. You can change that, of course, but it also serves as a barrier to an indexer that might ignore the hidden/system bits.

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

Friday, September 19, 2008 3:44 PM by Jules

The problem with this approach is that there's nothing preventing "non-weird" directories from being marked hidden&system.  Sure, there aren't any by default, but I certainly wouldn't want to exclude from my indexing any data that (e.g.) malware was attempting to hide from the user, and I've certainly seen malware using this strategy in the past.

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

Friday, September 19, 2008 4:29 PM by David Walker

Jules:  I think, by definition, all directories marked Hidden and System are "weird".  

Pretend we have a new attribute, called "Weird".  Any directories with this attribute set are therefore weird and should be skipped by user code.  Now, nothing will prevent malware from marking its data as Weird.  

You have just moved the problem, and you don't gain anything.  

We're back to this: everything marked Hidden and System is "weird" by definition.  The fact that malware can mark itself as Hidden & Ssytem is a different problem, which is not easy to solve, but it doesn't make this approach wrong.

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

Friday, September 19, 2008 4:51 PM by Mike Dunn

Could you use PathIsSystemFolder to do the check as well?

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

Friday, September 19, 2008 5:01 PM by mikeb

What should I consider directories that have only one of the hidden or system attributes set?

Normal or even weirder than weird?

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

Friday, September 19, 2008 9:08 PM by Mike Williams

This memo needs to go to the Windows Media Player team since v11 of the player keeps returning to index the Recycle Bin.

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

Saturday, September 20, 2008 12:13 AM by Joe Butler

The real wtf is that the developers probably went into Windows Explorer and said to themselves, "Hmm.  Let's just untick 'hide system and hidden files' to see what sort of stuff is hidden from us.  Oh, yes! - lots of things we don't want turning up in our product's scan...  Now, I wonder how we can figure out if we have one of these hidden, system files ourselves..."

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

Saturday, September 20, 2008 1:01 AM by Dan

Dave: Why shouldn't you be able to search the Recycle Bin?  Makes sense to me.

Let's say you delete a folder with documents you think you don't need any more, but there's one file in there you forgot you put there.  You go looking for that file later, but can't remember where you put it.

So you use search.  Since the Recycle Bin is indexed, you find it and are able to save it (yay!).

The Recycler folder may be intended to be hidden from the user, but the files in it certainly aren't.

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

Saturday, September 20, 2008 10:12 PM by Bob Riemersma

You see this in many support roles.

People often fail to come to you with the actual problem to be solved.  They've started down the blind alley of some random solution candidate and only ask for help in that blinkered context.

At least this seems to me to be the point Raymond is making here.  Obvious?  Yes.  So why all of this discussion?  Nod and move on.

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

Sunday, September 21, 2008 12:11 AM by wozza

It's my computer. If I want to search a hidden or system folder, that's totally my right. Give me a search tool that searches these places. And don't dare tell me what I should and shouldn't look at.

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

Monday, September 22, 2008 10:12 AM by ERock

Does this count as an example of leaky abstractions?

New Comments to this post are disabled
 
Page view tracker