2012-02-20 23 views
5

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

Trả lời

13

Lỗi 516 là NSFileWriteFileExistsError

Bạn không thể di chuyển một tập tin đến một nơi mà một tập tin đã tồn tại :)

(Xem tài liệu ở đây - https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Constants/Reference/reference.html và tìm kiếm cho '516')


Thêm hữu ích, đường dẫn đích của bạn phải là tên tệp chứ không phải thư mục. Hãy thử điều này:

// 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); 
} 
+0

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

+0

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

+0

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

2

Có được một tên thư mục tạm độc đáo bằng cách sử dụng các phương pháp sau đây:

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] 
+0

Đây là giải pháp tốt nhất nếu bạn di chuyển các thư mục có cùng tên. – Mat

0

Đường dẫn mục tiêu cho moveItemAtPath:toPath:error: phải là con đường mới hoàn chỉnh của tệp hoặc thư mục bạn đang di chuyển. Khi bạn làm điều đó ngay bây giờ, về cơ bản bạn đang cố gắng ghi đè lên thư mục tạm thời với tệp của bạn.

sửa chữa đơn giản:

NSString *targetPath = [tmpDir stringByAppendingPathComponent:[newPath lastPathComponent]]; 
if ([[NSFileManager defaultManager] moveItemAtPath:targetPath toPath: error:&error]) { 
    NSLog(@"Moved sucessfully"); 
} 
Các vấn đề liên quan