PDA

View Full Version : Scanning directories for file type




larswik
Nov 25, 2011, 11:56 PM
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
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
Nov 26, 2011, 03:13 AM
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
Nov 26, 2011, 03:49 PM
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?

mfram
Nov 26, 2011, 03:56 PM
&& is basic C syntax: logical AND

If there is a file and it is a directory then ...

jiminaus
Nov 26, 2011, 04:53 PM
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
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):

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
Nov 26, 2011, 05:14 PM
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!!!