Search code examples
iphoneiosipadnsfilemanagernsdocumentdirectory

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.


Solution

  •     #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.