Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I determine the size of all the items currently in the Recycle Bin?

-- T D-L

SpacerHey, Scripting Guy! AnswerScript Center

Hey, T D-L. You know, if we wanted to, the Scripting Guys could probably change our name to the Recycle Bin Guys; that’s because we get so many questions about scripting and the Recycle Bin. So why haven’t we changed our name? Well, for one thing, we wouldn’t know what to do with all these leftover Scripting Guys T-shirts. Perhaps more important, though, is the fact that we usually don’t have a very good answer when it comes to Recycle Bin questions. Can you send a file to the Recycle Bin rather than deleting it altogether? Well, kind of, but you still have to respond to the “Are you sure you want to send this file to the Recycle Bin?” prompt that appears. How about emptying the Recycle Bin? Same thing: you can do it, but you have to answer the “Are you sure you want to empty the Recycle Bin?” prompt that appears. It’s not very elegant, and it’s anything but fully automated.

However, this question we can help you with. Again, we don’t have the absolute perfect solution; for one thing, the script works only on the local computer. But when it comes to Recycle Bin questions, this is about as good as it gets:

Const RECYCLE_BIN = &Ha&
Const FILE_SIZE = 3

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(RECYCLE_BIN)

Set colItems = objFolder.Items

For Each objItem in colItems
    strSize = objFolder.GetDetailsOf(objItem, FILE_SIZE)
    arrSize = Split(strSize, " ")
    intSize = intSize + CLng(arrSize(0))
Next

Wscript.Echo intSize & " KB"

Our script starts out by defining a pair of constants. RECYCLE_BIN (which is assigned the hexadecimal value &Ha&) will be used to tell the script which special folder to bind to. Meanwhile, FILE_SIZE (with a value of 3) will be used to tell the script which folder item property we want to retrieve. That might not make much sense to you right now, but keep your fingers crossed and it should all become clear in a minute or two.

With these two constants in tow we create an instance of the Shell.Application object and then use the Namespace method to bind to the Recycle Bin. That’s what takes place here:

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(RECYCLE_BIN)

Now, in a perfect world, once we made a connection to the Recycle Bin we could simply say, “Hey, Recycle Bin: what exactly is the size of all your current items?” Unfortunately, though, this is not a perfect world. (If it was, the Scripting Guy who writes this column would actually be the Scripting Guy who plays centerfield for the Yankees.) Because of the special nature of the Recycle Bin folder (as well as some of the eccentricities of the Shell object) we have to manually calculate the size, something we can do by adding up the size of each and every item in the Recycle Bin. Before we can do that, however, we need to grab a collection of all the items currently in the Recycle Bin. That’s what this line of code is for:

Set colItems = objFolder.Items

As soon as we have our collection we can then set up a For Each loop to walk through all the items (that is, all the items in the Recycle Bin) and add up the file sizes:

For Each objItem in colItems

This is usually about the time where we say, “And from here on the rest of is easy.” But remember, this is the Recycle Bin we’re dealing with: nothing is ever easy when it comes to the Recycle Bin. Once we have a collection of items we can use the GetDetailsOf method to retrieve individual property values for each item in the collection. Because all we want is the file size (which, as far as the Shell object is concerned, has a property value of 3) we can use this line of code to return the file size for each item in the Recycle Bin (incidentally, this includes folders as well as files):

strSize = objFolder.GetDetailsOf(objItem, FILE_SIZE)

On paper, that looks pretty good. But, as you probably guessed, we a have problem: all our file sizes come back like this, with the string KB attached to the end:

100 KB
200 KB
300 KB

Why is that a problem? Well, try adding 100 KB + 200 KB + 300 KB and you’ll see why it’s a problem. (Hint: You’ll get a “Type mismatch” error because you’re trying to add together string values.)

See, that’s another reason we haven’t switched over and become the Recycle Bin Guys. Dealing with the Recycle Bin simply takes too much effort!

Fortunately we can easily take care of the KB tacked onto the end of each file size. How? Well, this line of code uses the VBScript Split function to divide each file size into an array:

arrSize = Split(strSize, " ")

By splitting on the blank space we end up with an array consisting of two items. The first item (with an index number of zero) is the file size itself; the second item is the string KB. For example, if the file size is 100 KB then the array arrSize will consist of these two items:

100

KB

Now, at long last, we’re in business. With the file size safely tucked away in the first item in the array we can use this line of code to start tallying up the sizes of all the items in the Recycle Bin:

intSize = intSize + CLng(arrSize(0))

What we’re doing here is using a variable named intSize to keep track of the total size of all the items. In this line of code we assign intSize the value of whatever is currently in intSize plus the value of the first item in the array (which, as you recall, has an index number of 0).

For example, the first time through the loop intSize will be equal to 0. If our first file size is 100 then intSize will be assigned this value:

0 + 100

The next time through the loop intSize will be equal to 100. If the size of the second file is 200 then intSize will be assigned this value:

100 + 200

If those are the only two files in the Recycle Bin then the size of the Recycle Bin is 300 KB, a fact that we echo after exiting the For Each loop.

Good question: why did we use the CLng function when working with the file size? Well, the CLng function is designed to “cast” a variable as a long integer. We left CLng out of the first iteration of this script only to discover that VBScript still wanted to treat the file size (e.g., 100) as a string variable. Because of that we got back a Recycle Bin size that looked like this:

1,2407,6407,0125,20410,06423,204 KB

Interesting, but not quite the same as the actual size: 54364 KB. By using the CLng function we simply ordered VBScript to treat arrSize(0) as a long integer, and everything worked just fine.

And now it’s off to the batting cages. After all, seeing as how it is possible to write a script that works with the Recycle Bin, well, then maybe it’s also possible that this Scripting Guy can play centerfield for the Yankees. We’ll let you know how that turns out.