AutoHotkey: Check Windows Folder Sizes

What?
So I needed to clear space on a workstation's C drive. There are other programs about and even some built-in to MS Windows that could potentially be used.

This is a quick article on how to write an AutoHotkey program to simply return the folders in the drive and display the size of all the files/folders contained within.

How?
So I'll be putting this program for download from the download section of this website but I don't expect anyone to trust an executable EXE so here's the code of what it's doing which you can copy and paste to an AutoHotkey script:
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
#SingleInstance force

; --------------------------------------------------------------------------------------
; Set program Defaults
; --------------------------------------------------------------------------------------

Title   := "Joes Folder Sizes"
Desc    := "This program will display folders and their respective sizes."
Width   := 500
Height  := 360
BgColor := "FFFFFF"
MarginX := 2
MarginY := 2
Options := "+Caption +Border +ToolWindow -Resize -MaximizeBox -MinimizeBox +OwnDialogs"
BaseDir := "C:\Windows\Temp"

; --------------------------------------------------------------------------------------
; Create Icons

ImageListID := IL_Create(5)
Loop 5
    IL_Add(ImageListID, "shell32.dll", A_Index)

; --------------------------------------------------------------------------------------
; Create GUI

Gui, New, %Options%, %Title%
Gui, Color, %BgColor%
Gui, Margin, %MarginX%, %MarginY%
Gui, Add, Edit, x2 y2 w406 vMyBaseDir +Disabled, %BaseDir%
Gui, Add, Button, x408 y1 w90 h23 vMyButtonBrowse gButtonBrowse, Browse...
Gui, Add, TreeView, x2 y25 w496 h310 vMyFolderTree ImageList%ImageListID%
Gui, Add, StatusBar, v_StatusBar
Gui, Show, w%Width% h%Height% xCenter yCenter
SB_SetText("Ready.")
Return

; --------------------------------------------------------------------------------------
; Subroutine: LoopThrough: loops through folders and counts file sizes (including subfolders)

LoopThrough:
SB_SetText("Reading current directory...")
vTotalSize = 0
Loop, Files, %MyBaseDir%\*, F
{
    vTotalSize := vTotalSize + A_LoopFileSize
}
vListingEntry := "<current_dir> (" autoByteFormat(vTotalSize) ")"
P0:=TV_Add(vListingEntry, 0, "Icon1")
Loop, Files, %MyBaseDir%\*, D
{
    vThisFolder := MyBaseDir "\" A_LoopFileName
    SB_SetText("Reading " vThisFolder "...")
    vListingEntry := A_LoopFileName " (" autoByteFormat(GetFileFolderSize(vThisFolder)) ")"
    P1:=TV_Add(vListingEntry, 0, "Icon4")
}
SB_SetText("Ready.")
Return

; --------------------------------------------------------------------------------------
; Subroutine: ButtonBrowse: Onclick allows user to select a folder to scan

ButtonBrowse:
    FileSelectFolder, vSelectedFolder, 3, , Select a folder
    if vSelectedFolder =
        MsgBox, You didn't select a folder.
    else
    {
        vSelectedFolder := RegExReplace(vSelectedFolder, "\\$")  ; Removes the trailing backslash, if present.
        GuiControl, , MyBaseDir, %vSelectedFolder%
    }
    Gui, Submit, NoHide
    GoSub, ClearTreeView
    GoSub, LoopThrough
Return

; --------------------------------------------------------------------------------------
; Subroutine: ClearTreeView: Clears the current TreeView

ClearTreeView:
    TV_Delete()
Return

; --------------------------------------------------------------------------------------
; https://autohotkey.com/board/topic/39659-func-autobyteformat-convert-bytes-to-byteskbmbgbtb/
; converts Size (in bytes) to byte(s)/KB/MB/GB/TB (uses best option)
; decimalPlaces is the number of decimal places to round
; --------------------------------------------------------------------------------------
autoByteFormat(size, decimalPlaces = 2)
{
    static size1 = "KB", size2 = "MB", size3 = "GB", size4 = "TB"

    sizeIndex := 0

    while (size >= 1024)
    {
        sizeIndex++
        size /= 1024.0

        if (sizeIndex = 4)
            break
    }

    return (sizeIndex = 0) ? size " byte" . (size != 1 ? "s" : "")
        : round(size, decimalPlaces) . " " . size%sizeIndex%
}

; --------------------------------------------------------------------------------------
; https://autohotkey.com/board/topic/67917-get-folder-size/
; function to loop through files/subfolders and total the file sizes

GetFileFolderSize(fPath="")
{
    FolderSize := 0
	if InStr(FileExist(fPath), "D")
	{
		Loop, %fPath%\*.*, 1, 1
			FolderSize += %A_LoopFileSize%
		Size := % FolderSize ? FolderSize : 0
		Return, Size
	}
	else if (FileExist(fPath) <> "")
	{
		FileGetSize, FileSize, %fPath%
		Size := % FileSize ? FileSize : 0
		Return, Size
	}
	else
		Return, -1
}


; --------------------------------------------------------------------------------------
; Closing subroutine

GuiClose:
CloseMe:
ExitApp

Related Articles

Joes Revolver Map

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin - Valid till 8 May 2022 3QnhmaBX7LQSRsC9hh6Je9rGQKEGNQNfPb
© 2021 Joel Lipman .com. All Rights Reserved.