0

Is it possible to copy a folder to a non empty destination without deleting the contents of the destination folder first? My current implementation is as follows

var folderObject = NSFileManager.defaultManager();
folderObject.removeItemAtPath_error(toPath, nil);
folderObject.copyItemAtPath_toPath_error(fromPath, toPath, nil));

What i need to achieve is overwrite the contents of destination folder with that of source folder.

Vardaan
  • 47
  • 1
  • 7
  • You should [edit] your question to show the implementation of `removeItemAtPath_error` and `copyItemAtPath_toPath_error` since those are not built-in functions. Btw this is Swift, don't use `;` at the end of the lines and conform to the Swift naming convention, which is lowerCamelCase for function names. You should also use argument labels rather than include them in the function name. So for instance change `removeItemAtPath_error` to `removeItem(at path:String,error:Error?)`, same for the other function. – Dávid Pásztor Jun 11 '18 at 10:40
  • Is `toPath` the destination folder and `fromPath` the source folder? – Willeke Jun 11 '18 at 11:03
  • @DávidPásztor The Swift tag was added by trungduc. The question was tagged Objective-C. `removeItemAtPath:error:` and `copyItemAtPath:toPath:error:` are `NSFileManager` methods. @Vandaan is this Python? – Willeke Jun 11 '18 at 11:13
  • @Willeke the code in the question doesn't make any sense in either case. That syntax is Swift syntax, but the function signatures don't match the built-in method's signatures in Swift. From the syntax, the tag editing seems like a valid decisions, but that piece of code isn't valid for calling the built-in `NSFileManager` methods either from Swift or Objective-C. – Dávid Pásztor Jun 11 '18 at 11:28
  • 2
    @DávidPásztor Take a look at [PyObjC](https://pyobjc.readthedocs.io/en/latest/core/intro.html) – Willeke Jun 11 '18 at 12:07
  • @Willeke @ DávidPásztor I am using the above in sketchapp which uses cocoascript – Vardaan Jun 12 '18 at 04:55

1 Answers1

0

The call removeItemAtPath_error deletes the destination directory and copyItemAtPath_toPath_error creates a directory of the same name as the deleted one, hence it is empty.

As a side note, the naming of your NSFileManager instance is somewhat misleading, as it is not a folder object but a file manager instance reference.

An example of how this can be done (I'm unfamiliar with PyObjC, so this is only for inspiration):

# Define paths
var sourcePath = "/my/source/path/"
var targetPath = "/my/target/path/"

# Create reference to file manager
var fileManager = NSFileManager.defaultManager();

# Create array of all file and directory paths inside the source directory
var sourcePaths = fileManager.contentsOfDirectoryAtPath_sourcePath_error(sourcePath, nil); 

# For each source file or directory
for sourcePath in sourcePaths:

  # Copy the file or directory to the target path
  fileManager.copyItemAtPath_toPath_error(sourcePath, targetPath, nil);

I am assuming that this recursively copies the directories from the source and maintains the directory tree.

Manuel
  • 14,274
  • 6
  • 57
  • 130