8
$\begingroup$

CopyFile return $Failed when the file exists on destination.

is there any way to force CopyFile to overwrite the existing one?

the way I use now is DeleteFile and then CopyFile

$\endgroup$
  • 2
    $\begingroup$ From the documentation for CopyFile["file1","file2"]: "file1 must already exist; file2 must not." $\endgroup$ – Karsten 7. Oct 18 '14 at 4:20
  • $\begingroup$ So this is a documentation bug? $\endgroup$ – Karsten 7. Oct 18 '14 at 10:11
  • $\begingroup$ i don't think an undocumented option should be considered a "bug". There may be legitimate reasons, especially for a system related function it may not be robustly tested for all system combinations. $\endgroup$ – george2079 Oct 18 '14 at 13:24
10
$\begingroup$

Use the OverwriteTarget -> True option. Alas, this is not specified in the documentation.

?? CopyFile

gives, among other details,

Options[CopyFile]={OverwriteTarget->False}

CopyFile[ "foo1.txt", "foo2.txt", OverwriteTarget -> True ]

does the trick for me.

$\endgroup$
  • $\begingroup$ (+1) very very nice. it is great to see you answer one on my equations again :) $\endgroup$ – Algohi Oct 18 '14 at 4:57
  • 5
    $\begingroup$ by the way, this option is not available in older version of MM. it is only in V10. $\endgroup$ – Algohi Oct 18 '14 at 4:59
  • $\begingroup$ Be careful using this option with RenameFile[], for me it resulted in loss of data. I wanted to do a batch rename changing the file extension to lower case. It worked for some files but inexplicably got $Failed for others. Maybe its the bug mentioned here? : mathematica.stackexchange.com/questions/60990/…. When I added the OverwriteTarget->True option it deleted the source file and did not rename it. I confirmed the file was deleted by exhaustive search on all hard drives with a programmer's tool and then recovered the file with Recuva. $\endgroup$ – user6552 Jul 5 '16 at 2:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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