Thursday, November 5, 2009

VB Script to get folder and subfolders sizes with treedepth...

In order to have an automated overview of space amount used in my backup job, I made this vbs script.
Create a FolderList.txt file with pathes to folders you want to scan (don't leave blank lines)
Set the values you want for :
isProcessFiles : Get files size too
treeDepth : tree depth when scanning folders
computeAllFoldersSize : compute full size of all folders


Const isProcessFiles = 1
Const treeDepth = 3
Const computeAllFoldersSize = 1

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLogFile = objFSO.CreateTextFile(".\FolderSizes.log")

if computeAllFoldersSize = 1 then
  fullSize = 0
  Set objTextFile = objFSO.OpenTextFile(".\FolderList.txt", ForReading)
  Do Until objTextFile.AtEndOfStream
    strFolderPath = objTextFile.Readline
    Set objFolder = objFSO.GetFolder(strFolderPath)
    fullSize = fullSize + objFolder.size
  Loop
  objLogFile.Writeline "Size of all folders in list file : " & FormatNumber(fullSize/(1024*1024*1024),3) & " GB"
  objLogFile.Writeline ""
  objTextFile.Close
End If


if treeDepth < 1 then wscript.quit


'For all folders specified in the text file
Set objTextFile = objFSO.OpenTextFile(".\FolderList.txt", ForReading)

Do Until objTextFile.AtEndOfStream
  strFolderPath = objTextFile.Readline
  Set objFolder = objFSO.GetFolder(strFolderPath)
  ProcessFolder (objFolder)
Loop



Sub ProcessFolder(objCurrentFolder)
  objLogFile.Writeline FormatNumber(objCurrentFolder.Size/(1024*1024),0) & " MB " & objCurrentFolder.path
  if treeDepth > 1 then Call ProcessSubFolders (objCurrentFolder, 1)
End Sub



Sub ProcessSubFolders(ByRef objCurrentFolder, ByVal currentRecurseLevel)

  if currentRecurseLevel >= treeDepth then exit Sub

  recurseTabs = ""
  For value = 0 To currentRecurseLevel - 1
    recurseTabs = recurseTabs & " "
  Next

  if objCurrentFolder.SubFolders.count = 0 then
    if isProcessFiles = 1 then
      Call ProcessFiles (objCurrentFolder, currentRecurseLevel)
    end if
  else
    Call ProcessFiles (objCurrentFolder, currentRecurseLevel)
    For Each objCurrentSubFolder In objCurrentFolder.SubFolders
      objLogFile.Writeline recurseTabs & FormatNumber(objCurrentSubFolder.Size/(1024*1024),0) & " MB " & objCurrentSubFolder.path
      Call ProcessSubFolders (objCurrentSubFolder, currentRecurseLevel + 1)
    Next
  end if
End Sub



Sub ProcessFiles (ByRef objCurrentFolder, ByVal currentRecurseLevel)

   if currentRecurseLevel >= treeDepth then exit Sub

   recurseTabs = ""
   For value = 0 To currentRecurseLevel - 1
     recurseTabs = recurseTabs & " "
   Next

   For Each objCurrentFile In objCurrentFolder.Files
     objLogFile.Writeline recurseTabs & FormatNumber(objCurrentFile.Size/(1024*1024),0) & " MB " & objCurrentFile.path
   Next
End Sub

1 comment:

  1. A special thanks for this informative post. I definitely learned new stuff here I wasn't aware of !

    ReplyDelete