How to get folder size of each sub folders?


Let’s just say you want to get folder size of each home folder of the users. Or trying to see which sub folders are growing fast. I use the following three ways depending on my requirement.

1. Fantastic tool named Disk Usage v1.33 by Mark Russinovich: http://technet.microsoft.com/en-us/sysinternals/bb896651.aspx

2. VBScript of my own. Copy the following script into Notepad and save as foldersize.vbs. Run the script as below:

CScript folder.vbs <folder path>

'********************************************************************************
' Script Name: foldersize.vbs
' Purpose: Display and generate a tab delimited file of subfolder names and it's
' size
' Parameter: <directory name with full path> if the path contains spaces, double
' quote the path name
'
' e.g., CScript foldersize.vbs \servernamesharename
'
' Written by: Anand Venkatachalapathy
' Created on : Jan 18 2010
'******************************************************************************** 

' Defining some variable
Dim fso
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim sLoc, LogFileName
Dim fLog, SourceLoc, sf 

' get the file system object
Set fso = CreateObject("Scripting.FileSystemObject") 

' Get the arguments of folder path
sLoc =  WScript.Arguments(0) 

' Define the log file name
LogFileName = Replace(sLoc,"","-") & ".txt" 

'Create the Log file
Set fLog = fso.OpenTextFile(LogFileName, ForWriting, True) 

'Get the sub folder list from the given path
Set SourceLoc = fso.GetFolder(sLoc)
Set sf = SourceLoc.SubFolders 

'Header for the log file
fLog.WriteLine "Folder Path" & chr(9) & "Size in MBs" 

'for each sub directory get the size
For Each SubDirs in sf
    FindFolder SubDirs.Path 
Next 

'close the log file
fLog.Close 

'**** End of Script **** 

'***********************************************************************************
' Subroutine: FindFolder (<sub-folder name with path>
' Purpose: Get the size of the given sub folder
'***********************************************************************************
Sub FindFolder (FolderSpec)
    'Get the specified folder object
    Set f1 = fso.GetFolder(FolderSpec)
    'Error processing - set if error occurs resume the script    
    On Error Resume Next 

    'Get the folder size
    fsize = f1.Size 

    'if the folder structure or any files are corrupted under this sub folder
    'getting the size will return an error
    '
    'check if we got an error,
    If Err.Number <> 0 Then
        'Yup, got an error
        Wscript.Echo f1.path & " is corrupted. Script cannot process this location."
        fLog.WriteLine f1.Path & chr(9) & "Permission Denied or Corruped files found"
        Err.Clear
    Else
        'display and log the folder name and size            
        WScript.Echo f1.Path & chr(9) & _
           FormatNumber(((fSize/1024)/1024),2)
        fLog.WriteLine f1.Path & chr(9) & _
           FormatNumber(((fSize/1024)/1024),2)
    End If
    On Error Goto 0
    Set f1 = Nothing
End Sub

3. Powershell Script. Copy and paste the following script into notepad and save it as foldersize.ps1. Open up the powershell and go the location where you want list the subfolders and its size and run this script.

# Script: foldersize.ps1
# Usage: Change the working directory where you want to list get size of the folders
# Run the script as c:<dir where this script exists>foldersize.ps1 
# If you want to create a file with the results of this code, run this script as
# C:<dir where this script exists>foldersize.ps1 > homefolders.txt
#
# Written by Anand Venkatachalapathy
# Date Written: Jan 18 2010 

#Get the directory listing and pass the resutls to ForEach 
get-childitem | where {$_.PSIsContainer} | foreach { 

#For every subfolder get the size 
$size = (Get-ChildItem $_ -recurse | where {!$_.PSIsContainer} | Measure-Object -Sum Length).Sum 

#add the sub folder path and size into table
$obj = new-object psobject
add-member -inp $obj noteproperty path $_.fullName
add-member -inp $obj noteproperty size $size 

#display the table
$obj 

}
#End of Script

Search tags: FolderSize, directory size, vbscript, powershell

Advertisements

5 thoughts on “How to get folder size of each sub folders?

  1. I have used and modified this script a bit to suit my needs, but one thing I’m not sure how to make it do is drill down to all the sub folder levels, not just one. here’s what I’m seeing, but it’s only the 1st level of subfolders, otherwise it’s exactly what I want.
    D:\scripts>cscript foldersize.vbs v:\
    Microsoft (R) Windows Script Host Version 5.6
    Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

    V:\EV 75 Standalone VS 0.30 9/22/2011 4:57:36 PM
    V:\EV_75_2012 0.06 11/2/2011 3:35:40 PM
    V:\EV8 BAT Test 13.28 2/6/2012 10:50:48 AM
    V:\EV2007 New 0.27 2/3/2012 4:02:28 PM
    V:\EVPATCP03 VS1 PTN1 25.55 6/13/2012 9:32:15 AM

    This is my script (your script modified for my use) – do you think you can point me in the right direction to make it drill down all levels? Thanks! Steve.

    ‘********************************************************************************
    ‘ Script Name: foldersize.vbs
    ‘ Purpose: Display and generate a tab delimited file of subfolder names and it’s
    ‘ size
    ‘ Parameter: if the path contains spaces, double
    ‘ quote the path name

    ‘ e.g., CScript foldersize.vbs \servernamesharename

    ‘ Written by: Anand Venkatachalapathy
    ‘ Created on : Jan 18 2010
    ‘********************************************************************************

    ‘ Defining some variable
    Dim fso
    Const ForReading = 1, ForWriting = 2, ForAppending = 8
    Dim sLoc, LogFileName
    Dim fLog, SourceLoc, sf

    ‘ get the file system object
    Set fso = CreateObject(“Scripting.FileSystemObject”)

    ‘ Get the arguments of folder path
    sLoc = WScript.Arguments(0)

    ‘ Define the log file name
    LogFileName = Replace(sLoc,””,”-“) & “.txt”
    LogFileName = “foldersize.txt”

    ‘Create the Log file
    Set fLog = fso.OpenTextFile(LogFileName, ForWriting, True)

    ‘Get the sub folder list from the given path
    Set SourceLoc = fso.GetFolder(sLoc)
    Set sf = SourceLoc.SubFolders

    ‘Header for the log file
    fLog.WriteLine “Folder Path” & chr(9) & “Size in MBs”

    ‘for each sub directory get the size
    For Each SubDirs in sf
    FindFolder SubDirs.Path
    Next

    ‘close the log file
    fLog.Close

    ‘**** End of Script ****

    ‘***********************************************************************************
    ‘ Subroutine: FindFolder (
    ‘ Purpose: Get the size of the given sub folder
    ‘***********************************************************************************
    Sub FindFolder (FolderSpec)
    ‘Get the specified folder object
    Set f1 = fso.GetFolder(FolderSpec)
    ‘Error processing – set if error occurs resume the script
    On Error Resume Next

    ‘Get the folder size
    fsize = f1.Size

    ‘Get Folder Modified Date
    fdate = f1.DateLastModified

    ‘if the folder structure or any files are corrupted under this sub folder
    ‘getting the size will return an error

    ‘check if we got an error,
    If Err.Number 0 Then
    ‘Yup, got an error
    Wscript.Echo f1.path & ” is corrupted. Script cannot process this location.”
    fLog.WriteLine f1.Path & chr(9) & “Permission Denied or Corruped files found”
    Err.Clear
    Else
    ‘display and log the folder name and size
    WScript.Echo f1.Path & chr(9) & _
    FormatNumber(((fSize/1024)/1024),2) & chr(9) & _
    fdate
    fLog.WriteLine f1.Path & chr(9) & _
    FormatNumber(((fSize/1024)/1024),2) & chr(9) & _
    fdate
    End If
    On Error Goto 0
    Set f1 = Nothing
    End Sub

  2. Mr. Martin,

    Add the following right ater the End If and right before the On Error Goto 0 in your FindFolder Sub…

    …………………….
    End If

    ‘ Process all subfolders in current folder recursively
    For Each subFolder In f1.SubFolders
    FindFolder subFolder.Path
    Next

    On Error Goto 0
    Set f1 = Nothing
    End Sub

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s