Get the list of files from selected folders having

2019-03-04 13:10发布

问题:

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.