Nombre de la carpeta en la ruta de mi directorio en iPhone

Soy nuevo en 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.

Cualquier ayuda será apreciada.

preguntado el 30 de julio de 12 a las 06:07

6 Respuestas

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
}

ahora 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.)

Respondido 30 Jul 12, 06:07

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

Respondido 30 Jul 12, 06:07

Hacer esto:

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);

Respondido 30 Jul 12, 06:07

echa un vistazo al NSFileManager clase

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];

Respondido 30 Jul 12, 06:07

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

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

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

Respondido 30 Jul 12, 06:07

Método más simple:

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);
        }
    }

respondido 19 nov., 13:04

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.