Get user profile size in vbscript

10,449

Solution 1

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.

Solution 2

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

Solution 3

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:
10,449

Related videos on Youtube

uranibaba
Author by

uranibaba

I love programming :-)

Updated on September 17, 2022

Comments

  • uranibaba
    uranibaba over 1 year

    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
    
    • squillman
      squillman about 14 years
      Are you strictly limited to VBScript?
    • uranibaba
      uranibaba about 14 years
      The rest of my script has to be in VBScript, but I could call an external program, I suppose.
  • uranibaba
    uranibaba about 14 years
    Thanks for the response! You're right, but fortunately I do have Administrator permissions. I can view the size just fine in Explorer.
  • Ryan Bolger
    Ryan Bolger about 14 years
    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.