2
$\begingroup$

I have the following folder structure

Main
+updates
 +newversion
  +subfolder1
+subfolder1

I would like to copy the content of the subfolder "newversion" to Main, overwriting the content where necessary, and preserving folder structure/relative paths, so for example all files in Main/updates/newversion/subfolder1 will go to main/subfolder1. Is there a simple mathematica command to do this, or do I have to do an OS-dependent approach using execute and command line commands?

$\endgroup$
2
  • $\begingroup$ CopyDirectory["path/Main/updates/newversion", "path/Main"] $\endgroup$ May 23, 2021 at 7:59
  • $\begingroup$ maybe I was not clear, I do not want to copy the folder newversion, only the files and folders inside it, and they have to go right into main. So I think I would need to rename newversion to main first, and then copy it with CopyDirectory["path/Main/updates/Main", "path/Main"]...but I see no option to overwrite content with copydirectory... is it possible at all? $\endgroup$ May 24, 2021 at 4:10

1 Answer 1

1
$\begingroup$

You must first read the file names, then change the names to new names and finally do the copy:

names = FileNames[All, "path/Main/updates/newversion", Infinity];

CopyFile[#, 
FileNameJoin[{"path/Main", StringDelete[#, "path/Main"]}]
] & /@ names
$\endgroup$
2
  • $\begingroup$ this will include also folders in the list of names, probably giving errors with copyfile? Should I rather use somthing like Filenames[.,....] to filter folders? $\endgroup$ May 24, 2021 at 10:17
  • 1
    $\begingroup$ The result of the command is a list of filenames, with an entry "Failed" where a directory name was given. Maybe that is not 100% clean, but otherwise there are no negative effects. $\endgroup$ May 24, 2021 at 10:28

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.