Tôi cố di chuyển một thư mục có tệp đến thư mục tạm thời nhưng tôi luôn nhận được cùng một lỗi: Thao tác không thể hoàn tất. (Lỗi ca cao 516.)Lỗi lạ khi di chuyển thư mục sang temp bằng moveItemAtPath. Lỗi ca cao 516
Đây là mã, bạn có thấy gì lạ? Cảm ơn trước.
// 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);
}
Cám ơn câu trả lời! Tôi quên nói rằng tôi đang làm việc cho nền tảng iOS, mặc dù tài liệu và mã lỗi giống nhau nếu bạn thay đổi URL "mac" bằng "ios". Tôi đã thử những gì bạn đã nói, xóa ứng dụng của tôi khỏi trình mô phỏng và thiết bị của tôi (vì vậy, thư mục tạm thời trống) và nói 516. Giới thiệu về những gì bạn nói về một tệp, trong tài liệu tôi có thể đọc: "moveItemAtPath: toPath: lỗi: Di chuyển tệp hoặc thư mục tại đường dẫn được chỉ định đến một vị trí mới đồng bộ " Bạn có biết cách nào khác để di chuyển thư mục trong thư mục tmp không? Danh mục hoặc thư viện? Cảm ơn! – Ricardo
Nhân tiện, mã của bạn hoạt động, nhưng chỉ di chuyển một tập tin, nhưng tôi thích di chuyển một thư mục đầy đủ các tập tin. Bất kỳ ý tưởng? Có lẽ sử dụng đệ quy, nhưng tôi chỉ muốn di chuyển thư mục vì xóa thư mục (hoặc sao chép tệp theo tệp) mất vài giây (có nhiều tệp, khoảng 500 MB). Bất kỳ ý tưởng hoặc đề xuất nào? Cảm ơn sự giúp đỡ của bạn. – Ricardo
Nó sẽ hoạt động nếu bạn chỉ cần vượt qua đường dẫn thư mục thay vì đường dẫn tệp, tức là thay vì chỉ 'newPath' sử dụng' [newPath stringByDeletingLastPathComponent] 'để lấy thư mục mà newFile đang ở? – deanWombourne