文件夹大小错误 [英] folder size wrong
本文介绍了文件夹大小错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
此代码正确,但是文件夹大小错误.如果我更改目录,大小总是错误的.例如,%@/Caches/com.apple.Safari/网页预览"的大小为23 MB,但我有16.5 KB.
This code is correct but the folder size is wrong. If I change the directory the size is always wrong. For example the size of "%@/Caches/com.apple.Safari/Webpage Previews" is 23 MB, but I have 16.5 KB.
NSString *path = [NSString stringWithFormat:@"%@/Caches/com.apple.Safari/Webpage Previews", [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0]];
NSNumber *fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil] objectForKey:NSFileSize];
resultsize += [fileSize unsignedLongLongValue];
我也用这个,但是大小总是错误的:
also I used this but the size is always wrong:
NSFileManager *fm = [[NSFileManager alloc] init];
NSURL *LibraryURL = [[fm URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask] lastObject];
NSURL *previewsURL = [LibraryURL URLByAppendingPathComponent:@"Caches/com.apple.Safari/Webpage Previews"];
resultSize += [[[fm attributesOfItemAtPath:[previewsURL path] error:nil] objectForKey:NSFileSize] unsignedIntegerValue];
你能帮我吗? 谢谢.
推荐答案
如果要获取文件夹中所有文件的大小,则需要遍历该文件夹的内容并获取该文件的大小.实际文件:
If you want to get the size of all the files in a folder, you'll need to iterate through the contents of the folder and get the sizes of the actual files:
unsigned long long totalSize = 0;
NSFileManager *fm = [[NSFileManager alloc] init];
NSURL *libraryURL = [[fm URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask] lastObject];
NSURL *previewsURL = [LibraryURL URLByAppendingPathComponent:@"Caches/com.apple.Safari/Webpage Previews"];
NSDirectoryEnumerator *enumerator = [fm enumeratorAtURL:previewsURL includingPropertiesForKeys:[NSArray arrayWithObject:NSURLFileSizeKey] options:0 errorHandler:nil /* or an actual error handler */];
for (NSURL *url in enumerator) {
NSNumber *sizeNumber;
if ([url getResourceValue:&sizeNumber forKey:NSURLFileSizeKey error:nil /* or an error */])
totalSize += [sizeNumber unsignedLongLongValue];
}
(我还没有测试过.)
这篇关于文件夹大小错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文