The file size in the COCOA API is quite simple to find out, for example:

NSNumber *value = nil; [url getResourceValue:&value forKey:NSURLFileSizeKey error:nil]; NSUInteger size = [value unsignedLongLongValue]; NSLog(@"File size: %li", size); 

But how can you find out the size of a directory in about the same simple way?

Now to get the size of the directory I use the recursive search method, like this:

 off_t sizeForFolderStat(const char *path) { off_t size = 0; DIR *directory; directory = opendir(path); if (directory == NULL) { fprintf(stderr, "could not open directory '%s'\n", path); fprintf(stderr, "error is %d/%s\n", errno, strerror(errno)); exit (EXIT_FAILURE); } struct dirent *entry; while ( (entry = readdir(directory)) != NULL) { char filename[MAXPATHLEN]; int result; struct stat statbuf; if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { continue; } snprintf(filename, MAXPATHLEN, "%s/%s", path, entry->d_name); result = lstat(filename, &statbuf); if (result != 0) { fprintf (stderr, "could not stat '%s': %d/%s\n", entry->d_name, errno, strerror(errno)); continue; } if (S_ISDIR(statbuf.st_mode)) { size += sizeForFolderStat(filename); } else { size += statbuf.st_size; } } closedir(directory); return size; } 

This feature works quite quickly, but when a directory contains a lot of attachments it may well load the processor.

Looking for easy ways to get the size of a directory, I found that using the NSMetadataItem class NSMetadataItem can very easily and quickly get the size of an application:

 NSUInteger size = 0; NSNumber *isApp; [url getResourceValue:&isApp forKey:NSURLIsApplicationKey error:nil]; if ([isApp boolValue]) { NSMetadataItem *metadata = [[NSMetadataItem alloc] initWithURL:url]; size = [[metadata valueForAttribute:@"kMDItemFSSize"] unsignedLongLongValue]; } 

Essentially .app is also a directory, but for normal directories (folders) or packages, this method returns only the size of the folder (approximately 4Kb), and not the full size of the directory including the size of its entire contents. I suspect that for packages or directories (folders) there are also similar methods - to quickly and easily get the size of a directory including the size of its entire contents.

If someone knows how to do this, please share your experience. Or tell me how you get the size of directories?

Thank you all in advance.

  • why do you need it? - Max Mikheyenko
  • @Max Mikheyenko why reinvent the wheel. In OS there should be means that allow you to get the size of the directory without overhead. - Toly
  • I mean, why do you need to know the size of the directory, what problem you are solving - Max Mikheyenko
  • @Max Mikheyenko in my case is additional information that the user can select from the list. - Toly
  • one
    @Toly, apparently, there is no easier way than brute force. All the solutions that I have seen, offer to solve this problem by recursive brute force. By the way, this is true not only for OSX, but also for Windows. There the framework also does not have for this method out of the box. - Olter

0