Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count the number of files inside a folder in Objective C (Cocoa)

I m making an array of images animate like a flicker book animation,i m storing these images inside a folder which intern resides inside Resource folder of my project in xcode.. these images will vary,that is why i have to determine the exact number of images inside the folder so how should i determine this? is there any API in cocoa to achieve this?

like image 652
Yadnesh Avatar asked Jun 01 '11 10:06

Yadnesh


2 Answers

Try

int paths = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:@"/your/path/here" error:NIL] count];

For more info, http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html

like image 174
Chanok Avatar answered Oct 30 '22 00:10

Chanok


First, you need to access the bundle path of your application:

 NSMutableString* bundlePath = [NSMutableString stringWithCapacity:4];
 [bundlePath appendString:[[NSBundle mainBundle] bundlePath]];

Now append your folder name to the bundlePath

 [bundlePath appendString:@"/MyFolder"];
 NSArray *directoryContent  = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:bundlePath error:nil];
 int numberOfFileInFolder = [directoryContent count];
like image 32
Jhaliya - Praveen Sharma Avatar answered Oct 29 '22 23:10

Jhaliya - Praveen Sharma