Ever needed to bundle a collection of files with your app to dump into the documents directory on first launch? I needed to copy a few directories and a couple of files for ChickenPing
First you need to drag your sample data folder into your project in XCode as a folder, not as a group. This gives it a blue icon instead of yellow (in XCode 4).
Next, in In application didFinishLaunchingWithOptions, you check to see whether a settings value (firstRun) is set. If not, you set it and call a method to copy your sample data folder over.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; if(![defaults objectForKey:@"firstRun"]){ [BusinessLogic copySampleData]; [defaults setObject:[NSDate date] forKey:@"firstRun"]; [defaults synchronize]; } - (void)applicationWillTerminate:(UIApplication *)application { [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"firstRun"]; } |
The method to actually copy the data will iterate through all files in your directory and copy them over to the documents directory.
+(void)copySampleData { NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *sourcePath = [[NSBundle mainBundle] pathForResource:@"sampledata" ofType:nil]; NSError *error; NSFileManager *fileManager = [NSFileManager defaultManager]; NSArray *sourceFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:sourcePath error:&error]; NSLog(@"Source Path: %@\n Documents Path: %@ \nFiles: %d", sourcePath, documentsPath, [sourceFiles count]); for (NSString *fileName in sourceFiles) { NSString *sourceFilePath = [sourcePath stringByAppendingPathComponent:fileName]; NSString *destinationFilePath = [documentsPath stringByAppendingPathComponent:fileName]; if([fileManager fileExistsAtPath:destinationFilePath]){ if([fileManager removeItemAtPath:destinationFilePath error:&error]){ NSLog(@"Error deleting %@: %@ (%@)", destinationFilePath, [error localizedDescription], [error localizedFailureReason]); } } NSLog(@"Bundle File: %@", sourcePath); if(![fileManager copyItemAtPath:sourceFilePath toPath:destinationFilePath error:&error]){ NSLog(@"Error copying %@ -> %@: %@ (%@)",sourceFilePath, documentsPath, [error localizedDescription], [error localizedFailureReason]); } } } |

XCOPY will accept UNC pathnames Examples: To copy a file: XCOPY C:\utils\MyFile D:\Backup\CopyFile
To copy a folder: XCOPY C:\utils D:\Backup\utils /i
To copy a folder including all subfolders. XCOPY C:\utils\* D:\Backup\utils /s /i The /i defines the destination as a folder. Notes In many cases the functionality of XCOPY is superseded by ROBOCOPY . To always overwrite destination files use the COPYCMD environment variable: SET COPYCMD=/Y When comparing Dates/Times the granularity (the finest increment of the timestamp) is 2 seconds for a FAT volume and 0.1 microsecond for an NTFS volume.
The only problem is that you can’t deploy XCopy with your iOS app.