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:
copyraw
#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
  1.  #NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases. 
  2.  #Warn  ; Enable warnings to assist with detecting common errors. 
  3.  SendMode Input  ; Recommended for new scripts due to its superior speed and reliability. 
  4.  SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory. 
  5.  #SingleInstance force 
  6.   
  7.  ; -------------------------------------------------------------------------------------- 
  8.  ; Set program Defaults 
  9.  ; -------------------------------------------------------------------------------------- 
  10.   
  11.  Title   :"Joes Folder Sizes" 
  12.  Desc    :"This program will display folders and their respective sizes." 
  13.  Width   :500 
  14.  Height  :360 
  15.  BgColor :"FFFFFF" 
  16.  MarginX :2 
  17.  MarginY :2 
  18.  Options :"+Caption +Border +ToolWindow -Resize -MaximizeBox -MinimizeBox +OwnDialogs" 
  19.  BaseDir :"C:\Windows\Temp" 
  20.   
  21.  ; -------------------------------------------------------------------------------------- 
  22.  ; Create Icons 
  23.   
  24.  ImageListID :IL_Create(5) 
  25.  Loop 5 
  26.      IL_Add(ImageListID, "shell32.dll", A_Index) 
  27.   
  28.  ; -------------------------------------------------------------------------------------- 
  29.  ; Create GUI 
  30.   
  31.  Gui, New, %Options%, %Title% 
  32.  Gui, Color, %BgColor% 
  33.  Gui, Margin, %MarginX%, %MarginY% 
  34.  Gui, Add, Edit, x2 y2 w406 vMyBaseDir +Disabled, %BaseDir% 
  35.  Gui, Add, Button, x408 y1 w90 h23 vMyButtonBrowse gButtonBrowse, Browse... 
  36.  Gui, Add, TreeView, x2 y25 w496 h310 vMyFolderTree ImageList%ImageListID% 
  37.  Gui, Add, StatusBar, v_StatusBar 
  38.  Gui, Show, w%Width% h%Height% xCenter yCenter 
  39.  SB_SetText("Ready.") 
  40.  Return 
  41.   
  42.  ; -------------------------------------------------------------------------------------- 
  43.  ; Subroutine: LoopThrough: loops through folders and counts file sizes (including subfolders) 
  44.   
  45.  LoopThrough: 
  46.  SB_SetText("Reading current directory...") 
  47.  vTotalSize = 0 
  48.  Loop, Files, %MyBaseDir%\*, F 
  49.  { 
  50.      vTotalSize := vTotalSize + A_LoopFileSize 
  51.  } 
  52.  vListingEntry :"<current_dir> (" autoByteFormat(vTotalSize) ")" 
  53.  P0:=TV_Add(vListingEntry, 0, "Icon1") 
  54.  Loop, Files, %MyBaseDir%\*, D 
  55.  { 
  56.      vThisFolder := MyBaseDir "\" A_LoopFileName 
  57.      SB_SetText("Reading " vThisFolder "...") 
  58.      vListingEntry := A_LoopFileName (" autoByteFormat(GetFileFolderSize(vThisFolder)) ")" 
  59.      P1:=TV_Add(vListingEntry, 0, "Icon4") 
  60.  } 
  61.  SB_SetText("Ready.") 
  62.  Return 
  63.   
  64.  ; -------------------------------------------------------------------------------------- 
  65.  ; Subroutine: ButtonBrowse: Onclick allows user to select a folder to scan 
  66.   
  67.  ButtonBrowse: 
  68.      FileSelectFolder, vSelectedFolder, 3, , Select a folder 
  69.      if vSelectedFolder = 
  70.          MsgBox, You didn't select a folder. 
  71.      else 
  72.      { 
  73.          vSelectedFolder :RegExReplace(vSelectedFolder, "\\$")  ; Removes the trailing backslash, if present. 
  74.          GuiControl, , MyBaseDir, %vSelectedFolder% 
  75.      } 
  76.      Gui, Submit, NoHide 
  77.      GoSub, ClearTreeView 
  78.      GoSub, LoopThrough 
  79.  Return 
  80.   
  81.  ; -------------------------------------------------------------------------------------- 
  82.  ; Subroutine: ClearTreeView: Clears the current TreeView 
  83.   
  84.  ClearTreeView: 
  85.      TV_Delete() 
  86.  Return 
  87.   
  88.  ; -------------------------------------------------------------------------------------- 
  89.  ; https://autohotkey.com/board/topic/39659-func-autobyteformat-convert-bytes-to-byteskbmbgbtb/ 
  90.  ; converts Size (in bytes) to byte(s)/KB/MB/GB/TB (uses best option) 
  91.  ; decimalPlaces is the number of decimal places to round 
  92.  ; -------------------------------------------------------------------------------------- 
  93.  autoByteFormat(size, decimalPlaces = 2) 
  94.  { 
  95.      static size1 = "KB", size2 = "MB", size3 = "GB", size4 = "TB" 
  96.   
  97.      sizeIndex :0 
  98.   
  99.      while (size >= 1024) 
  100.      { 
  101.          sizeIndex++ 
  102.          size /= 1024.0 
  103.   
  104.          if (sizeIndex = 4) 
  105.              break 
  106.      } 
  107.   
  108.      return (sizeIndex = 0) ? size " byte" . (size != 1 ? "s" : "") 
  109.          : round(size, decimalPlaces) . " " . size%sizeIndex% 
  110.  } 
  111.   
  112.  ; -------------------------------------------------------------------------------------- 
  113.  ; https://autohotkey.com/board/topic/67917-get-folder-size/ 
  114.  ; function to loop through files/subfolders and total the file sizes 
  115.   
  116.  GetFileFolderSize(fPath="") 
  117.  { 
  118.      FolderSize :0 
  119.      if InStr(FileExist(fPath), "D") 
  120.      { 
  121.          Loop, %fPath%\*.*, 1, 1 
  122.              FolderSize += %A_LoopFileSize% 
  123.          Size := % FolderSize ? FolderSize : 0 
  124.          Return, Size 
  125.      } 
  126.      else if (FileExist(fPath) <> "") 
  127.      { 
  128.          FileGetSize, FileSize, %fPath% 
  129.          Size := % FileSize ? FileSize : 0 
  130.          Return, Size 
  131.      } 
  132.      else 
  133.          Return, -1 
  134.  } 
  135.   
  136.   
  137.  ; -------------------------------------------------------------------------------------- 
  138.  ; Closing subroutine 
  139.   
  140.  GuiClose: 
  141.  CloseMe: 
  142.  ExitApp 
Category: AutoHotkey :: Article: 663

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: The information on this website is provided without warranty and any content is merely the opinion of the author. Please try to test in development environments prior to adapting them to your production environments. The articles are written in good faith and, at the time of print, are working examples used in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com

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.