I'm sending commands to a remote computer in order to have it copy a file. I want the file to be copied, but not to overwrite the previous file with the same name (if it exists). I also need the command to run without any prompts (xcopy likes to prompt whether the target name I've specified is file or directory, and it will also prompt about overwriting a file).
7 Answers
I have good results with xcopy /d
.
It will copy NEWER files, and since we can assume that existing files have same time-stamp, you will copy only files that don't exist.

- 19,718
- 12
- 58
- 99
-
9I use xcopy source dest /d /y /s – Paul Gregoire Sep 30 '13 at 01:31
-
new thing I discovered recently, regarding xcopy /s /d /y ... while copying directory structure, it will CREATE and then DELETE destination folders if no files match the mask that is given. very strange, and sometimes very odd. – Daniel Mošmondor Dec 08 '14 at 21:22
-
I found the /D option actually copies IFF the file already exists in the destination folder and is OLDER than the same named file from source. So the file HAS to exist first. Is there a way to XCOPY if there is no file in the destination? – Fandango68 Aug 27 '17 at 23:10
just in case anyone else finds this: robocopy x:\sourcefolder Y:\destfolder /s /e /r:0 /z
much better than xcopy, even gives you a table at the end informing of any failed or skipped files. Doesn't prompt to not overwrite.

- 552
- 7
- 18
-
2you sir are a gentleman and a scholar, at first didn't think it "wasn't overwriting" but then I started getting "100% New File ....." – Captnwalker1 Jun 30 '15 at 11:10
-
For anyone else since the question was for one file, based on this answer:https://stackoverflow.com/questions/8935583/how-to-skip-existing-and-or-same-size-files-when-using-robocopy, the command could be robocopy x:\sourcefolder Y:\destfolder /XO. And to make the command (like XCopy) silent you can include the commands provided here https://stackoverflow.com/questions/3898127/how-can-i-make-robocopy-silent-in-the-command-line-except-for-progress – Declan Taylor Mar 25 '20 at 00:46
Well, there's a certain remedy! It has helped me with saving much of my effort and time on Win10 while writing a setup for our product demo.
Just try to use piping:
@ECHO N|COPY /-Y SourceFiles Destination
As an example I used this piece of code so that I would have a clean gentle quiet and safe copy!
@FOR /D %%F in ("FooPath") DO @(
@ECHO N|COPY /-Y ^"%%~npdxF\*.*^" ^"GooPath^" 3>NUL 2>NUL >NUL
)
where obviously FooPath is the source and GooPath is the destination.
Enjoy!
(main source: https://ss64.com/nt/copy.html)

- 223
- 1
- 3
- 9
No way to make it NOT overwrite as far as I know. but /Y will make it overwrite. and /I will get rid of the file/dict prompt. See xcopy /?
for all options

- 4,255
- 26
- 46
-
Thanks.. Eventually I had to make it in two stages - first check for existence and then act accordingly. – Vic Mar 02 '11 at 13:59
You can also use the replace command. It has two modes: to add files that don't exist there or replace files that do exist. You want the previous mode:
replace <path1> <path2> /A

- 41
- 1
-
3An OK option, however you cannot use this with /S to include sub-directories. – Merkidemis Jun 19 '13 at 20:36
-
1I really like this option, but be warned that it fails with "Path not found" if the destination folder does not exist, and there does not appear to be an option to force creation of the destination folder if it is not present. – Jay Michaud Mar 24 '16 at 13:56
I had to copy AND rename files, so I got the prompt about creating a file or a directory.
This is the, rather "hackish" way I did it:
ECHO F | XCOPY /D "C:\install\dummy\dummy.pdf" "C:\Archive\fffc810e-f01a-47e8-a000-5903fc56f0ec.pdf"
XCOPY will use the "F" to indicate it should create the target as a file:
C:\install>ECHO F | XCOPY /D "C:\install\dummy\dummy.html" "C:\Archive\aa77cd6e-1d19-4eb4-b2a8-3f8fe60daf00.html"
Does C:\Archive\aa77cd6e-1d19-4eb4-b2a8-3f8fe60daf00.html specify a file name or directory name on the target
(F = file, D = directory)? F
C:\install\dummy\dummy.html
1 File(s) copied
I've also verified this command leaves existing files alone. (You should too :-)

- 1,159
- 15
- 9