2
$\begingroup$

What is the easiest way to copy one directory into another programmatically?

I tried:

CopyDirectory[
    "...location1...\\MyFolder",
    "...location2...\\MyFolder"
]

There are two folders with the same name in two different locations (location1 & location2). I can use windows file explorer to copy the location1's MyFolder and paste it on the location2's MyFolder and it starts merging them, i.e. the following happens:

  1. Any subfolder that matches the same path within this folder will also be merged.
  2. Any filename that matches gets overwritten.
  3. Any file or folder that doesn't exist in location2's MyFolder will be copied over from location1's MyFolder.
  4. Any file or folder which only exists in location2's MyFolder will be ignored and will continue to exist in location2's MyFolder.

How to do the same operation with Wolfram?

$\endgroup$
3
  • 1
    $\begingroup$ Maybe CreateArchiveand ExtractArchive with the option OverwriteTarget-> True ? $\endgroup$
    – andre314
    Mar 9 at 20:37
  • $\begingroup$ @andre314 Thank you it works! $\endgroup$
    – user13892
    Mar 10 at 5:31
  • $\begingroup$ Note : I have not studied how the file metainformations (file date, directory date, attributes ...) follow. It can be system dependent (and it's catastrophics with windows file explorer : directory dates don't follow). Idem for maximum file names length .Hope you don't care. $\endgroup$
    – andre314
    Mar 10 at 19:55

1 Answer 1

2
$\begingroup$

CreateArchiveand ExtractArchive with the option OverwriteTarget-> True are probably the solution.

So far I know,ExtractArchive is the only way to create a bunch of files in the system without specifying the name of each file.

Since recently, I use CreateArchiveand ExtractArchive to copy html pages (with the subdirectory of all the linked images) inside the file system.

This is allready usefull, but my true use-case will be for embedding html files in notebooks. For this, I use Import[ the archived hmtl file, "Byte"] in a variable called theEmbeddedData , and the reverse feature BinaryWrite[ new copy of the html file , theEmbeddedData]. Embedding the data of theEmbeddeData in the notebook can be done with Iconize, or with a button that extracts the data of theEmbeddedData in a tempory directory and open the page.

That will be probably to very very usefull.

I mention this because it's hard to find the 4 main ingredients of the processus : CreateArchive, ExtractArchive, Import[...,"Byte"] and BinaryWrite[...].

$\endgroup$
1
  • $\begingroup$ Using Archiving to merge folders or do other folder operations seems unnatural but at least it works! I will wait to accept this as an answer so that anyone who may know a better way can have an opportunity too. $\endgroup$
    – user13892
    Mar 10 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.