Strange error when moving a folder to temp with moveItemAtPath. Cocoa error 516
Asked Answered
S

3

5

I try to move a folder with a file to a temp folder, but I always receive the same error: The operation couldn’t be completed. (Cocoa error 516.)

This is the code, do you see anything strange? Thanks in advance.

    // create new folder
NSString* newPath=[[self getDocumentsDirectory] stringByAppendingPathComponent:@"algo_bueno"];
NSLog(@"newPath %@", newPath);
if ([[NSFileManager defaultManager] fileExistsAtPath:newPath]) {
    NSLog(@"newPath already exists.");
} else {
    NSError *error;
    if ([[NSFileManager defaultManager] createDirectoryAtPath:newPath withIntermediateDirectories:YES attributes:nil error:&error]) {
        NSLog(@"newPath created.");
    } else {
        NSLog(@"Unable to create directory: %@", error.localizedDescription);
        return;
    }
}

// create a file in that folder
NSError *error;
NSString* myString=[NSString stringWithFormat:@"Probando..."];
NSString* filePath=[newPath stringByAppendingPathComponent:@"myfile.txt"];
if ([myString writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error]) {
    NSLog(@"File created.");
} else {
    NSLog(@"Failed creating file. %@", error.localizedDescription);
    return;
}

// move this folder and its folder
NSString *tmpDir = NSTemporaryDirectory();
NSLog(@"temporary directory, %@", tmpDir);
if ([[NSFileManager defaultManager] moveItemAtPath:newPath toPath:tmpDir error:&error]) {
    NSLog(@"Movido a temp correctamente");
} else {
    NSLog(@"Failed moving to temp. %@", error.localizedDescription);
}
Statutable answered 20/2, 2012 at 13:23 Comment(0)
A
13

Error 516 is NSFileWriteFileExistsError

You can't move a file to a place where a file already exists :)

(See docs here - https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Constants/Reference/reference.html and search for '516')


More usefully, your destination path should be a file name, not a folder. Try this :

// move this folder and its folder
NSString *tmpDir = NSTemporaryDirectory();
NSString *tmpFileName = [tmpDir stringByAppendingPathComponent:@"my-temp-file-name"];
NSLog(@"temporary directory, %@", tmpDir);
if ([[NSFileManager defaultManager] moveItemAtPath:newPath toPath:tmpFileName error:&error]) {
    NSLog(@"Movido a temp correctamente");
} else {
    NSLog(@"Failed moving to temp. %@", error.localizedDescription);
}
Actinochemistry answered 20/2, 2012 at 13:28 Comment(4)
Thanks for reply! I forgot to say I'm working for the iOS platform, although the document and the error code is the same if you change in the URL "mac" by "ios". I tried what you said, removing my app from my simulator and device (so, temp dir is empty) and it says 516. About what you say about a file, in the docs I can read: "moveItemAtPath:toPath:error: Moves the file or directory at the specified path to a new location synchronously" Do you know any other way to move a folder inside tmp folder? A category or library? Thanks!Statutable
By the way, your code works, but only moves a file, but I prefer to move a folder full of files. Any idea? Perhaps using recursion, but I just wanted to move the folder because deleting the folder (or copy file by file) takes a few seconds (there are many files, about 500 MB). Any idea or suggestion? Thanks a lot for your help.Statutable
It should work if you just pass the folder path in instead of a file path i.e. instead of just newPath use [newPath stringByDeletingLastPathComponent] to get the folder that newFile is in?Actinochemistry
Thanks for reply, but no luck. I receive the same error, 516, an actually I wouldn't like to move the Documents folder to temp, just "a_folder" folder * /Users/ricardo/Library/Application Support/iPhone Simulator/5.0/Applications/A38C0282-D203-44F5-9F4B-AC560F7054D1/Documents/a_folder * /Users/ricardo/Library/Application Support/iPhone Simulator/5.0/Applications/A38C0282-D203-44F5-9F4B-AC560F7054D1/DocumentsStatutable
M
2

Obtain a unique temp folder name by using the following method:

NSFileManager unique file names

CFUUIDRef uuid = CFUUIDCreate(NULL);
CFStringRef uuidString = CFUUIDCreateString(NULL, uuid);
NSString *tmpDir = [[NSTemporaryDirectory() stringByAppendingPathComponent:(NSString *)uuidString];
CFRelease(uuid);
CFRelease(uuidString);
// MOVE IT
[[NSFileManager defaultManager] moveItemAtPath:myDirPath toPath:tmpDir error:&error]
Metagalaxy answered 6/5, 2012 at 1:24 Comment(1)
this is the best solution if you move folders with same name.Metagalaxy
T
0

The target path for moveItemAtPath:toPath:error: has to be the complete new path of the file or directory you're moving. As you do it now, you're basically trying to overwrite the temp directory itself with your file.

Simple fix:

NSString *targetPath = [tmpDir stringByAppendingPathComponent:[newPath lastPathComponent]];
if ([[NSFileManager defaultManager] moveItemAtPath:targetPath toPath: error:&error]) {
   NSLog(@"Moved sucessfully");
}
Tonatonal answered 6/5, 2012 at 1:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.