PDA

View Full Version : Searching for a named file




mdeh
Feb 16, 2009, 03:22 PM
I wish to find a specific named file, and get a path ref to it.
So far, I have done this,which is extremely inelegant.


while ( (path = [ dirEnum nextObject]))
{
NSLog(@"%@", path);
[ fm fileExistsAtPath: path isDirectory: &flag];
found = [path rangeOfString: @"Desktop"];
if ( flag != NO && found.location == NSNotFound )
[dirEnum skipDescendents];
}


What I end up with are a lot of paths that have the Directory desktop, ( which is what I want ( indirectly)), but what I really want is a specific file on the desktop in a folder called "Chapter 7".

Is there an elegant way of finding the specific file .

The reason I want to do this programmatically is that the file name is "/Users/m/Desktop/Chapter\ 7/7-7/Exercise_2.0_Ch7_7.m" and I want to avoid using the escape characters...if that makes any sense.


Anyway...an approach would be most helpful.

Thank you.



ChrisA
Feb 16, 2009, 05:08 PM
I wish to find a specific named file, and get a path ref to it.
......
Anyway...an approach would be most helpful.

Thank you.

From the terminal type "man find" (no quotes) to read the manual page for the "find" command. It will do what you need.

For example find -name foobar

mdeh
Feb 16, 2009, 05:15 PM
From the terminal type "man find" (no quotes) to read the manual page for the "find" command. It will do what you need.

For example find -name foobar

Well...you did answer my question!!! :)

I guess this is more of an instructive ( for me) issue.

for example...I have narrowed down one area that is not working.


#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSFileManager *fm = [NSFileManager defaultManager];
NSDirectoryEnumerator *dirEnum ;
NSString *path;
BOOL flag = NO;
NSString *value;

dirEnum = [fm enumeratorAtPath: NSHomeDirectory()];

while ( (path =[dirEnum nextObject]) != nil){
([ fm fileExistsAtPath: path isDirectory: &flag] );
value = (flag == YES) ? @"YES" : @"NO";
NSLog(@"%s",[value UTF8String]);
}


Flag never changes...and I am trying to figure out why..perhaps I am tired.

kainjow
Feb 16, 2009, 10:16 PM
I think NSDirectoryEnumerator returns a relative path to what you created it with, and NSFileManager works on absolute paths, so you need to do something like NSString *fullPath = [NSHomeDirectory() stringByAppendingPathComponent:path]; and use that instead.

lee1210
Feb 16, 2009, 10:19 PM
The path you are getting does not include the initial directory the enumerator is running over. You are not checking the return value of fileExistsAtPath:isDirectory:, if you did you'd see that was also false for every entry. Essentially, if you have a file called a.txt in your home directory, path would just contain the string a.txt. You'd pass that to fileExistsAtPath, and that file does not exist. I don't really know what directory this will look in (maybe /?), but it's not your home directory. You can try this code instead:

#import <Foundation/Foundation.h>

int main(int argc, char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSFileManager *fm = [NSFileManager defaultManager];
NSDirectoryEnumerator *dirEnum ;
NSString *path;
BOOL flag = NO;
NSString *value;
NSString *dir = NSHomeDirectory();

dirEnum = [fm enumeratorAtPath: NSHomeDirectory()];

while ( (path =[dirEnum nextObject]) != nil){
[fm fileExistsAtPath:[dir stringByAppendingPathComponent:path] isDirectory: &flag];
value = flag ? @"YES" : @"NO";
NSLog(@"Path: %@, Is Directory? %@",[dir stringByAppendingPathComponent:path],value);
}
[pool release];
return 0;
}


It will append the "starting directory" to the path returned, stick them together, then evaluates if that fileExistsAtPath:isDirectory:.

-Lee

EDIT: Bah, kainjow got his post up a few minutes before i finished testing this.

plinden
Feb 16, 2009, 10:42 PM
If you're looking for an "elegant" way to search for files programmatically, did you think about using Spotlight's API (http://developer.apple.com/samplecode/SpotlightAPI/)?

(Edit: That's actually for Tiger, but I think the API's the same in Leopard)

mdeh
Feb 17, 2009, 08:51 AM
I think NSDirectoryEnumerator returns a relative path to what you created it with, and NSFileManager works on absolute paths, so you need to do something like NSString *fullPath = [NSHomeDirectory() stringByAppendingPathComponent:path]; and use that instead.


Yes....I missed that completely...thank you.

mdeh
Feb 17, 2009, 08:55 AM
............ Essentially, if you have a file called a.txt in your home directory, path would just contain the string a.txt. .............. You can try this code instead:

#import <Foundation/Foundation.h>

int main(int argc, char *argv[]) {

........snip...........

while ( (path =[dirEnum nextObject]) != nil){
[fm fileExistsAtPath:[dir stringByAppendingPathComponent:path] isDirectory: &flag];
..............
[pool release];
return 0;
}


It will append the "starting directory" to the path returned, stick them together, then evaluates if that fileExistsAtPath:isDirectory:.



Thanks Lee....of course....!!! but I had spent a while and it was time for help. Once again, your help is much appreciated.