Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VBScript

Script: List user's homedir size

0.00/5 (No votes)
26 Feb 2013CPOL 5.9K  
The script takes the folder name and searches the Active Directory for a corresponding user.

I compiled a vbscript that loops through all the sub folders of a given folder and print out the size. The script takes the folder name and searches the Active Directory for a corresponding user. If found it prints the name of the user in the output.

Use:

cscript homedirsize.vbs

Three input boxes will appear...

  1. Path where the homedir folders are located. E.g., d:\home
  2. Width, in characters, of the first output column.
  3. Width, in characters, of the second output column.
VB
'****************************************************************************************
'
' Name: Homedirsize.vbs
' 
' Retrieves the size of each subdirectory and matches them to an AD account.
' Outputs a list of directories, username and size.
'
' Written by: Kristofer Källsbo
' 2013-02-26 - http://www.hackviking.com
'
'****************************************************************************************
Option explicit
dim path, column1, column2, objRoot, domainname, fso, partline, i, rootFolder, folder

' get path of homedirs
path = inputbox("Enter path of homedirs:")

' get column widths
column1 = Cint(inputbox("Enter width of first output column:"))
column2 = Cint(inputbox("Enter width of second output column:"))

' get the current domain
Set objRoot = GETOBJECT("LDAP://RootDSE")
domainname = objRoot.GET("defaultNamingContext")

' get the file system object
Set fso = CreateObject("Scripting.FileSystemObject")

' print description lines
wscript.echo "homedirsize.vbs runned on " & Date & " - " & Time
wscript.echo ""
wscript.echo LeftJustified("Foldername", column1) & LeftJustified("Username", column2) & "Size (Mb)"

for i = 0 to column1 + column2 + 8
    partline = partline & "-"
next

wscript.echo partline

' start looping all the subfolders
Set rootFolder = fso.GetFolder(path)
For Each folder in rootFolder.SubFolders
    Dim folderSize
    folderSize = folder.Size
            
    wscript.echo LeftJustified(folder.Name, column1) & LeftJustified(FindUser(folder.Name, _
      domainname), column2) & FormatNumber(((folderSize/1024)/1024),2) & " Mb"
Next

Set fso = Nothing

FUNCTION FindUser(BYVAL UserName, BYVAL Domain) 
    Dim cn,cmd,rs
    SET cn = CREATEOBJECT("ADODB.Connection")
    SET cmd = CREATEOBJECT("ADODB.Command")
    SET rs = CREATEOBJECT("ADODB.Recordset")

    cn.open "Provider=ADsDSOObject;"
    
    cmd.activeconnection=cn
    cmd.commandtext="SELECT Name FROM 'LDAP://" & Domain & _
             "' WHERE sAMAccountName = '" & UserName & "'"
    
    SET rs = cmd.EXECUTE

    IF err<>0 THEN
        FindUser = 2
        wscript.echo "Error connecting to Active Directory Database:" & err.description
    ELSE
        IF NOT rs.BOF AND NOT rs.EOF THEN
                 rs.MoveFirst
                 FindUser = rs.Fields("Name").Value
        ELSE
            FindUser = "N/A"
        END IF
    END IF
    cn.close
END FUNCTION

Function LeftJustified(ColumnValue, ColumnWidth)
   If(ColumnWidth < Len(ColumnValue) OR ColumnWidth = Len(ColumnValue)) then
        LeftJustified = Left(ColumnValue, ColumnWidth - 1) & " "
    else
        LeftJustified = ColumnValue & Space(ColumnWidth - Len(ColumnValue))
    End if
End Function

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)