Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How get list of folders and files from resource folder in iPhone?

I am doing folder structure in my resource folder like... Resource => MyData => S1 then in S1=> Name.png, data.ppt

Now I want to get all folder list and file names. Here MyData name will be only static other may change.Like I can add S1, S2 , S3 and number of files in that. So how to read these contents through Objective C? I tried below code.

NSString *bundlePathName = [[NSBundle mainBundle] bundlePath];
    NSString *dataPathName = [bundlePathName stringByAppendingPathComponent:@"Resources"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:dataPathName]) {
        NSLog(@"%@ exists", dataPathName);
        BOOL isDir = NO;
        [fileManager fileExistsAtPath:dataPathName isDirectory:(&isDir)];
        if (isDir == YES) {
            NSLog(@"%@ is a directory", dataPathName);
            NSArray *contents;
            contents = [fileManager contentsOfDirectoryAtPath:dataPathName error:nil];
            for (NSString *entity in contents) {
                NSLog(@"%@ is within", entity);
            }
        } else {
            NSLog(@"%@ is not a directory", dataPathName);
        }
    } else {
        NSLog(@"%@ does not exist", dataPathName);
    }

Thanks,

like image 205
Swapnil Avatar asked Dec 08 '11 11:12

Swapnil


1 Answers

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];
like image 132
Srikar Appalaraju Avatar answered Sep 21 '22 14:09

Srikar Appalaraju