Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I am trying to get the size of a user's local profile using VBScript. I know the directory of the profile (typically "C:\Users\blah").

The following code does not work for most profiles (Permission Denied error 800A0046):

Dim folder
Dim fso

Set fso = WScript.CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder("C:\Users\blah")
MsgBox folder.Size    ' Error occurs here

Is there another way to do this?

UPDATE:
I did some deeper digging and it turns out that the Permission Denied error occurs if permission is denied to some subfolders or files of the directory whose size I wish to get. In the case of user profiles, there's always a few system files that even the Administrator group does not have permission to access.

To get around this, I wrote a function that tries to get the folder size the normal way (above), then, if the error occurs, it recurses into the subdirectories of the folder, ignoring folder sizes that are permission denied (but not the rest of the folders).

Dim fso
Set fso = WScript.CreateObject("Scripting.FileSystemObject")

Function getFolderSize(folderName)
    On Error Resume Next

    Dim folder
    Dim subfolder
    Dim size
    Dim hasSubfolders

    size = 0
    hasSubfolders = False

    Set folder = fso.GetFolder(folderName)
    ' Try the non-recursive way first (potentially faster?)
    Err.Clear
    size = folder.Size
    If Err.Number <> 0 then     ' Did not work; do recursive way:
        For Each subfolder in folder.SubFolders
            size = size + getFolderSize(subfolder.Path)
            hasSubfolders = True
        Next

        If not hasSubfolders then
            size = folder.Size
        End If
    End If

    getFolderSize = size

    Set folder = Nothing        ' Just in case
End Function
share|improve this question
    
Are you strictly limited to VBScript? –  squillman Feb 5 '10 at 4:48
    
The rest of my script has to be in VBScript, but I could call an external program, I suppose. –  Cameron Feb 5 '10 at 14:14

5 Answers 5

Like you said, you have a permission denied error. Does the account this script is running under actually have permissions to traverse these user profile folders and calculate the size? If you can't view it manually using Explorer, your script isn't going to do any better.

share|improve this answer
    
Thanks for the response! You're right, but fortunately I do have Administrator permissions. I can view the size just fine in Explorer. –  Cameron Feb 5 '10 at 14:15
    
Sometimes the simplest answer eludes us. It's good that you found the real culprit though and unfortunate that the you had to resort to a recursive solution with error trapping. –  Ryan Bolger Feb 5 '10 at 17:50

Thanks for this!!

I thought I'd run into some whacky 2008 issue where permision is seemingly allowed, but not...

I need the code for a script that checks the physical size of the Recycle Bin, and compares it againt what the user or administrator would see.

So far, I have found between 2gig and 8gig of lost files on all my servers.

The grunt of the code is simply:

Const RECYCLE_BIN = &Ha&

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

Set RecycledObjects = objFolder.Items
For Each objItem in RecycledObjects
    TotalVisibleSize = TotalVisibleSize  + objItem.Size
Next


WriteLog "Obtaining size for C:\" & BinFolder, LOG_VERBOSE
WriteLog "Trying to get size", LOG_VERBOSE
TotalPhysicalSize = getFolderSize("c:\" & BinFolder)

if objFSO.FolderExists("d:\" & BinFolder) then
    WriteLog "Obtaining size for D:\" & BinFolder, LOG_VERBOSE
    TotalPhysicalSize = TotalPhysicalSize + getFolderSize("d:\" & BinFolder)
end if

The rest is up to you!

Thanks again Michael.

share|improve this answer

I created a script to get the local user profiles and their size from all Windows XP and Windows 7 machines in Active Directory. The script can be found here:

Active Directory: VBscript to enumerate the local profile size of all computers and users in Active Directory

share|improve this answer

@Ryan Bolger

You obviously are missing the point of this script. vbscript has restrictions on special folders whether you can access that folder or not thru the Explorer makes no difference. For instance, you may have access to the My Documents folder but in vbscript that is considered a special folder and will give you a permission denied no matter what Admin level you are, even if it's your own folder.

share|improve this answer

If you want to use powershell this one is nice too though this one is specifically written for the size of My Documents folder: http://gallery.technet.microsoft.com/ScriptCenter/en-us/149ef544-a34a-4896-b6c3-b09491757f04

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.