Option Explicit
Private fsoSystem As FileSystemObject
'____________________
Sub ListFolderSizes()
'¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
' Based on code posted by JPG here:
' http://www.mrexcel.com/board2/viewtopic.php?t=51719
Dim strStartPath As String, strToAbbr As String
Dim varFileInfo() As Variant
Dim booSubFolders As Boolean, booListFiles As Boolean
Dim rngCurrent As Range, rngCell As Range, rngCol As Range
Dim p%
Set fsoSystem = New FileSystemObject
strStartPath = GetStartPath
booSubFolders = (vbYes = MsgBox("Search subfolders?", vbYesNo + vbQuestion))
booListFiles = (vbYes = MsgBox("List files?", vbYesNo + vbQuestion + vbDefaultButton2))
ReDim varFileInfo(1 To 2, 1 To 1)
varFileInfo(1, 1) = "Folder Name"
varFileInfo(2, 1) = "Folder Size"
GetFolderInfo varFileInfo, strStartPath, booSubFolders, booListFiles
Set fsoSystem = Nothing
Range("A1").Resize(UBound(varFileInfo, 2), 2).Value = Application.Transpose(varFileInfo)
Application.StatusBar = False
If Not booSubFolders Then Exit Sub
Set rngCurrent = [a1].CurrentRegion
rngCurrent.Font.Name = "Courier New"
rngCurrent.Sort Key1:=Range("A1"), Header:=xlYes
rngCurrent.Columns(3).FormulaR1C1 = "=(LEN(RC[-2]) - LEN(SUBSTITUTE(RC[-2],""\"","""")))"
[C1].FormulaR1C1 = "=MAX(R[1]C:R[" & rngCurrent.Rows.Count - 1 & "]C)"
rngCurrent.Columns(2).NumberFormat = _
"[Black][>1000000]#,###.0,,"" MB"";[Blue][>1000]#.0,"" KB"";[Magenta]0 "" B"""
' If [C2] > 1 Then
' Set rngCol = rngCurrent.Columns(1)
' Set rngCol = rngCol.Offset(1).Resize(rngCol.Rows.Count - 1)
' strToAbbr = Mid(strStartPath, 1, InStrRev(Left(strStartPath, Len(strStartPath) - 1), _
' Application.PathSeparator))
' rngCol.Replace what:=strToAbbr, _
' Replacement:=Left(strToAbbr, 2) & "..\", _
' LookAt:=xlPart
' End If
With rngCurrent.Columns(3)
.Formula = .Value
End With
Set rngCol = rngCurrent.Columns(1)
Set rngCol = rngCol.Offset(1).Resize(rngCol.Rows.Count - 1)
'// Anything that's missing a period (no file extension)
'// is assumed to be a folder (not 100% accurate, but close)
If booListFiles Then
With rngCol.FormatConditions
.Delete
.Add Type:=xlExpression, _
Formula1:="=ISERROR(SEARCH(""."",A1))"
With .Item(1).Font
.Bold = True
.Italic = False
.ColorIndex = 5 'blue
End With
End With
End If
' Set rngCol = rngCol.Offset(1).Resize(rngCol.Rows.Count - 1)
' For Each rngCell In rngCol.Cells
' With rngCell
' p = InStrRev(.Value, Application.PathSeparator)
' .Value = Space(p - 1) & Right(.Text, Len(.Text) - p + 1)
' .Offset(, 1).Cut .Offset(, .Offset(, 2))
' If .Offset(, 2).NumberFormat = "General" Then .Offset(, 2).Clear
' End With
' Next rngCell
[C1:C2].Clear
End Sub
'__________________________________________________
Sub GetFolderInfo(ByRef varFileInfo As Variant, _
ByVal strFolder As String, _
ByVal booSubFolders As Boolean, _
ByVal booListFiles As Boolean)
'¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Dim fsoFolder As Scripting.Folder, fsoSubFolder As Scripting.Folder
Dim fsoFile As Scripting.File
Dim lngFolderCount As Long
On Error GoTo err_GetFolderInfo
Set fsoFolder = fsoSystem.GetFolder(strFolder)
lngFolderCount = UBound(varFileInfo, 2) + 1
ReDim Preserve varFileInfo(1 To 2, 1 To lngFolderCount)
varFileInfo(1, lngFolderCount) = fsoFolder.path
varFileInfo(2, lngFolderCount) = fsoFolder.Size
On Error GoTo 0
If booListFiles Then
For Each fsoFile In fsoFolder.Files
lngFolderCount = UBound(varFileInfo, 2) + 1
Application.StatusBar = "Items Found: " & lngFolderCount
ReDim Preserve varFileInfo(1 To 2, 1 To lngFolderCount)
varFileInfo(1, lngFolderCount) = fsoFile.path
varFileInfo(2, lngFolderCount) = fsoFile.Size
Next fsoFile
End If
If booSubFolders Then
On Error GoTo err_GetFolderInfo
For Each fsoSubFolder In fsoFolder.SubFolders
If Not fsoSubFolder Is Nothing Then
GetFolderInfo varFileInfo, fsoSubFolder.path, True, booListFiles
End If
Next fsoSubFolder
End If
Set fsoFolder = Nothing
Exit Sub
err_GetFolderInfo:
'¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨¨
Select Case Err.Number
Case 70 ' (permission denied)
varFileInfo(2, lngFolderCount) = 1
Resume Next
Case Else
MsgBox "Error # " & Err.Number & ": " & Err.Description
End Select
End Sub
'________________________________________
Private Function GetStartPath() As String
'¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Dim strDir As String, strPS As String
Dim fdDir As FileDialog
strPS = Application.PathSeparator
Set fdDir = Application.FileDialog(msoFileDialogFolderPicker)
[a1].Select
If ActiveCell = "" Then
fdDir.InitialFileName = Application.DefaultFilePath & strPS
Else
fdDir.InitialFileName = [a1]
End If
With fdDir
If .Show = 0 Then Exit Function
strDir = .SelectedItems(1)
End With
GetStartPath = strDir & IIf(Right(strDir, 1) <> strPS, strPS, vbNullString)
End Function