Search code examples
objective-ccocoafile-uploadcopynsfilemanager

how to copy a file on the network in cocoa


I would like to copy the selected file from my computer to another computer on the same network. I tried to use NSFileManager but I was not successful. Could please help how to do it?

NSFileManager *fileManager = [[NSFileManager alloc] init];
NSString * filePath = [NSHomeDirectory() stringByAppendingPathComponent:
[NSString stringWithFormat:@"Documents/test"]];
NSString * filePath2 = [NSHomeDirectory() stringByAppendingPathComponent:
[NSString stringWithFormat:@"Shared/Test"]];
[fileManager copyItemAtPath:filePath toPath:filePath2 error:NULL];
[fileManager release];

Solution

  • 2 suggestions:

    1) as per the documentation, in this line the error should be "nil" not "NULL"

    [fileManager copyItemAtPath:filePath toPath:filePath2 error:NULL];
    

    2) Maybe the code is not finding the files. I notice you do not have any file extension on the paths (maybe "test" should be "test.txt"?). Most files have an extension even if you can't see the extension in the Finder. Get Info on the file to check its extension and fix the code if that's the case.