Get Folder Size

This process prompts the user to select a folder and calculates the total size of all the files contained in that folder and its subfolders.

function button_pressed
    #Checks if the user pressed the OK button in the previous dialog. If not (i.e if the user pressed the "Cancel" button), the main body of the process will not be executed.

    if ButtonPressed='Cancel' then
        exit 
    end
end


function get_folder_size 
    #The next action initializes a variable ("FolderSize") that will be used to store the total folder's size.

    set FolderSize to 0
    Folder.GetFiles Folder:SelectedFolder  \
                    FileFilter:'*' \
                    IncludeSubfolders:True \
                    FailOnAccessDenied:True \
                    SortBy1:Folder.SortBy.NoSort \
                    SortDescending1:False \
                    SortBy2:Folder.SortBy.NoSort \
                    SortDescending2:False \
                    SortBy3:Folder.SortBy.NoSort \
                    SortDescending3:False \
                    Files=> g:AllFilesInFolder
  
    #The next action loops though all the items of the "AllFilesInFolder" variable (which contains a list of files).

    #In each iteration, the current item (file) of the list will be stored into the variable "item"

    loop foreach item in AllFilesInFolder
        #Increases the "FolderSize" variable's value by the current file's size. When this loop is completed, every file's size in the folder will be added to the "FolderSize" variable (which was originally equal to 0).

        set FolderSize to FolderSize + item.Size
    end

    #The next action divides the folder size by 1048576 to calculate the size in MB

    set FolderSizeInMB to (FolderSize/1048576)
    #The next two actions convert the "FileSize" and "FileSizeInMB" variables (which contain numbers) to text. This is done for formating reasons only (to insert 1000 separator and control how many decimal digits will be displayed).

    Text.NumberToText Number: FolderSize  \
                    DecimalPlaces:0 \
                    UseThousandsSeparator:True \
                    FormattedNumber=> g:FormattedFolderSize

    Text.NumberToText Number: FolderSizeInMB  \
                    DecimalPlaces:3 \
                    UseThousandsSeparator:True \
                    FormattedNumber=> g:FormattedFolderSizeInMB
end

Display.SelectFolder Description:'' \
                    InitialDirectory:'' \
                    IsTopMost:False \
                    SelectedFolder=> SelectedFolder \
                    ButtonPressed=> ButtonPressed

call button_pressed

call get_folder_size

Display.ShowMessage Title:'Folder Size Results' \
                    Message:'The folder:'+SelectedFolder+' and its subfolders contain '+AllFilesInFolder.Count+' files with a total size of: '+FormattedFolderSize+' bytes or: '+FormattedFolderSizeInMB+' MB' \
                    Icon:Display.Icon.None \
                    Buttons:Display.Buttons.OK \
                    DefaultButton:Display.DefaultButton.Button1 \
                    IsTopMost:True \
                    ButtonPressed=> ButtonPressed