# File Path relative to current file



## rhale1 (Mar 23, 2003)

Looking at all of the path documentation, this seems possible. I want to get a file's path (I do this with an open panel) relative to the current file/directory. That is, if I have a file called myself.file and want to find the path of additionalfile.file in relation to myself.file (so that I don't get things like /users/os_user/Documents/whatever or /Library/WebServer/Documents). How exactly would I do this in Cocoa? I can get the full path, but this isn't what I need. Any ideas?


----------



## Darkshadow (Mar 24, 2003)

Hmm...you can get the entire contents of a certain directory, then go through that for the file you need (contents are in an array). 

[[NSFileManager defaultManager]directoryContentsAtPathath]


----------



## binaryDigit (Mar 25, 2003)

I think a clearer example may be warranted here.  Do you mean that if you have:

/foo/bar/myfile.file

and 

/foo/baz/otherfile.file

that you want to figure out that the relative path to otherfile.file (in relation to myfile.file) is:

../baz/otherfile.file

Is this what you're trying to calculate?


----------



## rhale1 (Mar 25, 2003)

Yes, it is. I got a hit from Cocoa-Dev, by John Anderson. I'll post it here incase anyone else wants this help.

Now I am trying to get an NSPopUpButton menu to be populated from an array. That is, I want an NSMenuItem to correspond to a record in my Array (stored via NSUserDefaults), so that if my array has values Ryan, Josh, Joe, Lisa for key "UserName" in an Array, I want the menu to show all of the values as seperate menu items.


----------



## binaryDigit (Mar 25, 2003)

> _Originally posted by rhale1 _
> *Yes, it is. I got a hit from Cocoa-Dev, by John Anderson. I'll post it here incase anyone else wants this help.
> ...
> *



Is there actually some Cocoa calls that do this for you?  I assumed that you would just parse the respective path strings to get what you wanted?

Sorry, can't help on your other question.


----------



## rhale1 (Mar 25, 2003)

Insert these into your app, and then call what you need. Note: I didn't write these, and John Anderson (who did) says they may be buggy, but I didn't have any trouble w/ them.

The .m file:
http://rhdev.marhost.com/dev/PathProcessing.m

The .h file:
http://rhdev.marhost.com/dev/PathProcessing.h


----------



## Arden (Mar 25, 2003)

Shouldn't that be the other way around?


----------



## rhale1 (Mar 25, 2003)

Thanks, didn't notice that. All better now!


----------



## binaryDigit (Mar 25, 2003)

OK, so it is just some code to do it.  I was going to be really impressed if someone had taken the time to actually make this a part of the os api.  Thanks for posting it.


----------



## rhale1 (Mar 25, 2003)

No problem.


----------

