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 &&
Comments (18)
  1. blah says:

    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.

  2. John says:

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

    The horror…the horror…

  3. RUF says:

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

    User will see it, as expected.

  4. John says:

    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.

  5. John says:

    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.

  6. Rick C says:

    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.

  7. Dan says:

    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.

  8. Dave says:

    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.

  9. Jules says:

    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.

  10. David Walker says:

    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.

  11. Mike Dunn says:

    Could you use PathIsSystemFolder to do the check as well?

  12. mikeb says:

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

    Normal or even weirder than weird?

  13. Mike Williams says:

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

  14. Joe Butler says:

    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…"

  15. Dan says:

    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.

  16. Bob Riemersma says:

    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.

  17. wozza says:

    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.

  18. ERock says:

    Does this count as an example of leaky abstractions?

Comments are closed.

Skip to main content