Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 977
R. Dewi Avatar asked Mar 12 '12 15:03

R. Dewi


People also ask

How do you get a list of all files in a folder?

Press and hold the SHIFT key and then right-click the folder that contains the files you need listed. Click Open command window here on the new menu. A new window with white text on a black background should appear. o To the left of the blinking cursor you will see the folder path you selected in the previous step.

How do I read all files in a directory in CPP?

Call opendir() function to open all file in present directory. Initialize dr pointer as dr = opendir("."). If(dr) while ((en = readdir(dr)) != NULL) print all the file name using en->d_name.

How do I get a list of files in a folder from text?

Right-click that folder and select Show more options. Click Copy File List to Clipboard on the classic menu. You'll still need to paste the copied list into a text file. Launch Run, type Notepad in the Open box, and click OK.


2 Answers

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

like image 161
arun.s Avatar answered Oct 19 '22 15:10

arun.s


You want to use enumeratorAtURL:includingPropertiesForKeys:options:error: instead. It does a deep enumeration by default:

NSURL *myDirectoryURL = [[NSBundle mainBundle] URLForResource:@"Assets" withExtension:@""];
NSDirectoryEnumerator *directoryEnumerator = [[NSFileManager defaultManager] enumeratorAtURL:myDirectoryURL includingPropertiesForKeys:[NSArray array] options:0 errorHandler:^BOOL(NSURL *url, NSError *error) {
    // handle error
    return NO;
}];

NSString *fileOrDirectory = nil;
while ((fileOrDirectory = [directoryEnumerator nextObject])) {
    // use file or directory
}

The options argument lets you specify deep or shallow enumeration.

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

like image 5
Jess Bowers Avatar answered Oct 19 '22 16:10

Jess Bowers