Krunal
Krunal

Reputation: 6490

Name of folder in my directory path in iPhone

I am new to iPhone,

I want to check how many folders are there in my Document Directory and i want fetch name of all folders and i want to store it in my array.

I want only name of folders and not contents inside folder.

Any help will be appreciated.

Upvotes: 3

Views: 1449

Answers (6)

Rajesh Loganathan
Rajesh Loganathan

Reputation: 11217

Simplest method:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSFileManager *manager = [NSFileManager defaultManager];
NSArray *imageFilenames = [manager contentsOfDirectoryAtPath:documentsDirectory error:nil];

for (int i = 0; i < [imageFilenames count]; i++)
    {

    NSString *imageName = [NSString stringWithFormat:@"%@/%@",documentsDirectory,[imageFilenames objectAtIndex:i] ];

        if (![[imageFilenames objectAtIndex:i]isEqualToString:@".DS_Store"])
        {
          NSLog(@"\n %@",imageName);
        }
    }

Upvotes: 0

cloosen
cloosen

Reputation: 993

    #define  rootFileName  XXXXXXXX   
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *yourDirectory = [documentsDirectory stringByAppendingPathComponent:rootFileName];
    //fileList便是包含有该文件夹下所有文件的文件名及文件夹名的数组
    NSArray *fileList = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:yourDirectory error:nil];

        for(int i = 0; i < [fileList count]; i++)
        {
            NSString *fileName = [fileList objectAtIndex:i];
            NSLog(@"yourFileName:%@",fileName);
            if([fileName isEqualToString:@".DS_Store"])
            {
                NSString *dotDS_StorePath = [NSString stringWithFormat:@"%@/.DS_Store",yourDirectory];
                [[NSFileManager defaultManager] removeItemAtPath:dotDS_StorePath error:nil];
            }
        }

there is one thing you should know , the fileLists has a more file is .DS_Store , the file is created by the os.

Upvotes: 2

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39978

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

NSArray *filePathsArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:documentsDirectory  error:nil];

NSLog(@"files array %@", filePathsArray);

Upvotes: 0

John Corbett
John Corbett

Reputation: 1615

take a look at the NSFileManager class

it sounds like you want the subpathsAtPath: method here's the sample code

BOOL isDir=NO;
NSArray *subpaths;
NSString *fontPath = @"/System/Library/Fonts";
NSFileManager *fileManager = [[NSFileManager alloc] init];
if ([fileManager fileExistsAtPath:fontPath isDirectory:&isDir] && isDir)
    subpaths = [fileManager subpathsAtPath:fontPath];
[fileManager release];

Upvotes: 0

Paresh Navadiya
Paresh Navadiya

Reputation: 38239

DO this:

NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *fileList = [[fileManager directoryContentsAtPath:yourDocumentPath] retain]; // your path here instead (DOCUMENTS_FOLDER)
NSMutableArray *directoryList = [[NSMutableArray alloc] init];
for(NSString *file in fileList) {
    NSString *path = [yourDocumentPath stringByAppendingPathComponent:file]; // create path for directory and check it exits or not as directory
    BOOL isDir = NO;
    [fileManager fileExistsAtPath:path isDirectory:(&isDir)];
    if(isDir) {
        [directoryList addObject:file];
    }
}
NSLog(@"%@", directoryList);

Upvotes: 1

user529758
user529758

Reputation:

NSFileManager *mgr = [NSFileManager defaultManager];
NSString *documentsDir [NSSearchPathsForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSArray *allFilesInDocuments = [mgr contentsOfDirectoryAtPath:documentsDir error:NULL];
NSMutableArray *found = [NSMutableArray array];
for (NSString *filename in allFilesInDocuments)
{
    NSString *fullPath = [documentsDir stringByAppendingPathComponent:filename];
    BOOL isDir;
    [mgr fileExistsAtPath:fullPath isDirectory:&isDir];
    if (isDir) [found addObject:fullPath]; // or filename
}

now found should contain all the paths/names of the subdirectories of the Documents directory (i. e. entries in the Documents directory which are directories themselves.)

Upvotes: 3

Related Questions