1
votes

My folder contains only one sub-folder, and I don't know sub-folder's name. This sub-folder contains a html file, and once again I don't know the html file's name.

My question is how I can get full path of this file by using

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

//- access sub-folder?
//- access html file?

EDITED:

I wrote a method to return the only one sub-folder as follow:

+ (NSString*) get1stSubFolder:(NSString*)folder
{
    NSDirectoryEnumerator *directoryEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:folder];

    //- no recursive
    [directoryEnumerator skipDescendents];

    NSString* file;
    while (file = [directoryEnumerator nextObject])
    {
        BOOL isDirectory = NO;
        BOOL subFileExists = [[NSFileManager defaultManager] fileExistsAtPath:file isDirectory:&isDirectory];
        if (subFileExists && !isDirectory) {

            return file;
        }
    }

    return nil;
}

I always get nil as result. Do you know where was I wrong at?

2

2 Answers

1
votes
1
votes

Use NSDirectoryEnumerator like this.

NSURL *documentsDirectoryURL = [NSURL URLWithString:NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] ];
///If the folder you want to browse for subfolders is NSDocumentDirectory.

NSArray *keys = [NSArray arrayWithObject:NSURLIsDirectoryKey];

NSDirectoryEnumerator *enumerator = [[[NSFileManager alloc] init]
                                     enumeratorAtURL:documentsDirectoryURL
                                     includingPropertiesForKeys:keys
                                     options:0
                                     errorHandler:^(NSURL *url, NSError *error) {
                                         return YES;
                                     }];

 for (NSURL *url in enumerator) {
    NSError *error;
    NSNumber *isDirectory = nil;
    if (! [url getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:&error]) {
        NSLog(@"Error %@",error);
    }
    else if ([isDirectory boolValue]) {
        NSLog(@"Folder URL: %@",url);
    }else{
        NSLog(@"File URL: %@",url);

    }
}