Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get all the file names of documents in my sandbox?

Tags:

ios

iphone

I have the following code:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

However I am clueless as to how to get all the files names and assign them to an array?

Any help would be appreciated.

like image 575
milof Avatar asked May 30 '11 20:05

milof


2 Answers

NSFileManager has a method called contentsOfDirectoryAtPath:error: that returns an array of all the files in that directory.

You can use it like this:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
if([paths count] > 0)
{
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSError *error = nil;
    NSArray *documentArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:&error];
    if(error)
    {
        NSLog(@"Could not get list of documents in directory, error = %@",error);
    }
}

The documentsArray object will contain a list of all the files in that directory.

like image 190
DHamrick Avatar answered Nov 04 '22 05:11

DHamrick


Use NSFileManager's contentsOfDirectoryAtPath:error: method.

like image 22
Anomie Avatar answered Nov 04 '22 07:11

Anomie