Scanning directories for file type

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

  1. macrumors 65816

    Joined:
    Sep 8, 2006
    Messages:
    1,494
    #1
    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 XP.....app
    After the OK button is pressed on the NSOpenPanel window
    Code:
    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

    itickings

    Joined:
    Apr 14, 2007
    Messages:
    921
    #2
    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. macrumors 65816

    Joined:
    Sep 8, 2006
    Messages:
    1,494
    #3
    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?
    Code:
    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

    Joined:
    Jan 23, 2010
    Messages:
    791
    Location:
    San Diego, CA USA
    #4
    && is basic C syntax: logical AND

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

    jiminaus

    Joined:
    Dec 16, 2010
    Messages:
    1,448
    Location:
    Sydney
    #5
    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.
    Code:
    + (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 
                   enumeratorAtURL:rootURL
                   includingPropertiesForKeys:nil
                   options:NSDirectoryEnumerationSkipsPackageDescendants
                           | NSDirectoryEnumerationSkipsHiddenFiles
                   errorHandler:errorHandlerOrNil
               ];
        
        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):
    Code:
    NSArray *contents 
        = [MYObject 
            findFilesWithExtension:@"mov"
            recursivelyStartingAtPath:@"/Users/jiminaus"
            withErrorHandler:^BOOL(NSURL *url, NSError *error) {
                    NSLog(@"Error at %@: %@", url, error);
                    return YES;
                }
          ];
    
    for (NSString *fileName in contents) {
        NSLog(@"%@", fileName);
    }
    
     
  6. macrumors 65816

    Joined:
    Sep 8, 2006
    Messages:
    1,494
    #6
    Hi Jim,
    YES

    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