2013-02-18 40 views
5

我在NSDocumentDirectory創造了一些目錄,如何獲取NSDocumentDirectory中directores的列表?

if (![tbxCreateFolder.text isEqualToString:@""]) { 
NSArray *arrPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectoryStr = [arrPaths objectAtIndex:0]; // Get documents folder 
NSString *dataPath = [documentsDirectoryStr stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@",tbxCreateFolder.text]]; 

if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]){ 

    NSError* error; 
    if([[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]){ 

     NSLog(@"success"); 
     //Perfrom DB Insertation 
    } 
    else 
    { 
     NSLog(@"[%@] ERROR: attempting to write create MyFolder directory", [self class]); 
     NSAssert(FALSE, @"Failed to create directory maybe out of disk space?"); 
    } 
} 

}

現在,我想我在文件夾創建的目錄列表,但反應

NSArray *arrPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *basePathString = ([arrPaths count] > 0) ? [arrPaths objectAtIndex:0] : nil; 
NSLog(@"dirContents %@",basePathString); 

反應是:

/Users/amir/Library/Application Support/iPhone Simulator/6.1/Applications/7003E8D5-CCBB-4E54-896B-F4BD2F4D2CB1/Documents 

回答

4

這是代碼:

NSString *documentDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
// create directory named "test" 
[[NSFileManager defaultManager] createDirectoryAtPath:[documentDirPath stringByAppendingPathComponent:@"test"] withIntermediateDirectories:YES attributes:nil error:nil]; 
// retrieved all directories 
NSLog(@"%@", [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentDirPath error:nil]); 

NSArray *paths = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentDirPath error:nil]; 
BOOL isDir = NO; 
for (NSString *path in paths) { 
    if ([fileManager fileExistsAtPath:path isDirectory:&isDir] && isDir) { 
     // path is directory 
    } 
} 
+0

也在所有文件中響應。但我只想得到目錄的名稱。 – 2013-02-18 08:15:31

+0

@AmiriDev我已經更新了代碼。 – SAPLogix 2013-02-18 08:28:39

0

爲此,您可以用代碼幾行簡單

NSString *documentDirectoryPath = // Initialize with Documents path 
BOOL isDir = NO; 
NSFileManager *fileManager = [NSFileManager defaultManager]; 
NSArray *files = [fileManager contentsOfDirectoryAtPath:documentDirectoryPath error:NULL]; 

NSMutableArray *directoriesPaths = [NSMutableArray array]; 
for (NSString *filePath in files) { 
    if ([fileManager fileExistsAtPath:filePath isDirectory:&isDir] && isDir) { 
     [directoriesPaths addObject:filePath]; 
    } 
} 

NSLog(@"directories %@", directoriesPaths); 
+0

可以請你填寫「NSString * documentDirectoryPath = //用文檔路徑初始化」 「 – 2013-02-18 08:26:52

+0

當然可以。你已經有了Documents目錄的路徑,在你的代碼中它是'basePathString'。在你的下面添加我的代碼,但在此之前,將'NSString * documentDirectoryPath = //用文檔路徑初始化爲'NSString * documentDirectoryPath = basePathString' – pawelropa 2013-02-18 09:06:25

3

你總是可以列舉使用-fileExistsAtPath:isDirectory:這樣的頂級目錄的內容:

NSFileManager *sharedFileManager = [NSFileManager defaultManager]; 
NSDirectoryEnumerator *files = [sharedFileManager enumeratorAtPath:yourPath]; 

for (NSString *path in files) { 
    BOOL isDirectory = NO; 
    [sharedFileManager fileExistsAtPath:path isDirectory:&isDirectory]; 
    if (isDirectory) { 
     // you found a directory, so do what you will with it 
    } 
} 
0

試試這個,

NSArray *arrPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *strFilePath = [[arrPath objectAtIndex:0] 
[[NSFileManager defaultManager] createDirectoryAtPath:[strFilePath stringByAppendingPathComponent:@"Sample"] withIntermediateDirectories:YES attributes:nil error:nil]; 
+0

此列表文件是否存在或只有目錄? – 2013-07-11 16:47:38