Option Explicit
'Shows size of files in subfolders
'Written by Eric Phelps

Main

Sub Main
Dim fs 'As Scripting.FileSystemObject
Dim strFolderSize 'As String
Const ForWriting = 2
Const TemporaryFolder = 2
Dim filTemp 'As Sripting.File
Dim ts 'As Sripting.TextStream
	'On Error Resume Next
	Set fs = CreateObject("Scripting.FileSystemObject")
	'Check to see we got a folder to start with
	If Wscript.Arguments.Count = 0 Then
		MsgBox "Drop a directory on this script"
		Wscript.Quit
	End If
	If Not fs.FolderExists(Wscript.Arguments(0)) Then
		MsgBox "Drop a DIRECTORY on this script"
		Wscript.Quit
	End If
	strFolderSize = SubFolderSizes(fs.GetFolder(Wscript.Arguments(0)))
	If Len(strFolderSize) < 950 Then
		MsgBox strFolderSize,,"Folder Size"
	Else 
		Set ts = fs.GetSpecialFolder(TemporaryFolder).CreateTextFile("FolderSize.txt", True, False)
		ts.Write strFolderSize
		ts.Close
		CreateObject("Wscript.Shell").Run "Notepad.exe """ & fs.GetSpecialFolder(TemporaryFolder).Path & "\FolderSize.txt""", 1
	End If
End Sub


Function SubFolderSizes(objFolder) 'As String
Dim fil 'As Scripting.File
Dim fils 'As Scripting.Files
Dim fol 'As Scripting.Folder
Dim fols 'As Scripting.Folders
Dim strFolderSize 'As String
Dim lngFolderSize 'As Long
Dim lngTotalSize 'As Long
	'On Error Resume Next
	'Get file sizes for the dropped folder
	Set fils = objFolder.Files
	For Each fil In fils
		lngFolderSize = lngFolderSize + fil.Size
	Next
	lngTotalSize = lngFolderSize
	strFolderSize = Left(FormatNumber(lngFolderSize,0) & "			  ",14) & vbTab & objFolder.Path & vbCrLf	
	'Check for any sub folders and get sizes on them
	Set fols = objFolder.SubFolders
	For each fol in fols
		lngFolderSize = FolderSize(fol)
		lngTotalSize = lngTotalSize + lngFolderSize
		strFolderSize = strFolderSize & Left(FormatNumber(lngFolderSize,0) & "			  ",14) & vbTab & fol.Path & vbCrLf
	Next
	strFolderSize = strFolderSize & vbCrLf & Left(FormatNumber(lngTotalSize,0) & "			  ",14) & vbTab & "Total Size"
	SubFolderSizes =  strFolderSize
End Function

Function FolderSize(objFolder)'As Long
Dim lngFolderSize
Dim fils, fil, fols, fol
	'On Error Resume Next
	'Get each file in turn
	Set fils = objFolder.Files
	If Err.Number <> 0 Then Exit Function
	For Each fil In fils
		lngFolderSize = lngFolderSize + fil.Size
	Next
	'Check for any sub folders and recursively process them
	Set fols = objFolder.SubFolders
	For each fol in fols
		If Lcase(fol.Name) <> "recycled" Then
			lngFolderSize = lngFolderSize + FolderSize(fol)
		End If
	Next
	FolderSize = lngFolderSize
End Function

