Search code examples
objective-cipadios5nsfilemanager

how to get all files in a directory in ipad using objective c


i need to get all files in a directory, including all sub directories and all files in each subdirectory. in objective C, i have try to use this method [[NSFileManager defaultManager] contentsOfDirectoryAtPath:filePath error:nil]

but it just give me an array of contents file in directory filePath, i cant get all files in the sub directory, can somebody help me??

thank you


Solution

  • - (void)scanPath:(NSString *) sPath {
    
        BOOL isDir;
    
        [[NSFileManager defaultManager] fileExistsAtPath:sPath isDirectory:&isDir];
    
        if(isDir)
        {
            NSArray *contentOfDirectory=[[NSFileManager defaultManager] contentsOfDirectoryAtPath:sPath error:NULL];
    
            int contentcount = [contentOfDirectory count];
            int i;
            for(i=0;i<contentcount;i++)
            {
                NSString *fileName = [contentOfDirectory objectAtIndex:i];
                NSString *path = [sPath stringByAppendingFormat:@"%@%@",@"/",fileName];
    
    
                if([[NSFileManager defaultManager] isDeletableFileAtPath:path])
                {   
                    NSLog(path);
                    [self scanPath:path];
                }
            }
    
        }
        else
        {
            NSString *msg=[NSString stringWithFormat:@"%@",sPath];
            NSLog(msg);
        }
    }
    

    you can call this function which will log all the files in the directory, hope this will help.