Venkat1282
Venkat1282

Reputation: 113

How to find out the folders information in the the document directory

I am developing an application. In it I want to find out the number of folders in the document directory and find the XML file in each folder. Please tell me how to find out the folders' information in the the document directory.

Upvotes: 0

Views: 118

Answers (2)

tim
tim

Reputation: 1682

If I understand you correctly you want to find all xml files in all folders in the documents directory?

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

NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory
                                                                        error:nil];
for (NSString *path in contents) {
    BOOL isDir;
    if ([[NSFileManager defaultManager] fileExistsAtPath:path
                                             isDirectory:&isDir]
        && isDir) {
        // directory in documents directory
        NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path
                                                                                   error:nil];
        for (NSString *dirContentsPath in dirContents) {
            if ([[dirContentsPath pathExtension] isEqualToString:@"xml"]) {
                NSLog(@"xml found: %@", dirContentsPath);
            }
        }
    }
}

You might want to add recursion for deeper directories.

Upvotes: 1

Janak Nirmal
Janak Nirmal

Reputation: 22726

To get List of directories use following code, there is 1 attribute isDirectory which gives you true/false for folder/file.

NSFileManager *fileManager = [NSFileManager defaultManager];
fileList = [[fileManager directoryContentsAtPath:PATH_TO_SEARCH] retain];
NSMutableArray *arrDirectory = [[NSMutableArray alloc] init];
for(NSString *file in fileList) 
{
    NSString *path = [PATH_TO_SEARCH stringByAppendingPathComponent:file];
    BOOL isItDirectory = NO;
    [fileManager fileExistsAtPath:path isDirectory:(&isItDirectory)];
    if(isItDirectory)
        [arrDirectory addObject:file];
}

your array will contain all the directory list here. You can follow same pattern for xml files.

Hope this helps.

Upvotes: 1

Related Questions