1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Searching for a named file

Discussion in 'Mac Programming' started by mdeh, Feb 16, 2009.

  1. macrumors 6502

    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.
  2. macrumors G4

    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
  3. macrumors 6502

    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.
  4. Moderator emeritus


    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.
  5. macrumors 68040


    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:.


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


    If you're looking for an "elegant" way to search for files programmatically, did you think about using Spotlight's API?

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


    Yes....I missed that completely...thank you.
  8. macrumors 6502

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

Share This Page