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 \\servername\sharename
'
' 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
No comments:
Post a Comment