Question

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

Was it helpful?

Solution

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)".

OTHER TIPS

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.

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top