Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mdeh

macrumors 6502
Original poster
Jan 3, 2009
345
2
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.


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

macrumors G5
Jan 5, 2006
12,576
1,692
Redondo Beach, California
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
Code:
find -name foobar
 

mdeh

macrumors 6502
Original poster
Jan 3, 2009
345
2
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
Code:
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.


Code:
#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

Moderator emeritus
Jun 15, 2000
7,958
7
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

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
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:
Code:
#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

macrumors 601
Apr 8, 2004
4,029
142
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)
 

mdeh

macrumors 6502
Original poster
Jan 3, 2009
345
2
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

macrumors 6502
Original poster
Jan 3, 2009
345
2
............ 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:
Code:
#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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.