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);
}