简体   繁体   English

如何在iOS的现有文件夹中查找子文件夹

[英]how to find subfolder in an existing folder in iOS

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. 该子文件夹包含一个html文件,而我又一次不知道该html文件的名称。

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? 你知道我在哪里错吗?

Use NSDirectoryEnumerator . 使用NSDirectoryEnumerator It should help you. 它应该可以帮助您。

Use NSDirectoryEnumerator like this. 像这样使用NSDirectoryEnumerator。

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);

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在sqlite iOS中查找现有文件夹名称 - How to find existing folder name in sqlite iOS iOS:更新子文件夹中的文件夹结构 - iOS: Update folder structure in subfolder 如何找到iOS模拟器文件夹 - How to find the iOS simulator folder iOS >> CoreData >>如何找到特定的应用程序文件夹? - iOS >> CoreData >> How to find the Specific App Folder? iOS:在Cashes文件夹和每个子文件夹中搜索视频文件 - iOS: Search for video files in cashes folder and each subfolder 使用现有的 ios 框架进行统一。 如何找到viewController的替换 - using existing ios framework for unity. how to find replacement of viewController 如何 Flutter 将文件写入 iOS iPhone“在我的手机上查找”文件夹 - How to Flutter write files to iOS iPhone “Find on my phone” Folder iOS / Swift 4:如何在不删除文件和文件夹现有目标的情况下复制包含多个包含文件的文件夹的文件夹? - iOS/Swift 4: How to copy a folder containing several folders containing files without removing the files and folders existing destination? 如何在iOS中创建文件夹 - How to create the folder in ios 在Dreamweaver中找不到ios的/ Developer文件夹? - Can't find /Developer folder for ios in Dreamweaver?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM