Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy folder from iPhone Resources directory to document directory

BOOL success;
NSFileManager *fileManager = [[NSFileManager defaultManager]autorelease];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,
                                                      NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:@"DB"];
success = [fileManager fileExistsAtPath:documentDBFolderPath];

if (success){
 return;
}else{
 NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath]
                                         stringByAppendingPathComponent:@"DB"];
 [fileManager createDirectoryAtPath: documentDBFolderPath attributes:nil];
    [fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath           
                                                                         error:&error];
 }
}

Like this.

Resources/DB/words.csv => DB folder copy => Document/DB/words.csv

I want to copy DB subdirectory at Resources folder. I thought that source is good. But that source makes folder and doesn't copy files in DB folder at Resources folder.

I really want to copy files in DB folder at Resources folder. please help me.

like image 916
Beomseok Avatar asked Jan 31 '10 09:01

Beomseok


2 Answers

1) Do not -autorelease the NSFileManager. You are double-releasing it which will crash your app.

2) No need to call -createDirectoryAtPath:. From the SDK doc of -copyItemAtPath:toPath:error:,

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

and creating the directory the copy to fail.

like image 183
kennytm Avatar answered Nov 03 '22 14:11

kennytm


Swift 3.0

Using String

func copyFolder(){

    // Get the resource folder
    if let resourceMainPath = Bundle.main.resourcePath{

        var isDirectory = ObjCBool(true)
        // Get the path of the folder to copy
        let originPath = (resourceMainPath as NSString).appendingPathComponent("NameOfFolder")
        // Get the destination path, here copying to Caches
        let destinationPath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first!
        // Append the folder name to dest path so that system creates the directory if it doesnt exist
        let destPath = (destinationPath as NSString).appendingPathComponent("/NameOfFolder")
        let fileManager = FileManager.default
        if fileManager.fileExists(atPath: destPath, isDirectory:&isDirectory ){
            // If an overwrite behavior is needed, remove and copy again here
             print("Exists")
        }else{
            // Do the copy
            do {
                try fileManager.copyItem(atPath: originPath, toPath: destPath)
            }catch let error{
                print(error.localizedDescription)
            }
        }
    }else{

    }

}

Using URL

func copyTheFolder(){

    // Get the resource folder
    if let resourceMainURL = Bundle.main.resourceURL{
        var isDirectory = ObjCBool(true)
        // Get the path of the folder to copy
        let originPath = resourceMainURL.appendingPathComponent("NameOfFolder")
        // Get the destination path, here copying to Caches
        let destinationPath = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true).first!
        // Append the folder name to dest path so that system creates the directory if it doesnt exist
        let destURL = URL(fileURLWithPath: destinationPath).appendingPathComponent("/NameOfFolder")
        let fileManager = FileManager.default
        if fileManager.fileExists(atPath: destURL.path, isDirectory:&isDirectory ){
            // If an overwrite behavior is needed, remove and copy again here
            print("Exists")

        }else{
            // Do the copy
            do {
                try fileManager.copyItem(at: originPath, to: destURL)

            }catch let error{
                print(error.localizedDescription)
            }
        }
    }
}
like image 22
anoop4real Avatar answered Nov 03 '22 12:11

anoop4real