views:

291

answers:

3

Hi,

I'm using this method by Dave DeLong to calculate the size of a folder using the Carbon File Manager API:

http://github.com/davedelong/BuildCleaner/blob/b2712242b4eea1fff0e78a08b393a417e3019c8a/NSFileManager+FileSize.m

(Its the first method there)

The issue I'm having is that some folders (.app bundles in my case) are not reporting the correct size. For example, DiskWarrior is 8.2MB, and the method reports 6.6MB

Any ideas on why this is happening?

THANKS

+1  A: 

The Finder will report file sizes rounded to the nearest multiple of the block size (usually 4 KB) followed by the real size in bytes, and many (most) applications are bundles of files, so the true size of the application may be far smaller than the size shown as the first ("on disk") value.

You can test this out by doing something (in the Terminal) like:

echo -n 'foo' > foo.txt

If you Get Info on this file in the Finder, it will report the size as "4 KB on disk (3 bytes)".

Wevah
Hmm I tried using the shell command "du -sk <path to folder>" to get the size of the folder, and it still reports it MUCH closer to the actual thing than the Carbon file manager does. And I'm pretty sure that the du utility doesn't round the block size like Finder does.
macatomy
du displays block counts (disk usage). -k makes it report the value in 1 KB blocks instead of whatever the disk's block size is.
Wevah
A: 

If you know how to use applescript's in your code, here's a method o return the size that you'd see in the Finder Get Info window. Note that the returned value is in bytes.

on getSizeInBytesFromPosixPath(posixPath)
    try
     set b to (POSIX file posixPath) as string
     tell application "Finder" to return size of (b as alias)
    on error
     return 0
    end try
end getSizeInBytesFromPosixPath
regulus6633
Thanks, I tried this method and it works--the size is exactly as reported by Finder. However, its very slow compared to other ways of doing it and it crashes under load. I need to process a large number of files and folders at once. Any other ways to get the size as reported by Finder?
macatomy
+1  A: 

I've improved on that source code of mine that you've linked to. Here's the newer version:

http://github.com/davedelong/BetterInfo/blob/aa1cfe079dad6207a7ddac84b108a768c2cc7156/NSFileManager+BetterInfo.m (You'll also need the corresponding .h file and this support file)

Now instead of returning and NSUInteger, it returns a struct of type "BIItemSyze", which has six members: dataLogicalSize, dataPhysicalSize, resourceLogicalSize, resourcePhysicalSize, logicalSize, and physicalSize.

Dave DeLong