How Can I List All the Empty Folders on a Specified Drive?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I list all the empty folders (that is, folders with a size of 0 bytes) on a specified drive?

— DD

SpacerHey, Scripting Guy! AnswerScript Center

Hey, DD. You know, physicist Edwin Schrodinger once proposed a thought experiment in which a cat is placed in a sealed box, along with a canister of poison gas. The experiment is designed in such a way that there is exactly a 50-50 chance that the canister will release the gas, killing the cat instantly. According to quantum mechanics, until you open the box and check for yourself, the cat is somehow both dead and alive. One of the great paradoxes of modern life.

Note. So why is this just a thought experiment and not a real experiment? We don’t know for sure, but imagine this: you place a cat in a sealed box, along with a canister of poison gas. Now suppose that the canister doesn’t release the gas. Would you want to be the poor researcher who had to open the box and deal with what would likely be one crazy-mad cat? Obviously Schrodinger had no desire to be that person, either. (Editor’s Note: There’s also the small detail that you might actually kill a cat.)

Another famous paradox is the Grandfather Paradox. Suppose you go back in time and murder your own grandfather. Obviously that would mean that you would never be born. But if you were never born how were you able to go back in time and kill your own grandfather? And if you were able to go back in time, why the heck did you go back and kill your grandfather instead of bringing back a bunch of Willy Wonka Scrumdidlyuptious Bars, quite possibly the best candy bar ever made?

At the very least, go back and kill the grandfather of the person who decided to stop making the Scrumdidlyuptious Bar.

So is any of this relevant to retrieving a list of all the empty folders on a specified drive? Kind of; after all, it turns out that getting a list of empty folders is both harder than you might expect and, at the same time, easier than you might expect. Call it the Empty Folders Paradox. (We were going to call it the Grandfather Paradox, but that name was already taken.)

Let’s start off by talking about why it’s harder than you might expect. At first glance this sounds pretty easy; after all, you can use WMI’s Win32_Directory class to return a collection of all the folders on a drive. Can’t you just write a query that asks for all the folders that have a FileSize equal to 0?

Sure, except that you’ll be disappointed in the results. That’s because, as far as WMI is concerned, all folders have the same FileSize: no size. Because the FileSize property isn’t fully-implemented on the Win32_Directory class, FileSize always come back as Null.

Always.

That’s a problem: not only does it eliminate your ability to use WMI to retrieve a list of empty folders, but it also makes it a bit tricky to perform this task against remote computers. And yet, paradoxically, although the FileSize problem makes this task harder than you’d expect, the final solution (which uses the FileSystemObject) is easier than you might expect. In fact, that final solution requires no more code than this:

On Error Resume Next

Set FSO = CreateObject(“Scripting.FileSystemObject”) ShowSubFolders FSO.GetFolder(“C:\”)

Sub ShowSubFolders(Folder) For Each Subfolder in Folder.SubFolders If Subfolder.Size = 0 Then Wscript.Echo Subfolder.Path End If ShowSubFolders Subfolder Next End Sub

As you can see, we start out by implementing the On Error Resume Next statement (just in case we run into any problems). We then create an instance of the Scripting.FileSystemObject. That brings us to this line of code:

ShowSubFolders FSO.GetFolder(“C:\”)

ShowSubFolders is a “recursive subroutine” that we’ll use – repeatedly – in order to get at all the subfolders in C:\, including all the subfolders in those subfolders, and the subfolders in those sub-subfolders, etc. We won’t discuss recursion in any detail today; that goes a bit beyond the scope of this column. (If you thought the Grandfather Paradox was difficult to conceptualize, wait till you try tackling recursion!) However, you can find a brief introduction to recursion in the Microsoft Windows 2000 Scripting Guide.

Note. No, you don’t have to go back in time to the year 2000 in order to read the Scripting Guide; just access it online. If you do go back in time, though, say hi to your grandfather for us.

Inside our subroutine we use this line of code to retrieve a collection of all the top-level subfolders found in C:\:

For Each Subfolder in Folder.SubFolders

Good question: what do we mean by “top-level” subfolders? That means we’re going to get back a subfolder like C:\Scripts; however, we will not get back any sub-subfolders like C:\Scripts\WMI or C:\Scripts\ADSI. The SubFolders property works on only one level of subfolders at a time. But we’ll show you how to get around that issue in just a minute.

Once we have a collection of subfolders we use this line of code to determine whether the folder is empty (that is, whether or not it has a Size equal to 0):

If Subfolder.Size = 0 Then

If the Size is equal to 0 we echo back the folder Path. If the Size is not equal to 0, well, in that case we don’t do anything at all.

This is where things get a little paradoxical:

ShowSubFolders SubFolder

You’re right: we’re already inside the ShowSubFolders subroutine and yet now we’re calling that same very same subroutine. That sounds crazy (and it is), but such is the nature of recursion. The basic idea is this: we start out in C:\ and get a collection of folders. Let’s say the first folder we get back is C:\Scripts. We check the file size, then call the ShowSubFolders subroutine to get back a new collection, one consisting of all the subfolders found in C:\Scripts. Let’s assume there’s just one such subfolder: C:\Scripts\WMI. We check the file size and then call ShowSubFolders again, this time to see if C:\Scripts\WMI has any subfolders. This process continues until we run out of subfolders, in which case we go back to C:\ and check the next subfolder in that collection.

That’s OK; it gives us a headache, too. But it works and, fortunately, you don’t have to keep track of which subfolders you’ve looked at and which subfolders you haven’t looked at; VBScript takes care of that for you.

Note. Originally VBScript didn’t keep track of that stuff for you. But we sent Peter back in time and had him change that. He was supposed to bring us some candy bars, too, but he said he got hungry on the trip back

Believe it or not, that’s all we have to do. Run the script and you should get back a list of empty folders:

C:\Documents and Settings\Test\Local Settings\Temp
C:\Documents and Settings\Test\NetHood
C:\Documents and Settings\Test\PrintHood
C:\drvrtmp
C:\Inetpub\wwwroot\aspnet_client\system_web\2_0_50727
C:\Program Files\Accessories
C:\Program Files\Accessories\Imagevue
C:\Program Files\CA\eTrust Antivirus\ArcTemp

Notice that we have C:\Program Files\Accessories and C:\Program Files\Accessories\Imagevue. That’s a sign that we’re retrieving all the subfolders, and not just the top-level ones.

As an added bonus, it turns out that you can even use this script against remote computers, provided that you use administrative shares like C$. If that’s the case, then the following script will return a list of all the empty folders found on drive C of the computer atl-fs-01:

On Error Resume Next

Set FSO = CreateObject(“Scripting.FileSystemObject”) ShowSubfolders FSO.GetFolder(“\\atl-fs-01\C$”)

Sub ShowSubFolders(Folder) For Each Subfolder in Folder.SubFolders If Subfolder.Size = 0 Then Wscript.Echo Subfolder.Path End If ShowSubFolders Subfolder Next End Sub

So is this the end of today’s column? Well, we’re not sure. After all, to paraphrase Zeno’s Paradox, before you can reach the end of the column you have to reach the halfway point of the column. But before you can reach the halfway point of the column you have to reach the halfway point of the halfway point. But before you can reach – well, the gist of the paradox is that you never, ever reach the end of the column.

Which is probably the case for a lot of our befuddled readers.

0 comments

Discussion is closed.

Feedback usabilla icon