Get user profile size in vbscript
- by Cameron
Hello,
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