how to copy file from main bundle to Document Folder

10,478

Solution 1

I don't think your documents folder is updating because an older version of that db exists. You can purge your documents directory with the following method, and add it to the top of your viewDidLoad:

- (void)viewDidLoad
{
    [self purgeDocumentsDirectory];
    [self CopyDatabase];
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)purgeDocumentsDirectory
{
    NSLog(@"Purging Documents Directory...");
    NSString *folderPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSError *error = nil;
    for (NSString *file in [[NSFileManager defaultManager] contentsOfDirectoryAtPath:folderPath error:&error]) {
        [[NSFileManager defaultManager] removeItemAtPath:[folderPath stringByAppendingPathComponent:file] error:&error];
    }
}

Solution 2

This is my solution

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

        NSString *pgnPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.text", @"100_Greatest_games"]];

        if ([fileManager fileExistsAtPath:pgnPath] == NO)
        {
            NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"100_Greatest_games" ofType:@"text"];
            [fileManager copyItemAtPath:resourcePath toPath:pgnPath error:&error];
            if (error) {
                NSLog(@"Error on copying file: %@\nfrom path: %@\ntoPath: %@", error, resourcePath, pgnPath);
            } 
    }
   }
Share:
10,478

Related videos on Youtube

MmD
Author by

MmD

Updated on September 15, 2022

Comments

  • MmD
    MmD over 1 year

    I want to check one file in Document folder.I want if this file not exist in document folder copy it from main bundle to document folder. I write this code and this file to be copy but my problem here.....!!!! my file is .sqlite file (Database file) and when copy to document folder hasn't data in self!!!! why??? while the this file when is in main bundle has data in self.

    this is my code but I don't know why not work!!!!

    #define DataName @"mydatabase.sqlite"
    
    
    - (void)viewDidLoad
    {
        [self CopyDatabase];
        [super viewDidLoad];
        // Do any additional setup after loading the view from its nib.
    }
    
    - (NSString*)DatabasePath
    {
        NSArray *Paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *DocumentDir = [Paths objectAtIndex:0];
        //NSLog(@"%@",DocumentDir);
        return [DocumentDir stringByAppendingPathComponent:DataName];
    
    }
    
    - (void)CopyDatabase
    {
        BOOL success;
        NSFileManager *fileManager = [NSFileManager defaultManager];
        success = [fileManager fileExistsAtPath:[self DatabasePath]];
        NSString *FileDB = [[[NSBundle mainBundle]resourcePath]stringByAppendingPathComponent:DataName];
        if (success)
        {
            NSLog(@"File Exist");
            return;
        }
        else
        {
            [fileManager copyItemAtPath:FileDB toPath:[self DatabasePath] error:nil];
        }
    
    }
    
    • trojanfoe
      trojanfoe over 10 years
      Show the definition of DataName. Also log the resulting FileDB string using NSLog(). Also don't pass error:nil; use this mechanism to get better error reporting... it's worth the 2 minutes of effort.
  • rmaddy
    rmaddy over 10 years
    Why would you want to purge all files in the Documents folder every time this view is loaded?
  • apollosoftware.org
    apollosoftware.org over 10 years
    i had the EXACT same issue above with an old copy of a sqlite.DB. The symptoms sounded familiar, hence the purge was what it required. It doesn't necessarily need to be loaded every time the view is loaded, rather if you do some simple logic like compare the two databases' timestamps or size, you can copy the new copy based on that outcome in lieu of every time, as needed. The Documents directories are notorious for leaving a mess of files behind.
  • apollosoftware.org
    apollosoftware.org over 10 years
    the other alternative would be to clean your targets, and pray your db doesn't change again, but for now leaving the purge statement in there until production may be helpful. Once the db stops changing then you can remove it simply.