Wednesday, June 23, 2021

System Administration: Get Folder Size Script

Author: Hans van der Zaag
Platform: Windows NT, Windows 2000, Windows XP
Type: VBScript

DESCRIPTION: This script calculates the size of all subfolders within a specified folder and sorts the data automatically into an Excel workbook. Getfoldersize.vbs is particularly useful when you want to find out which folder takes the highest amount of disk space.

Scroll down to view the script.
Download as text file.

System Administration: Get Folder Size Script

'	Name:		getfoldersize.vbs
'	Version:	1.0
'	Date:		7-5-2002
'	Author:		Hans van der Zaag
'	Description:	getfoldersize.vbs calculates the size of all subfolders within
'			a folder and sorts this data in an excel workbook

rootfolder = Inputbox("Enter directory/foldername: " & _
chr(10) & chr(10) & "(i.e. C:Program Files or " & _
"ServernameC$Program Files)" & chr(10) & chr(10), _
"Getfoldersize", "C:Program Files")

outputfile = "c:foldersize_" & Day(now) & Month(now) & Year(now) & ".xls"

Set fso = CreateObject("scripting.filesystemobject")
if fso.fileexists(outputfile) then fso.deletefile(outputfile)

'Create Excel workbook
set objXL = CreateObject( "Excel.Application" )
objXL.Visible = False

'Counter 1 for writing in cell A1 within the excel workbook
icount = 1

'Run checkfolder
CheckFolder (FSO.getfolder(rootfolder))

Sub CheckFolder(objCurrentFolder)

For Each objFolder In objCurrentFolder.SubFolders
 FolderSize = objFolder.Size
 Tmp = (FormatNumber(FolderSize, 0, , , 0)/1024)/1024
 ObjXL.ActiveSheet.Cells(icount,1).Value = objFolder.Path
 ObjXL.ActiveSheet.Cells(icount,2).Value = Tmp
 'Wscript.Echo Tmp & " " & objFolder.Path
	 'raise counter with 1 for a new row in excel 
 icount = icount + 1

'Recurse through all of the folders
For Each objNewFolder In objCurrentFolder.subFolders
CheckFolder objNewFolder

End Sub

'sort data in excel
objXL.Selection.Sort objXL.Worksheets(1).Range("B1"), _
  2, _
  , _
  , _
  , _
  , _
  , _
  0, _
  1, _
  False, _

'Lay out for Excel workbook 

objXL.Columns(1).ColumnWidth = 60
objXL.Columns(2).ColumnWidth = 15
objXL.Columns(2).NumberFormat = "#,##0.0"
objXL.Range("B1:B1").NumberFormat = "d-m-yyyy"
objXL.Selection.Font.Bold = True
objXL.Selection.Font.ColorIndex = 5
objXL.Selection.Font.Italic = True
objXL.Selection.Font.Size = 16
ObjXL.ActiveSheet.Cells(1,1).Value = "Survey FolderSize " 
ObjXL.ActiveSheet.Cells(1,2).Value = Day(now) & "-" & Month(now) & "-"& Year(now)
ObjXL.ActiveSheet.Cells(3,1).Value = UCase(rootfolder)
ObjXL.ActiveSheet.Cells(5,1).Value = "Folder"
ObjXL.ActiveSheet.Cells(5,2).Value = "Total (MB)"

'Finally close the workbook
Set ObjXL = Nothing

'Message when finished
Set WshShell = CreateObject("WScript.Shell")
Finished = Msgbox ("Script executed successfully, results can be found in " & Chr(10) _
& outputfile & "." & Chr(10) & Chr(10) _
& "Do you want to view the results now?", 65, "Script executed successfully!")
if Finished = 1 then WshShell.Run "excel " & outputfile

Disclaimer: We hope that the information in these pages is valuable to you. Your use of the information contained in these pages, however, is at your sole risk. All information on these pages is provided “as -is”, without any warranty, whether express or implied, of its accuracy, completeness, fitness for a particular purpose, title or non-infringement, and none of the third-party products or information mentioned in the work are authored, recommended, supported or guaranteed by me. I shall not be liable for any damages you may sustain by using this information, whether direct, indirect, special, incidental or consequential, even if it has been advised of the possibility of such damages.

Similar articles

Latest Articles

3 AI Implementations That...

I was on a joint educational call for the World Talent Economic Economic forum on mobile computing this week. We drifted to topics that...

Survey of Site Reliability...

NEW YORK — Site reliability engineers (SREs) are warning of a looming scalability ceiling and saying the adoption of AIOps isn’t happening at a...

Druva Integrates sfApex to...

SUNNYVALE, Calif. — A maker of software for cloud data protection and management is helping companies safeguard essential customer data that their sales and...

Best Data Science Tools...

Data science has transformed our world. The ability to extract insights from enormous sets of structured and unstructured data has revolutionized numerous fields —...