Mac Scanning directories for file type

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
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
2011-11-25 21:40:27.783 Mov Catalog[5577:903] Mov Array has (
"dynamics_reel.mov",
"LaCumbre_Comp.mov",
"sandbar_30.mov",
"SkyHighSports.mov",
"sy_fly_in.mov",
"UNIT-0004 Bonqo BelievesMed_prog.mov"
)
2011-11-25 21:40:27.784 Mov Catalog[5577:903] dir Array had (
"3D camera rig",
"4-23-11 invoice",
"aug 11 hotel invoice",
bf,
BLANK,
"Canon 7D CineStyle",
"My Music Bouce",
pascal,
"Porsche Photos",
"red dwarf 8",
"stopwatch stills",
"xCode Videos",
"Xp Calc9.3.app",
"Xp Calc_9.2.app",
"Xp Calc_v9.1.app",

"YMCA Zoe"
)
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];

}
 

itickings

macrumors 6502a
Apr 14, 2007
924
5
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.
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.
 

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
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?
 

jiminaus

macrumors 65816
Dec 16, 2010
1,449
1
Sydney
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);
}
 

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
Hi Jim,
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;
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!!!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.