• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Obtain directory size.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Obtain directory size.


  • Subject: Re: Obtain directory size.
  • From: Daniel Jalkut <email@hidden>
  • Date: Fri, 20 May 2005 09:42:34 -0700


On May 20, 2005, at 9:09 AM, Julian wrote:

Hello,
Since NSFileManager has no capabilities of returning the size of a folder(a folder + contents), what would be a suitable & fast method for obtaining this besides enumerating as it's slow as christmas. I am looking for perhaps a carbon call to get this in one swoop.

I don't think there's anything that does it all for you.  I use Carbon to iterate, which is must faster than using NSEnumerator, but it still may not be the best way.

Note my comment about the resource forks.  The application I'm using this in doesn't grab the resource forks so it doesn't care about their size.

- (unsigned long long) fastFolderSizeAtFSRef:(FSRef*)theFileRef
{
    FSIterator    thisDirEnum = NULL;
    unsigned long long totalSize = 0;
    
    // Iterate the directory contents, recursing as necessary
    if (FSOpenIterator(theFileRef, kFSIterateFlat, &thisDirEnum) == noErr)
    {
        const ItemCount kMaxEntriesPerFetch = 256;
        ItemCount actualFetched;
        FSRef    fetchedRefs[kMaxEntriesPerFetch];    
        FSCatalogInfo fetchedInfos[kMaxEntriesPerFetch];
        
        // DCJ Note right now this is only fetching data fork sizes... if we decide to include
        // resource forks we will have to add kFSCatInfoRsrcSizes

        OSErr fsErr = FSGetCatalogInfoBulk(thisDirEnum, kMaxEntriesPerFetch, &actualFetched,
                                        NULL, kFSCatInfoDataSizes | kFSCatInfoNodeFlags, fetchedInfos,
                                        fetchedRefs, NULL, NULL);
        while ((fsErr == noErr) || (fsErr == errFSNoMoreItems))
        {
            ItemCount thisIndex;
            for (thisIndex = 0; thisIndex < actualFetched; thisIndex++)
            {
                // Recurse if it's a folder
                if (fetchedInfos[thisIndex].nodeFlags & kFSNodeIsDirectoryMask)
                {
                    totalSize += [self fastFolderSizeAtFSRef:&fetchedRefs[thisIndex]];
                }
                else
                {
                    // add the size for this item
                    totalSize += fetchedInfos[thisIndex].dataLogicalSize;
                }
            }
            
            if (fsErr == errFSNoMoreItems)
            {
                break;
            }
            else
            {
                // get more items
                fsErr = FSGetCatalogInfoBulk(thisDirEnum, kMaxEntriesPerFetch, &actualFetched,
                                        NULL, kFSCatInfoDataSizes | kFSCatInfoNodeFlags, fetchedInfos,
                                        fetchedRefs, NULL, NULL);
            }
        }
        FSCloseIterator(thisDirEnum);
    }
    return totalSize;
}


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

  • Follow-Ups:
    • Re: Obtain directory size.
      • From: Daniel Jalkut <email@hidden>
References: 
 >Obtain directory size. (From: Julian <email@hidden>)

  • Prev by Date: Re: Obtain directory size.
  • Next by Date: Re: Getting the NSDateFormatter strings from the user's preferences
  • Previous by thread: Re: Obtain directory size.
  • Next by thread: Re: Obtain directory size.
  • Index(es):
    • Date
    • Thread