Discussion in 'Mac Programming' started by larswik, Oct 23, 2013.

  1. larswik macrumors 68000

    Sep 8, 2006
    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

        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?
  2. chown33 macrumors 604

    Aug 9, 2009
  3. larswik, Oct 23, 2013
    Last edited: Oct 23, 2013

    larswik thread starter macrumors 68000

    Sep 8, 2006
    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.
  4. chown33 macrumors 604

    Aug 9, 2009
    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.
  5. mfram macrumors 65816

    Jan 23, 2010
    San Diego, CA USA
    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.
  6. chown33 macrumors 604

    Aug 9, 2009
    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.
  7. gnasher729 macrumors P6


    Nov 25, 2005
    stringByExpandingTildeInPath works just fine.

Share This Page