Search code examples
cocoansfilemanager

How do I copy a UNIX Executable File with NSFileManager?


I am trying to use NSFileManager copyItemAtURL:toURL:error: to move a UNIX executable file (a command line program) from one directory to another but I always get an error that says the URL type is unsupported. I assume this is because without an extension on the file it is being viewed as a directory but I'm not sure. Is it possible to move this type of file with NSFileManager?

Edit: Here is my code

#define SAVE_DIR   [@"~/Library/Prog" stringByExpandingTildeInPath]
#define PROG_PATH  [SAVE_DIR stringByAppendingString:@"/ProgCom"]
#define RESOURCES  [[NSBundle mainBundle] resourcePath]
#define LOCAL_PROG [RESOURCES stringByAppendingString:@"/ProgCom"]

-(void)moveProg
{
    NSError *error = nil;
    NSURL *fromURL = [NSURL URLWithString:LOCAL_PROG];
    NSURL *toURL = [NSURL URLWithString:PROG_PATH];

    NSLog(@"%@", [fromURL path]);
    NSLog(@"%@", [toURL path]);

    if ([fMan fileExistsAtPath:[fromURL path]]) {
        [fMan copyItemAtURL:fromURL
                      toURL:toURL
                      error:&error];
        if (error)
            [NSApp presentError:error];
    }
}

The error I receive:

The file couldn't be opened because the specified URL type isn't supported.

And finally what gets logged:

fromURL = /Users/Nick/Library/Developer/Xcode/DerivedData/Prog-dpnblqaraeuecyadjgizbinfrtcm/Build/Products/Debug/Prog.app/Contents/Resources/ProgCom

toURL = /Users/Nick/Library/Prog/ProgCom

Solution

  • The problem is you're using +[NSURL URLWithString:]. This is producing an invalid URL, since you're not actually giving it one. What you want is +[NSURL fileURLWithPath:], which will produce a file:///Users/Nick/... URL.