Strange error when moving a folder to temp with moveItemAtPath. Cocoa error 516
13,559
Solution 1
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);
}
Solution 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]
Related videos on Youtube
Author by
Ricardo
Updated on June 04, 2022Comments
-
Ricardo almost 2 years
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); }
-
Ricardo about 12 yearsThanks 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!
-
Ricardo about 12 yearsBy 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.
-
deanWombourne about 12 yearsIt 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? -
Ricardo about 12 yearsThanks 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/Documents
-
Mat about 12 yearsthis is the best solution if you move folders with same name.