views:

31

answers:

2

The File.listFiles() method lists the contents of a folder. On windows however, when you call the method on your root drive, it also yields some - in lack of a better phrase - "virtual" folders (Like "Documents and Settings", which got replaced by "Users" in recent windows versions).

If you then call listFiles() for one of those "virtual" folders, it always returns null. And that's where my problem is, as I want to recursively walk through all the folders. I need a way to filter those folders out and preferably not by checking their names...

There are also some additional folders, that the normal user doesn't see (like "System Volume Information" or the Recycle-bin folder for that drive), and I would be glad to find a method of filtering those out as well without blacklisting the names.

Unfortunately, those folders behave just like real ones. What I'm looking for is something like a File.isSystemFolder()-method.

I'm really thankful for any advice

+2  A: 

You can filter those hidden system folders by testing if File#isHidden() returns true.

I am not sure about "virtual" folders. I think distinguishing them is only possible with the New IO 2 which is coming in Java 7. I'll have to check that first yet on a Vista/Win7 machine (I'm currently on XP).


Update: I did a quick test at our 2K3 Server, the Documents and Settings by default also returns true for File#isHidden() while Users don't. You could make use of that as well.

BalusC
thx, I'm going with that now
DeX3
A: 

I don't think you'll be in luck with a File.isSystemFolder method, because I can't imagine it would be easy to impose a definition of "system folder" that's broad enough for JVMs on a range of operating systems to be able to implement correctly and usefully. As Balus suggests, checking for non-hidden folders is a good start and might well be enough on its own (since this replicates what they would see in an explorer shell).

About the "virtual" folders - first, are you sure that they're virtual? Is C:\Documents and Settings (or later, C:\Users) not the real, canonical path for this folder? If it's not, then you could easily weed these out on windows by seeing whether getCanonicalPath().equals(getAbsolutePath()) (so long as you're prepared for a few false positives with superfluous .s and ..s).

If this doesn't work, then they're just normal folder - you need to think about what logical properties these folders have that make them "virtual". Or in other words, come up with some predicate that can be expressed in terms of the methods on File that captures what you want to filter. Even I'm not sure right now quite what would and would not fit, based on your English description.

Andrzej Doyle
Thanks for the advice, the canonical and absolute paths are the same unfortunately, so I'm gonna take the isHidden() approach. Maybe this isn't such a bad choice after all, for the user might want to avoid hidden folders here in the first place ^^
DeX3