[英]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.