开发者

Copy Directories With copyItemAtPath?

I am trying to copy the directory "DATA_001" (and its contents) into the directory "CRYO". I w开发者_StackOverflow中文版as under the impression that I could do this using copyItemAtPath like I would for a file? Is this the wrong way to be doing this?

NSString *sourceDir = @"/Users/Fuzzygoat/Documents/DATA_001";
NSString *destDir   = @"/Users/Fuzzygoat/Documents/CRYO";
NSString *sourceFile = @"/Users/Fuzzygoat/Documents/DATA_001/caroline.png";
NSString *destFile   = @"/Users/Fuzzygoat/Documents/CRYO/cjg.png";

// COPY DIR
success = [fileManager copyItemAtPath:sourceDir toPath:destDir error:&dError];
if(success != YES) NSLog(@"Error");

// COPY FILE
success = [fileManager copyItemAtPath:sourceFile toPath:destFile error:&fError];
if(success != YES) NSLog(@"Error");

gary


If you copy a directory all of the contents are recursively copyed, which means your second call to copy is completely superfluous, you can just do this:

NSString *sourceDir = @"/Users/Fuzzygoat/Documents/DATA_001";
NSString *destDir   = @"/Users/Fuzzygoat/Documents/CRYO";

// COPY DIR
success = [fileManager copyItemAtPath:sourceDir toPath:destDir error:&dError];
if(success != YES) NSLog(@"Error: %@", dError);

Obviously if you don't want to copy everything in the directory you should not copy the directory itself, and instead only copy the entries you want.

I should note that you didn't specify that you were having any particular problems. There are obviously a number of reasons this sort of thing can fail (permissions, issues, etc), which should be indicated by the value of dError. If you are asking this question because you have been getting unexpected results you need to include more details about what is going on and how it differs from your expectations.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜