42

In my project I have two file .txt (in Resources folder), how can I copy them inside documents folder?

cyclingIsBetter
  • 17,447
  • 50
  • 156
  • 241

3 Answers3

118

Copies txtFile from resource to document if not already present.

NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *txtPath = [documentsDirectory stringByAppendingPathComponent:@"txtFile.txt"];

if ([fileManager fileExistsAtPath:txtPath] == NO) {
    NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"txtFile" ofType:@"txt"];
    [fileManager copyItemAtPath:resourcePath toPath:txtPath error:&error];
}

If you want to overwrite every time then try this:

if ([fileManager fileExistsAtPath:txtPath] == YES) {
    [fileManager removeItemAtPath:txtPath error:&error];
}

NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"txtFile" ofType:@"txt"];
[fileManager copyItemAtPath:resourcePath toPath:txtPath error:&error];
Monolo
  • 18,205
  • 17
  • 69
  • 103
taskinoor
  • 45,586
  • 12
  • 116
  • 142
  • In that case if the file already exists then remove that using removeItemAtPath:error: – taskinoor Jul 01 '11 at 08:17
  • ok then, if I want to make changes at my file, I modify the file that is inside "Resource Folder" and after I replace this file in documents folder as you show me...ok? – cyclingIsBetter Jul 01 '11 at 08:25
  • If you have changed the file in resource then the file in document should be overwritten after running the 2nd code. Is it working or not? Have you checked? – taskinoor Jul 01 '11 at 08:30
  • another thing: in this way my file is in documents folder and it's ok. Now I want to read this file, what is the way to read it? – cyclingIsBetter Jul 01 '11 at 08:34
5

Swift 3

code for file is exist or not, if not then copy.

func CheckFileisExistOrNot(strPath:String) {
    let filemgr = FileManager.default
    if !filemgr.fileExists(atPath: strPath) {
        let resorcePath = Bundle.main.path(forResource: "\(Global.g_databaseName)", ofType: ".db")
        do {
            try filemgr.copyItem(atPath: resorcePath!, toPath: strPath)
        }catch{
            print("Error for file write")
        }
    }
}
Community
  • 1
  • 1
Ilesh P
  • 3,940
  • 1
  • 24
  • 49
1
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [[paths objectAtIndex:0]stringByAppendingString:@"csvfile"];

NSString *txtPath = [documentsDirectory stringByAppendingPathComponent:@"sample.csv"];

if ([fileManager fileExistsAtPath:txtPath] == NO) {
    NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"sample" ofType:@".csv"];
    [fileManager copyItemAtPath:resourcePath toPath:txtPath error:&error];
sagar modi
  • 37
  • 7