Scanning directories for file type

Discussion in 'Mac Programming' started by larswik, Nov 25, 2011.

  1. macrumors 68000

    Sep 8, 2006
    I am working on a little app for my Mac that will scan directories for a file type like, "mov" files. I have NSOpenPanel pop up and then I select the directory and when I hit OK It saves the directory to an NSMutableArray. I then sort through the Array to see if the file is of .mov type or another directory. If it is a mov I save it to a new array or if it finds a directory it saves it to a different array. It works fine except it is adding anything with .app extension to the directory array. here is a list of the results and code. At the bottom you can see the
    After the OK button is pressed on the NSOpenPanel window
    if (result == NSOKButton){
            BOOL dirPathVerify = NO;
            dirPath = [[NSString alloc]initWithFormat:[getFile filename]];
            NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:dirPath error:nil];
            for (int i = 0; i < dirContents.count; i++) { 
               NSString *aPath = [dirContents objectAtIndex:i];
               NSString *fullPath = [dirPath stringByAppendingPathComponent:aPath];
                if ([filemgr fileExistsAtPath:fullPath isDirectory: &dirPathVerify] &&dirPathVerify ) {
                    [dirArray addObject:aPath];
                else if ([[aPath pathExtension] isEqualToString:@"mov"]) {
                    [movArray addObject:aPath];
            NSLog(@"Mov Array has %@", movArray);
            NSLog(@"dir Array had %@", dirArray);
        [movArray release];
        [dirArray release];
  2. macrumors 6502a


    Apr 14, 2007
    Are you sure it is adding everything with an .app extension, or is it adding just real application bundles? They are directory hierarchies after all, with the top-level directory having a name that ends with a .app extension.
  3. thread starter macrumors 68000

    Sep 8, 2006
    I am not sure? This is some new territory for me. I thought it would be fun to expand my coding a bit this weekend. I wanted to generate a list with the .mov files on my external hard drive to better help me find clips for the videos I work on. As I scan a folder(directory) it finds mov files and other directories that I want to scan through. So in 1 Array I gather the mov information and in the second Array I gather the other directories paths to scan later.

    I found this line of code on line but I don't get all of it?
    if ([filemgr fileExistsAtPath:fullPath isDirectory: &dirPathVerify] &&dirPathVerify )
    I understand the first part and it checks to see if the file exists at that path. The next part isDirectory I think is self explanatory if it is a file or directory. But the next 2 with the & and the double &&? From reading C I know the & refers to the address of the variables location but the double && I have no clue?
  4. macrumors 6502a

    Jan 23, 2010
    San Diego, CA USA
    && is basic C syntax: logical AND

    If there is a file and it is a directory then ...
  5. macrumors 65816


    Dec 16, 2010
    Lars, are you wanting to just find all the .mov files in a given directory and recursively the subdirectories, and the subdirectories of the subdirectories, and so on; but without ever find any .mov files in a package?

    If so you should be using NSFileManager's enumeratorAtURL:includingPropertiesForKeys:options:errorHandler:.

    For example, this kind of method will do that.
    + (NSArray *)
        findFilesWithExtension:(NSString *)extenstion
        recursivelyStartingAtPath:(NSString *)path 
        withErrorHandler:(BOOL (^)(NSURL *url, NSError *error))errorHandlerOrNil
        NSMutableArray *contents = [NSMutableArray array];
        NSURL *rootURL = [NSURL fileURLWithPath:path];
        NSFileManager *fileManager = [NSFileManager defaultManager];        
        NSDirectoryEnumerator *dirEnumerator
            = [fileManager 
                           | NSDirectoryEnumerationSkipsHiddenFiles
        for (NSURL* url in dirEnumerator) {
            NSString *urlExtension = [url pathExtension];
            if ([urlExtension isEqualToString:extenstion]) {
                NSString *fileName = [url path];
                [contents addObject:fileName];
        return contents;
    It can be used like so (assuming the method was put in a MYObject class):
    NSArray *contents 
        = [MYObject 
            withErrorHandler:^BOOL(NSURL *url, NSError *error) {
                    NSLog(@"Error at %@: %@", url, error);
                    return YES;
    for (NSString *fileName in contents) {
        NSLog(@"%@", fileName);
  6. thread starter macrumors 68000

    Sep 8, 2006
    Hi Jim,

    I found myself searching for for video clips I shot half a year ago on my drive. I thought I would build a data base which could present thumbnails and meta data of all the clips on my drive with an extension of , mov. This sounded like a great opportunity to learn something new.

    From your code it looks like I started off using wrong code, went with what I knew so far. I got it working but was struggling with what you mentioned of going into sub-directories and then their sub-directories to locate these files and so on.

    I have never worked with NSFileManager's enumeratorAtURL so this sounds like a fun challenge for Saturday. I will read up on and it and thanks for pointing me in the right direction!!!

Share This Page