title: Copy Folder (w/contents) from bundle to Documents directory - iOS
body: <h1>EDIT: SOLVED</h1>
  
  <p>Thanks Brooks. Your question led me to keep digging into if the file even existed in my bundle - and it did not!</p>
  
  <p>So by using this code (also below): iPhone/iPad: Unable to copy folder from NSBundle to NSDocumentDirectory and the instructions for adding a directory properly to Xcode (from <a href="http://www.iphonedevsdk.com/forum/iphone-sdk-development/31256-nsbundle-pathforresource-indirectory-gives-nil.html" rel="noreferrer">here</a> and below)I was able to get it to work.</p>
  
  <p>Copy folder into Xcode:</p>
  
  <ol>
  <li>Create a Directory on your Mac.</li>
  <li>Select Add Existing Files to your project</li>
  <li>Select the Directory you want to import</li>
  <li>In the pop-up window make sure you select "Copy items into
  destination group's folder" and "Create Folder References for any
  added folders"</li>
  <li>Hit "Add"</li>
  <li><p>The Directory should appear blue instead of yellow.</p>
  
  <pre><code>-(void) copyDirectory:(NSString *)directory {
  NSFileManager *fileManager = [NSFileManager defaultManager];
  NSError *error;
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDirectory = [paths objectAtIndex:0];
  NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:directory];
  NSString *resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:directory];
  
  if (![fileManager fileExistsAtPath:documentDBFolderPath]) {
      //Create Directory!
      [fileManager createDirectoryAtPath:documentDBFolderPath withIntermediateDirectories:NO attributes:nil error:&amp;error];
  } else {
      NSLog(@"Directory exists! %@", documentDBFolderPath);
  }
  
  NSArray *fileList = [fileManager contentsOfDirectoryAtPath:resourceDBFolderPath error:&amp;error];
  for (NSString *s in fileList) {
      NSString *newFilePath = [documentDBFolderPath stringByAppendingPathComponent:s];
      NSString *oldFilePath = [resourceDBFolderPath stringByAppendingPathComponent:s];
      if (![fileManager fileExistsAtPath:newFilePath]) {
          //File does not exist, copy it
          [fileManager copyItemAtPath:oldFilePath toPath:newFilePath error:&amp;error];
      } else {
          NSLog(@"File exists: %@", newFilePath);
      }
  }
  </code></pre>
  
  <p>}</p></li>
  </ol>
  
  <p>========================
  END EDIT</p>
  
  <p>Frus-stray-shee-on! Anyway...</p>
  
  <p>The code below copies my folder from the app bundle to the Documents folder in the simulator just fine. However on the device I get an error and no folder. Using ze Google I found out that the error (260) means the file (in this case my folder) does not exist.</p>
  
  <p>What could be going wrong? Why can I not copy my folder from the bundle to the Documents? I have checked that the files exist - although the folder is not showing up - because Xcode wants flat file? Did it turn my folder (dragged into Xcode) into a flat file of assets instead?</p>
  
  <p>I thank you for any assistance.</p>
  
  <pre><code>//  Could not copy report at path /var/mobile/Applications/3C3D7CF6-B1F0-4561-8AD7-A367C103F4D7/cmsdemo.app/plans.gallery to path /var/mobile/Applications/3C3D7CF6-B1F0-4561-8AD7-A367C103F4D7/Documents/plans.gallery. error Error Domain=NSCocoaErrorDomain Code=260 "The operation couldn‚Äôt be completed. (Cocoa error 260.)" UserInfo=0x365090 {NSFilePath=/var/mobile/Applications/3C3D7CF6-B1F0-4561-8AD7-A367C103F4D7/cmsdemo.app/plans.gallery, NSUnderlyingError=0x365230 "The operation couldn‚Äôt be completed. No such file or directory"}
  
  NSString *resourceDBFolderPath;
  
  NSFileManager *fileManager = [NSFileManager defaultManager];
  NSError *error;
  NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory,
                                                       NSUserDomainMask, YES);
  NSString *documentsDirectory = [paths objectAtIndex:0];
  NSString *documentDBFolderPath = [documentsDirectory stringByAppendingPathComponent:@"plans.gallery"];
  BOOL success = [fileManager fileExistsAtPath:documentDBFolderPath];
  
  if (success){
      NSLog(@"Success!");
      return;
  } else {
      resourceDBFolderPath = [[[NSBundle mainBundle] resourcePath]
                                        stringByAppendingPathComponent:@"plans.gallery"];
      [fileManager createDirectoryAtPath: documentDBFolderPath attributes:nil];
      //[fileManager createDirectoryAtURL:documentDBFolderPath withIntermediateDirectories:YES attributes:nil error:nil];
  
      [fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath           
                            error:&amp;error];
  }
  
      //check if destinationFolder exists
  if ([ fileManager fileExistsAtPath:documentDBFolderPath])
  {
      //removing destination, so source may be copied
      if (![fileManager removeItemAtPath:documentDBFolderPath error:&amp;error])
      {
          NSLog(@"Could not remove old files. Error:%@",error);
          return;
      }
  }
  error = nil;
  //copying destination
  if ( !( [ fileManager copyItemAtPath:resourceDBFolderPath toPath:documentDBFolderPath error:&amp;error ]) )
  {
      NSLog(@"Could not copy report at path %@ to path %@. error %@",resourceDBFolderPath, documentDBFolderPath, error);
      return ;
  }
  </code></pre>