开发者

How to copy a file to portable drive root with cocoa?

I've tried the following

[[NSFileManager defaultManager] copyItemAtPath:@"whatever.txt"
 toPath:@"/Volumes/MyDrive" error:&copyError];

This gives me the error "The operation couldn’t be completed. File exists"

If I 开发者_开发问答try to copy it to "/Volumes/MyDrive/testFolder" everything copies to testFolder just fine.


I've tried the following

[[NSFileManager defaultManager] copyItemAtPath:@"whatever.txt"
 toPath:@"/Volumes/MyDrive" error:&copyError];

This gives me the error "The operation couldn’t be completed. File exists"

First, as KennyTM already told you, the error message is telling you one possible cause of the problem: The file already exists. The destination must not exist; you must either delete it or give a different name for the destination.

Second, there is another possible cause of the problem: You only specified the destination folder, not the complete destination path. You must specify the complete destination path, including the destination filename. Quoth the documentation:

“When a file is being copied, the destination path must end in a filename—there is no implicit adoption of the source filename.”

If you want the copy to have the pathname /Volumes/MyDrive/whatever.txt, that's the pathname you need to pass.

Also, don't forget to check whether the copy succeeded before you attempt to look at the error object. You should only look at the error object if the copy failed.

If I try to copy it to "/Volumes/MyDrive/testFolder" everything copies to testFolder just fine.

I think you'll find that testFolder is, in fact, a file—specifically, it's the copy of whatever.txt.


Isn't the error very clear? "The operation couldn’t be completed. File exists". The doc of -copyItemAtPath:… states that:

The file specified in srcPath must exist, while dstPath must not exist prior to the operation.

You need to call -removeItemAtPath:error: to remove the destination file if you want to override it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜