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

larswik

macrumors 68000
Original poster
Sep 8, 2006
1,552
11
I am moving files from folder to folder. I would think there is a convince method in NSFileManager that returns the full string or URL path to a file. I can easily get the contents of a directory like this

Code:
    NSFileManager *fm = [NSFileManager defaultManager];
    NSString *filepath = @"~/SomeDirectory";
    filepath = [filepath stringByExpandingTildeInPath];
    NSArray *fileNamesArray = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:filepath error:NULL];

As I NSLog the array it contains the file names. I would like to get the full paths to the files. Is there a simple method to return the full path? I looked through the docs for NSFileManager and the moveItemsAtPath: method expects the full path to the item.

Is there no method like this sudo code -(NSArray*)contentPathsAtDirectory:?

BTW, I am able to get the path of the files in question and move them just fine. I was just wondering if there was a method that I am missing that simplifies this process?
 
Thanks Chown33. I didn't think to look in the NSString class since I was working with files.

Now that I think about it that should have been obvious. moveItemsAtPath: expects 3 parameters, 2 of them being NSStrings for the paths. I would think I would find a solution method, if one exists, in the class that it is asking for.
 
Last edited:
It can sometimes be unclear where to look.

I think of it as "names" vs. "files". If the operation involves names of things, i.e. pathnames, path components, etc. than I start in NSString. If the operation involves actual access to the file-system, then I start in NSFileManager.

Even that doesn't always work, because some collection classes can read and write their content to files, without using NSFileManager. E.g. see NSArray and NSDictionary and their writeToFile: and xxWithContentsOfFile: methods.
 
Also, don't use "~" in paths in Objective-C. "~" is a shell construct that won't be useful to you in C/Obj-C. You'll have to use the NSFileManager calls to get the user's home directory path if you want some subdirectory underneath there.
 
Also, don't use "~" in paths in Objective-C. "~" is a shell construct that won't be useful to you in C/Obj-C. You'll have to use the NSFileManager calls to get the user's home directory path if you want some subdirectory underneath there.

There are NSString methods for expanding a tilde in a pathname, and abbreviating to one. See the NSString class reference doc, and find "tilde" on the page.
 
Also, don't use "~" in paths in Objective-C. "~" is a shell construct that won't be useful to you in C/Obj-C. You'll have to use the NSFileManager calls to get the user's home directory path if you want some subdirectory underneath there.

stringByExpandingTildeInPath works just fine.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.