Question

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];
Was it helpful?

Solution

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.

OTHER TIPS

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

Please note that lengthy operation on startup must be avoided:

  1. Not a good User Experience (delay and croppy behavior)
  2. Watchdog in iOS can kill your app as if it were stuck.

So perform copy in a secondary thread (or operation... or whatever uses a different execution path).

Another problem will arise if You need data to populate your UI: in that case:

  • Disable UI elements
  • Start an async / threaded operation
  • In the completion call back of copying (via a notification, a protocol.. or other means) notify to the UI interface it can start fetching data.

For example we copy a ZIP file and decompress it, but it takes some time so we had to put it in a timer procedure that will trigger UI when done.

If You need an example, ket me know.

PS:
Copying using ZIP file is MORE efficient as:

  1. Only call to file system
  2. Far less bytes to copy

The bad news: you must use a routine do decompress zip file, but you can find them on the web.

Decompressing Zip files should be more efficient as these calls are written in straight C, and not in Cocoa with all the overhead.

    [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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top