VBScript to list folders by size
From Tech-Wiki
More actions
VBScript example that scans folders on a selected local drive and writes folder sizes to a semicolon-delimited CSV file.
Original sample preserved
The script below is retained from the legacy article as written. Test it in a non-production location before using it against large filesystems or adapting it for automation.
Output
The script writes its output to:
C:\foldersize.csv
The user can enter a drive letter or * to search local fixed drives.
Script
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