views:

761

answers:

3

Hi,

I need to copy a few sample files from my app's resource folder and place them in my app's document folder. I came up with the attached code, it compiles fine but it doesn't work. All the directories I refer to do exist. I'm not quite sure what I am doing wrong, could someone point me in the right direction please?

NSFileManager*manager = [NSFileManager defaultManager];

NSString*dirToCopyTo = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

NSString*path = [[NSBundle mainBundle] resourcePath];

NSString*dirToCopyFrom = [path stringByAppendingPathComponent:@"Samples"];


NSError*error;

NSArray*files = [manager contentsOfDirectoryAtPath:dirToCopyFrom error:nil];

for (NSString *file in files)
{
        [manager copyItemAtPath:[dirToCopyFrom stringByAppendingPathComponent:file] toPath:dirToCopyTo error:&error];

        if (error)
        {
            NSLog(@"%@",[error localizedDescription]);
    }
}

EDIT: I just edited the code the way it should be. Now however there's another problem:

2010-05-15 13:31:31.787 WriteIt Mobile[4587:207] DAMutableDictionary.h 2010-05-15 13:31:31.795 WriteIt Mobile[4587:207] FileManager Error:Operation could not be completed. File exists

EDIT : I have fixed the issue by telling NSFileManager the names of the copied files's destinations.

        [manager copyItemAtPath:[dirToCopyFrom stringByAppendingPathComponent:file] toPath:[dirToCopyTo stringByAppendingPathComponent:file] error:&error];
+2  A: 

I think the problem is in this line:

NSArray*files = [manager contentsOfDirectoryAtPath:dirToCopyTo error:nil];

You are listing files in a destination directory instead of the source. Change it to something like:

NSArray*files = [manager contentsOfDirectoryAtPath:dirToCopyFrom error:nil];

And you should be fine.

Eimantas
thanks! this was indeed the problem. now i've hit another one though - this is my log output:2010-05-15 13:31:31.787 WriteIt Mobile[4587:207] DAMutableDictionary.h2010-05-15 13:31:31.795 WriteIt Mobile[4587:207] FileManager Error:Operation could not be completed. File exists2010-05-15 13:31:31.795 WriteIt Mobile[4587:207] DAMutableDictionary.m2010-05-15 13:31:31.802 WriteIt Mobile[4587:207] FileManager Error:Operation could not be completed. File existsetc... etc....what does that mean exactly?
David Schiefer
Seems that you're trying to overwrite a file. Before copying - make sure it does not exist.
Eimantas
but the Documents directory is empty? which is why I am surprised?
David Schiefer
even after resetting the iPhone Simulator, it still says the same.
David Schiefer
+2  A: 

I think the problem is that yo are reading the files to copy from dirToCopyTo and I think you meant dirToCopyFrom

Also to get the documents directory you should be using NSDocumentDirectory with - (NSArray *)URLsForDirectory:(NSSearchPathDirectory)directory inDomains:(NSSearchPathDomainMask)domainMask

Mark
it doesn't really matter since both return the same result ;)
David Schiefer
In a non English version of OSX is the directory always Documents?
Mark
yes marc, my iPhone is set to german and all documents directories are still called "Documents".
David Schiefer
Mark: That method doesn't exist on the iPhone. The only way in an iPhone app (aside from forging the path from whole cloth) is the `NSSearchPathForDirectoriesInDomains` function: http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Functions/Reference/reference.html#//apple_ref/c/func/NSSearchPathForDirectoriesInDomains
Peter Hosey
A: 
    [manager copyItemAtPath:[dirToCopyFrom stringByAppendingPathComponent:file] toPath:dirToCopyTo error:&error];

The destination path is the path you want the copy to have, including its filename. You cannot pass the path to a directory expecting NSFileManager to fill in the name of the source file; it will not do this.

The documentation says that the destination path must not describe anything that exists:

… dstPath must not exist prior to the operation.

In your case, it's the path to the destination directory, so it does exist, which is why the copy fails.

You need to make it a path to the destination file by appending the desired filename to it. Then it will not exist (if not previously copied), so the copy will succeed.

Peter Hosey