Getting File Size--Finder or Command Line?

I’ve had issues getting the size of folders or mounted volumes if they take too long to return the information. What is the most reliable way to get the size of a folder on a remote volume, or the volume itself?

Kinda surprised no opinions on this…

Well, I keep running into the problem where the folder size isn’t returned fast enough for AppleScript and had to resort to the below code, which takes forever to run on a batch of 520 folders on our design server (despite simply using “Get Info” manually returning quick answers). What mechanism does Get Info use to get folder size behind-the-scenes?

Despite saying files_to_mark (the code is borrowed from elsewhere) the reference can be a file or folder (hence the “item” reference), and is usually a folder (which means the item/folder size has to be aggregated by the OS, presumeably?).

repeat with currently_coloring in files_to_mark
	tell application "Finder"
		
		--get item size overcome Finder being slow getting size of large folders
		repeat
			try
				set current_size_kb_sci to 0
				set current_size_kb_sci to size of (item currently_coloring) --answer in kb, scientific notation
				if current_size_kb_sci > 0 then exit repeat
			on error
				delay 1
			end try
		end repeat
		
		-- accumulate size
		set total_size_kb_sci to total_size_kb_sci + current_size_kb_sci
		
		--set total_size to total_size + current_size
		set label index of item currently_coloring to "1"
	end tell
end repeat

I use the df -h command.

Not being a shell person, what would that look like…would it be…

do shell df -h item_path

…?

Is there some reason this doesn’t work for you:?

set S to size of (info for (path to documents folder))

Use this suggestion instead of the other one. Also, this is only slightly
faster than the one Adam posted. There is no real fast way to get
the folder size. Even in Obj-C this takes the same about of time.

set the_item to quoted form of POSIX path of (choose folder)
do shell script "du -sh " & space & the_item

Cheers,

Craig

Speed is a problem…AppleScript is very impatient when the Finder takes too long to aggregate the file sizes of everything in a folder to get a folder size, especially, as in my case, on a server.

I can use the above, but have to enclose it in the “try” and loop it until I get a value returned due to the delay in the Finder returning a value. My last attempt to use my script with this sort of “try until you get a value” took more than 90 minutes to run before I lost patience and was wondering if there was a more foolproof/faster way to get the job done.

Y’all are famous for such solutions, doncha know? :wink:

Assuming you don’t have access (other than a share) to the server then there isn’t a faster way that I know of. If you are permitted to ssh to the server (or VNC to it) then you can get the answer that way by running the command on the server and returning the answer…