VBScript to list folders by size

From Tech-Wiki
Revision as of 02:41, 8 August 2016 by Fabricio.Lima (Talk | contribs)

Jump to: navigation, search

This script will generate a CSV file sorting the subfolders by size for a specified disk drive. Just copy and save as .vbs


On Error Resume Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
sTitle = "Folder Size"

'create the output file
Set LogFile = objFSO.CreateTextFile("C:\foldersize.csv", True)

strDrv = InputBox("Enter drive letter to search (letter only)" & vbcrlf & _
                vbcrlf & "Enter * to search all local drive letters", sTitle, "*")

If (strDrv = "") Then WScript.Quit

If strDrv = "*" Then
  Dim Drive
  For Each Drive in objFSO.Drives
    If Drive.DriveType = 2 Then
      Set objDrv = objFSO.GetFolder(Drive & "\")
      CheckSize objDrv
    End If
  Next
Else
  Set objDrv = objFSO.GetFolder(strDrv & ":\")
  CheckFolder objDrv
End If

LogFile.Close
MsgBox "Folder Size Completed." + vbCr + "Please check c:\foldersize.csv for details."

Sub CheckFolder (objCurrentFolder)

    For Each objFolder In objCurrentFolder.SubFolders
        if strcomp(objFolder.Name,"System Volume Information",1) then
		   LogItem objFolder
		End if
    Next

End Sub

Sub LogItem (objFolder)
   On Error Resume Next
   strOutput = CStr(objFolder.Name) + ";" + CStr(FormatNumber(objFolder.Size/1024/1024/1024,1)) + ";GB"
   LogFile.WriteLine strOutput

End Sub