directory size





3
Date Submitted Thu. Mar. 2nd, 2006 5:23 AM
Revision 1 of 1
Syntax Master dannyboy
Tags File | Folder | VBSCRIPT
Comments 0 comments
this function returns the aggregated size
of all files in a directory and its subdirectories

Function getDirectorySize(pCurrentDir)
        Dim numFSize
        Dim numDSize
        Dim strQuery
        Dim refItem
        Dim colFiles
        Dim colSubdirs
        numFSize = 0
        numDSize = 0
        'first get a reference to all files in the directory
        strQuery = "ASSOCIATORS OF {Win32_Directory='
" & pCurrentDir.Name & "'} WHERE AssocClass=CIM_DirectoryContainsFile Role=GroupComponent ResultRole=PartComponent"
        set colFiles = refWMIService.ExecQuery(strQuery)
        '
loop through each file and add the size of each to numFSize
        For Each refItem In colFiles
                numFSize = numFSize + refItem.FileSize
        Next
        set colFiles = Nothing
        'now get a reference to all the subdirectories
        strQuery = "ASSOCIATORS OF {Win32_Directory='
" & pCurrentDir.Name & "'} WHERE AssocClass=Win32_SubDirectory ResultRole=PartComponent"
        set colSubdirs = refWMIService.ExecQuery(strQuery)
        '
loop through each subdirectory, and add its
        'size to numDSize by recursively calling this Function
        For Each refItem in colSubDirs
                numDSize = numDSize + getDirectorySize(refItem)
        Next
        set colSubdirs = Nothing
        '
finally, print stats and return the total size
        WScript.echo pCurrentDir.Name & ": " & numFSize & " bytes in files - " & numDSize & " bytes in subdirs"
        getDirectorySize = numFSize + numDSize
End Function
 

Comments

There are currently no comments for this snippet.

Voting