Can I Use a Script to Determine the Size of a Folder on a Remote Computer?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! Can I use a script to determine the size of a folder on a remote computer?

— SS

SpacerHey, Scripting Guy! AnswerScript Center

Hey, SS. Boy, you’d think you’d be able to do that, wouldn’t you? Especially if you’ve glanced through the WMI class Win32_Directory and noticed the property FileSize. Based on that, you might think, “Well, obviously I could just write a WMI script to get the folder size.” In turn you might write a script very similar to this one, which tries to determine the size of the folder C:\Scripts on the computer atl-ws-01:

strComputer = “atl-ws-01”
Set objWMIService = GetObject _
    (“winmgmts:” & “!\\” & strComputer & “\root\cimv2”)
Set colFolders = objWMIService.ExecQuery _
    (“Select * from Win32_Directory where Name = ” _
        & “‘c:\\scripts'”)
For Each objFolder in colFolders
    Wscript.Echo objFolder.FileSize
Next

Unfortunately, this script won’t do you any good, not because of any problem with the scripting code or the scripting syntax, but simply because the FileSize property doesn’t work. It doesn’t matter what folder you connect to and it doesn’t matter whether the folder is on the local computer or a remote computer: the FileSize will always come back Null.

Note. We should mention that there is also a FileSize property in the CIM_DataFile class, the WMI class used for managing files. That FileSize property works; it will actually tell you the size, in bytes, of a file.

So what do we do now? Well, you can always use the FileSystemObject to determine the size of a folder. Here, for example, is a script that tells you the size of our C:\Scripts folder:

Set objFolder = objFSO.GetFolder(“C:\Documents and Settings”)
Set objFile = objFSO.CreateTextFile(“c:\scripts\folder_size.txt”)
Wscript.Echo objFolder.Size

The only problem is that the FileSystemObject is designed to work locally, and the folder you want to size is located on a remote computer. So do we know of a way to work around this problem? Well, we know at least one.

If you’re using Administrative shares (you know, like C$ and D$), then you can get at this folder information by connecting to the appropriate Administrative share. In other words, you can use a script similar to this, which uses the UNC path \\atl-ws-01\C$\Scripts to get at the C:\Scripts folder on the remote computer:

Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set objFolder = objFSO.GetFolder(“\\atl-ws-01\C$\Scripts”)
Wscript.Echo objFolder.Size

This works great, provided you’re using Administrative shares. But what if you’ve disabled those shares on your computers? What then?

Well, to tell you the truth, you might be out of luck, unless you want to run your folder size script as a logon or logoff script. If you have a shared folder of some kind on the remote computer, you could also copy the folder size script to the remote machine, run it locally, grab the data, and then delete it again; we showed a similar technique in an earlier Hey, Scripting Guy! column.

And what if you don’t have any shared folders on the remote computer? Unfortunately, we don’t have much in the way of suggestions, other than perhaps searching the Internet for a third-party utility that can retrieve the size of files and folders on a remote machine.

0 comments

Discussion is closed.

Feedback usabilla icon