How Can I Determine the Size of the My Documents Folder?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I determine the size of the My Documents folder?

— SC

SpacerHey, Scripting Guy! AnswerScript Center

Hey, SC. The My Document folder is actually just another folder on the hard disk, one with a path similar to this: C:\Documents and Settings\kenmyer\My Documents. Of course, the actual path will vary depending on the name of the logged-on user; in addition, the My Documents folder can be redirected to another location, which means it isn’t necessarily in the C:\Documents and Settings folder either. What we’re trying to say is that the tricky part here is actually finding the My Documents folder; once you find it you can then use the FileSystemObject to determine the folder size (and we can do that because My Documents is, as we noted, just another folder on the hard disk).

So how do we find the My Documents folder? Well, there are a couple ways to do this, but we’re going to use the Shell object; that’s because the Shell object can be used to find the path to all sorts of special folders, including My Pictures, My Music, My Network Places, etc. Here’s a script that reports the size of the My Documents folder:

Const MY_DOCUMENTS = &H5&

Set objFSO = CreateObject(“Scripting.FileSystemObject”) Set objShell = CreateObject(“Shell.Application”)

Set objFolder = objShell.Namespace(MY_DOCUMENTS) Set objFolderItem = objFolder.Self strPath = objFolderItem.Path

Set objFolder = objFSO.GetFolder(strPath) Wscript.Echo objFolder.Size

We begin by defining a constant named MY_DOCUMENTS and assigning it the value &H5&, the value used by the Shell object to reference the My Documents folder. We then create two separate objects: objFSO (an instance of the FileSystemObject) and objShell (an instance of the Shell object).

Now the fun begins. We use the Namespace method to bind to the My Documents folder and then use the Self property to create an object reference to the FolderItem object that represents My Documents. (We won’t worry about why we have to do that; it’s just something that the Shell object requires.) Finally, we store the value of the Path property (e.g., C:\Documents and Settings\kenmyer\My Documents) into a variable named strPath.

Still with us? Now that we know the path to the My Documents folder we can use the FileSystemObject and the GetFolder method to bind to My Documents. Yes, we already did this once, but that was with the Shell object. Now we have to repeat the process with the FileSystemObject. Why? Well, the Shell object can locate the My Documents folder for us, but can’t report the folder size. The FileSystemObject can report the folder size, but can’t locate the My Documents folder for us. Thus we have to use both objects to complete this single task.

After we bind to My Documents using the FileSystemObject we can then echo the size of the folder using this line of code:

Wscript.Echo objFolder.Size

Granted, not the most straightforward process in the world but at least it returns the size of the My Documents folder. And that’s all we really care about.

0 comments

Discussion is closed.

Feedback usabilla icon