How would I calculate the size of a folder (including all contents)?


#1

I tried using Get File Attribute but it seems to get the size of the individual folder and not all of the contents.


SCRIPT: Get Size of Entire Folder
#2

Put this script in an Execute AppleScript Action. It will return size of all items (files, folders, sub-folders) in the folder currently selected in the Finder.

use AppleScript version "2.5" -- El Capitan (10.11) or later
use framework "Foundation" -- this IS required
use scripting additions

## Some Scripts may work with Yosemite, but no guarantees ##
#  This script has been tested ONLY in macOS 10.11.6+

tell application "Finder" to set finderSelectionList to selection as alias list
set folderAlias to item 1 of finderSelectionList

--- Returns Entire Folder Size in Bytes ---
tell application "System Events" to set folderSize to size of folderAlias

--- Convert Bytes to Size-Appropriate Units as a String ---
set folderSize to (current application's NSByteCountFormatter's stringFromByteCount:folderSize countStyle:(current application's NSByteCountFormatterCountStyleDecimal)) as text

return folderSize

#3

Thanks!


#4

@8times9,

If one of the above posts solves your problem/question as originally stated, please check the “Solved” checkbox (click for details) at the bottom of that post.

Otherwise, please post your remaining questions/issues about this problem.
If you have other questions, please start a new topic.


#5

Hmm…looking at this, I’m not sure how I would integrate it into its intended use case in my macro.

I’m automatically importing files on CDs and copying them to Dropbox. Because I’m using a free account, my storage is limited to 2.4 GB, and trying to add any files once the limit is reached results in syncing halting.

My goal is to get the current size of the items in the Dropbox folder, add the value to the size of the file on the CD, and ensure the value is less than 2.4 GB.


#6

The shell command “du” is what you need. This would give you a human-readable size of your desktop:

du -hs ~/Desktop

Leave out the “h” for a size in bytes.


#7

This should get you started. You need to confirm or change the first two actions that are in magenta, and to add the Actions you want to copy/move the file to DropBox.

###MACRO:   Check Folder Size Against Limits

~~~ VER: 1.0    2018-01-01 ~~~

####DOWNLOAD:
Check Folder Size Against Limits.kmmacros (9.9 KB)
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


Questions?


#8

With a few tweaks I integrated it into my current macro, thank you for taking the time to make it.