Wednesday, June 14, 2006
« Phew! Saved By Reflector! | Main | Utah .NET User Group on Biztalk 2006 »

Here's an interesting one.

I was futzing around today with the various classes in the System.IO namespace that deal with the file system (e.g. File, Directory, Path, FileInfo, etc).  Is it just me or does it seem like an omission to not have something like an .IsEmpty property?  Granted, this functionality doesn't exist even in the Windows API as such, but still...the .NET framework is all about making the task of developing software easier, more accessible.

Sure, there are a few strategies that one could employ to determine if a folder is indeed empty (none of which I like).  They usually involve getting the list of files and subdirectories and determining if there are none.  I don't really like these approaches because they involve getting the full list of files and folders first just to compare the count to zero (0).  For example:

private bool isDirectoryEmpty(string path) {
   string[] subDirs = Directory.GetDirectories(path);
   if ( 0 == subDirs.Length ) {
      string[] files = Directory.GetFiles(path);
      return ( 0 == files.Length );
   }
   return false;
}

Suppose the directory contained hundreds or thousands of files and/or directories?  A bunch of cycles are wasted simply enumerating all of the files only to discard the results.  Is there a better way using the .NET framework?

I propose there is and I have a better solution, but I'd like your thoughts and ideas.  I'll post my answer in another post.  (I'm hoping there's something built-in that I'm simply overlooking, but I'm not holding out for it)

(BTW: I really think you should be able to have code that resembles the following and be done with it.  I guess I could propose it to the .NET team (which I'll do).  If nothing else, we could wait until .NET 3.0 when we have extension methods at our disposal.)

if ( Directory.IsEmpty(@"C:\SomePath") ) {
   // happily proceed knowing that it was empty at the moment we called the method
   // ...but might not be at this moment :)
}

Tuesday, June 27, 2006 9:10:00 AM (Mountain Standard Time, UTC-07:00)
I know this is old by now but you said you'd post a follow up. My thought was to compare the structure of an empty directory to one of a non-empty one but that's easier said than done. One file or subdirectory is enough to declare the directory as non-empty but what about 0 byte files? Technically the directory is empty then but it has fileInfo that is enumerated.



From this example, the method you proposed seems to be the standard practice:

http://msdn2.microsoft.com/en-us/library/system.io.directory.aspx



Though file enumeration is rather quick in .NET, it is a lot of wasted cycles as you mention, almost too many. If it behaved like Outlook folders where the size is shown at the folder level it would be a little easier than enumerating every file and subdirectory to calculate the results.
Tuesday, June 27, 2006 9:43:00 AM (Mountain Standard Time, UTC-07:00)
Jeremy Brayton,



Thanks for your follow-up. I am still planning on posting my solution, but I didn't want to just 'put it out there' as quickly if no one was going to respond to my initial puzzle.



I agree, that technically a directory is empty with a zero-byte file, but even then, you can't simply delete the folder (because it has some contents) unless you explicitly identify that you want to delete it recursively.



What I find crazy is the fact that there really isn't a simple way to check to see if a Directory is empty (e.g. no files/directories contained) that is built-in.
Name
E-mail
Home page

Comment (Some html is allowed: a@href@title, b, i, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview