Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch file to run xcopy without overwriting existing files

I need my program to run:

xcopy s:\* z:\ /E

When xcopy runs, it will prompt if a file needs to be overwritten, so I want the batch file to answer no in all cases to the prompt.

How can I accomplish this?

like image 300
Eae Avatar asked Jul 11 '13 07:07

Eae


People also ask

Does xcopy replace existing files?

Copies source files changed on or after the specified date only. If you do not include a MM-DD-YYYY value, xcopy copies all source files that are newer than existing destination files. This command-line option allows you to update files that have changed.

How do I copy files without replacing existing files?

If you are copying files using drag-drop or copy/paste, you may simply choose “Skip this file” or “Skip these files” option to not overwrite the files that are already existed at the destination folder. Or, if you are using command line copy, you can answer N to bypass these files that are already existed.

Does batch copy overwrite?

If the copy command is run from within a batch job you do not need to use the /Y switch: it will overwrite existing files.

How do you copy and not replace?

All you have to do is hold down the Shift key while you click on no. It has the same effect as saying No To All which means that the copy process from that moment on will automatically select no if a duplicate file is found in the destination directory.


2 Answers

I almost overlooked this in the switches myself. I was aided by an Experts Exchange article.

Here is the switch of significance:

/D:m-d-y Copies files changed on or after the specified date. If no date is given, copies only those files whose source time is newer than the destination time.

The "If no date is given" portion is of particular importance. This doesn't exactly answer the "without the overwriting existing files" question, but it does answer it if the existing file's time stamp of the source file is not newer than the destination file.

Close enough for government work.

like image 184
Jeff Fischer Avatar answered Oct 16 '22 14:10

Jeff Fischer


I've been using the following flags for years. It seems to accomplish all the behaviors you describe in the question.

xcopy s:\ z:\ /S /Y /D

S will copy all subfolders and files within them Y prevents any overwrite prompts D essentially copies anything newer than what is already in the destination

like image 30
y-i_guy Avatar answered Oct 16 '22 12:10

y-i_guy