Search code examples
iphoneobjective-cnsfilemanager

How to move a directory to a new location on iPhone


Here's my method which should simply move the contents of a directory from /someDirectory to /addons/id/UUID:

  CFUUIDRef uuidObj = CFUUIDCreate(nil); //create a new UUID
  //get the string representation of the UUID
  NSString *uuidString = (NSString*)CFUUIDCreateString(nil, uuidObj);

  //MOVE the addon to the addons directory addons/shortname/UUID
  NSString *pathToAddon = [LWEFile createDocumentPathWithFilename:[NSString stringWithFormat:@"%@", relPath]];
  NSString *pathToAddonDest = [LWEFile createDocumentPathWithFilename:[NSString stringWithFormat:@"addons/%@/%@", [character objectForKey:@"shortName"], uuidString]];

  // move the files
  NSError* error;
  if([[NSFileManager defaultManager] moveItemAtPath:pathToAddon toPath:pathToAddonDest error:&error] != YES)
  {
    NSLog(@"Unable to move file: %@", [error localizedDescription]);
  }

  //release the uuid stuff
  [uuidString release];
  CFRelease(uuidObj);

The move fails with The operation couldn’t be completed. (Cocoa error 4.). However the same code works if I change pathToAddonDest to:

NSString *pathToAddonDest = [LWEFile createDocumentPathWithFilename:[NSString stringWithFormat:@"addons/%@", [character objectForKey:@"shortName"], uuidString]];

So I can write from /someDirectory to /addons/someDirectory but not from /someDirectory to /addons/someDirectory/UUID.

Any ideas why a seemingly simple rename wouldn't work in this way?


Solution

  • You should create directory before before you could move it there. /addons/someDirectory/UUID --- create this path before you try moving the content.