Getting a list of files in the Resources folder - iOS

CodeGuy picture CodeGuy · Jun 18, 2011 · Viewed 97.8k times · Source

Let's say I have a folder in my "Resources" folder of my iPhone application called "Documents".

Is there a way that I can get an array or some type of list of all the files included in that folder at run time?

So, in code, it would look like:

NSMutableArray *myFiles = [...get a list of files in Resources/Documents...];

Is this possible?

Answer

Deepak Danduprolu picture Deepak Danduprolu · Jun 18, 2011

You can get the path to the Resources directory like this,

NSString * resourcePath = [[NSBundle mainBundle] resourcePath];

Then append the Documents to the path,

NSString * documentsPath = [resourcePath stringByAppendingPathComponent:@"Documents"];

Then you can use any of the directory listing APIs of NSFileManager.

NSError * error;
NSArray * directoryContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:&error];

Note : When adding source folder into the bundle make sure you select "Create folder references for any added folders option when copying"