Get the list of files from selected folders having sizes >100MB with their paths objective c

孤人 提交于 2019-12-31 07:28:07

问题


I want to get the list of all files with their paths & sizes for my mac system.

From that, I want to filter only those files which have file size above 100 MB.

I got the size of my system using the below code.

NSError *error;

NSFileManager *fileManager = [NSFileManager defaultManager];
NSDictionary *fileAttributes = [fileManager attributesOfFileSystemForPath:@"/" error:&error];

Now I want to get the list of files with their paths and sizes.

I searched a lot but those couldn't meet to my requirements.

Pleas help me on this.

Thanks in advance...


回答1:


I got my solution with the following scenario.

Thanks CRD for giving me the idea.

First, I took a path of a folder. Then for getting subfolders only, I used NSDirectoryEnumerator and then I used the code to find the size of file.

Once I got the size of the file, then I checked the file size and added to my array.

So it worked.

NSString *Path = [NSString stringWithFormat:@"/Users/%@/",NSUserName()];

NSDirectoryEnumerator *de = [[NSFileManager defaultManager] enumeratorAtPath:Path];

NSString *file;
NSMutableArray *arrList = [[NSMutableArray alloc]init];

 while ((file = [de nextObject]))
 NSLog(@"file %@",file);

 Path = [NSString stringWithFormat:@"/Users/%@/",NSUserName()];

 Path = [Path stringByAppendingString:file];
 NSLog(@"path %@",Path);
NSError *attributesError = nil;

 NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:Path error:&attributesError];

 NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
 long long fileSize = [fileSizeNumber longLongValue];
 NSLog(@"%lld",fileSize);

 float sizeKB = ((float)fileSize / 1000);
 NSLog(@"%f",sizeKB);

 float sizeMB = ((float)fileSize / 1000000);
 NSLog(@"%f",sizeMB);

 if (sizeMB >= 100.0)
 {
     [arrList addObject:file];
     [test addObject:file.lastPathComponent];
 }

Hope it helps someone else also.



来源:https://stackoverflow.com/questions/31308695/get-the-list-of-files-from-selected-folders-having-sizes-100mb-with-their-paths

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!