开发者

How can i get the count of the files in a folder? [duplicate]

This question already has answers here: Get the count of files i开发者_JAVA百科n a directory (3 answers) Closed 10 years ago.

How can i get the number of files in a folder on iOS?


NSFileManager *filemgr = [NSFileManager defaultManager];

NSArray *filelist= [filemgr directoryContentsAtPath: yourPath];

int count = [filelist count];

NSLog ("%i",count);


directoryContentsAtPath: is deprecated.

If you want get files count of Documents folder.

try to this.

NSFileManager *fm = [NSFileManager defaultManager];
NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSArray *filelist= [fm contentsOfDirectoryAtPath:[docPaths objectAtIndex:0] error:nil];
int filesCount = [filelist count];
NSLog(@"filesCount:%d", filesCount);


- (NSArray *)directoryContentsAtPath:(NSString *)path is deprecated. Use - (NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error instead. This would mean that Matt S's code from above would become:

NSFileManager * filemgr = [NSFileManager defaultManager];
NSArray * filelist = [filemgr directoryContentsAtPath:yourPath error:nil];
int count = [filelist count];
NSLog("%d", count);


NSArray* files = [[NSFileManager defaultManager] directoryContentsAtPath:DIRECTORY];
NSLog(@"%d",[files count]);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜