Monday 2 March 2015

get all files in nested directory in iOS Objective C

NSArray *arrayFilesPath = [self openEachFileAt:DOCUMENT_PATH];
NSLog(@"All path %@", arrayFilesPath);
- (NSMutableArray*)openEachFileAt:(NSString*)path {
    NSString* file;
    
    NSMutableArray *arrayFilePath = [NSMutableArray new];
    
    NSDirectoryEnumerator* enumerator = [[NSFileManager defaultManager] enumeratorAtPath:path];
    
    while (file = [enumerator nextObject])
    {
        // check if it's a directory
        BOOL isDirectory = NO;
        
        [[NSFileManager defaultManager] fileExistsAtPath: [NSString stringWithFormat:@"%@/%@",path,file]
                                             isDirectory: &isDirectory];
        if (!isDirectory)
        {
            // open your file …
            [arrayFilePath addObject:[NSString stringWithFormat:@"%@/%@",path,file]];
        }
        else
        {
            [arrayFilePath addObjectsFromArray:[self openEachFileAt: file]];
        }
    }
    
    return arrayFilePath;
}

No comments:

Post a Comment