tags:

views:

276

answers:

1

I am trying to copy the directory "DATA_001" (and its contents) into the directory "CRYO". I was 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

A: 

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.

Louis Gerbarg
Thank you Louis, the copy_Dir and then the copy_File were just for testing, so thats why I included both. I was under the impression that my code was wrong, which seems not to be the case from your Answer. I will go back and have a better look. Many thanks again.
fuzzygoat
Its actually pilot error, or rather me being a muppet. What I had done was create the "CRYO" directory thinking it would copy the files in. dError was indeed the key as it said "file already exists" ... many thanks again and sorry for wasting your time.
fuzzygoat