I have a method which gives me the fileSize of a directory. I looked up in the Documentation but it's not declared which entity NSFileSize has.
Asked
Active
Viewed 3,555 times
3 Answers
5
NSFileSize
The key in a file attribute dictionary whose value indicates the file's size in bytes. The corresponding value is an NSNumber object containing an unsigned long long. Important If the file has a resource fork, the returned value does not include the size of the resource fork. Available in iOS 2.0 and later. Declared in NSFileManager.h.
-
If it output my FileSize in the console it has a value of 25228 (bytes?) if I calculate the size in megabytes it outputs 25 228 bytes = 0.0240592957 megabytes but the directory in the Finder is 10MB large. – btype May 31 '12 at 08:26
-
@btype As I said in my answer: There's no `NSFileSize` for directories. You have to explain what you do and post code that fails. Please edit your question to clarify. – Nikolai Ruhe May 31 '12 at 09:18
-
well, i suppose that you get just the size of the folder (i mean the file used to store informations of the folder), and not the contents files size... – meronix May 31 '12 at 09:19
3
NSFileSize
is a key in attribute dictionaries as returned by NSFileManager
's attributesOfItemAtPath:error:
. The object is an NSNumber from which you can get the POD value using unsignedLongLongValue
.
You don't get NSFileSize
for dictionaries. What method do you mean?

Nikolai Ruhe
- 81,520
- 17
- 180
- 200
0
I solved it myself, thanks to the hint that a directory has no FileSize. Here is my completed Code:
-(float)getSizeOfDirectory:(NSString *)directory{
NSFileManager *filemgr;
NSArray *filelist;
int count;
float cacheSize = 0;
filemgr =[NSFileManager defaultManager];
filelist = [filemgr contentsOfDirectoryAtPath:directory error:NULL];
count = [filelist count];
for (NSString *url in filelist) {
NSData *data = [filemgr contentsAtPath:[NSString stringWithFormat:@"%@/%@",directory,url]];
cacheSize = cacheSize + ([data length]/1000);
}
cacheSize = (cacheSize/1024);
NSLog(@"cacheSize: %f MB",cacheSize);
return cacheSize;
}

btype
- 1,593
- 19
- 32
-
This code is not only utterly wasteful but also incorrect. [In this answer](http://stackoverflow.com/a/28660040/104790) I'm explaining why and how it could be done faster and more precisely. – Nikolai Ruhe Sep 25 '15 at 12:11