Cullen SUN
Cullen SUN

Reputation: 3547

How to get the directory tree of iPhone App Documents Directory

In my App, I created subfolders under Documents, and subfolders under subfolders. So there is a Document tree. I want to get the tree in a way that I am able to list them in a tableview. They will look like the following, linear listing with different indent to show their level.

enter image description here

I am not sure whether there is some convenient way to get this done or not, when I am trying to do it using while loop and for loop.

Appreciate your suggestions.

Thanks.

Upvotes: 1

Views: 1185

Answers (3)

UPT
UPT

Reputation: 1480

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

and

NSFileManager *manager = [NSFileManager defaultManager];
    NSArray *fileList = [manager directoryContentsAtPath:documentsDirectory];
    for (NSString *s in fileList){
       NSLog(s);
    }

Upvotes: 0

Moshe
Moshe

Reputation: 58067

Apple provides a number of classes for file system access, and even some for recursion that you're trying to do.

I suggest that you take a look at the Apple File System Programming Guide and the NSFileManager class documentation. Additionally, the NSDirectoryEnumerator class may help as well.

Upvotes: 1

Mohammad
Mohammad

Reputation: 1666

I do not know how to get entire hierarchy at a time. But following method can be helpful.

1 Use this to get documents derectory.

NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentPath = [paths objectAtIndex:0];

2 use - (NSArray *)contentsOfDirectoryAtPath:(NSString )path error:(NSError *)error for getting list of directories in document directory.

Read this Class refrence for more details.

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSFileManager_Class/Reference/Reference.html

3 Using following method you can make path of internal directory.

NSString *directoryPath = [documentPath stringByAppendingPathComponent:[MAIN_DIRECTORY stringByAppendingPathComponent:filePath]];

Using above three can together you can brows content of document directory.

Hope this is helpful

Upvotes: 0

Related Questions