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

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

  • Comments 2
  • Likes
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.

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Hi Scripting Guy,

    I'm trying to create a simple script that will return a list of folders in the current directory and their respective sizes:

    Set objShell = CreateObject("WScript.Shell")

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Set strCurrentDirectory = objFSO.GetParentFolderName(Wscript.ScriptFullName)

    Set objFolder = objFSO.GetFolder("strCurrentDirectory")

    Set colSubfolders = objFolder.Subfolders

    For Each objSubfolder in colSubfolders

     Wscript.Echo objSubfolder.Name, objSubfolder.Size

    Next

    Yet it doesn't like the fact that I am trying to pass the current directory into a variable and errors out with: D:\dirsize.vbs(3, 1) Erreur d'exécution Microsoft BLOCKED SCRIPT Objet requis: '[string: "D:\"]'.

    Can you point me in the right direction please?

  • Scripting.Filesystemobject will fail, even if you are the local administrator, if you have the folders & files with ownership null the script will fail...