Eae
Eae

Reputation: 4321

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?

Upvotes: 38

Views: 110579

Answers (7)

Zibri
Zibri

Reputation: 9857

(FOR /f %M IN ('dir /s s:\') DO @echo n)|xcopy s:\ z:\ /S /Y /D

this will answer N for every file...

Upvotes: 0

user6366151
user6366151

Reputation:

Windows Command Interpreter:

for /f "delims=" %i in ('dir /b /s "source"') do (echo n | xcopy "%i" "destination"  [switches] /-y)

Windows Batch:

for /f "delims=" %%i in ('dir /b /s "source"') do (echo n | xcopy "%%i" "destination"  [switches] /-y)

Please remember to make sure it prompts you before starting to copy/overwrite any files.

Upvotes: 0

y-i_guy
y-i_guy

Reputation: 783

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

Upvotes: 48

Aditya Agarwal
Aditya Agarwal

Reputation: 513

This works for me:

echo n|xcopy s:\* z:\ /E    

Upvotes: 3

hamacker
hamacker

Reputation: 93

To avoid question y/n/all you can simulate "type in" using echo command as: echo n|xcopy blabla... But it´s not correct because any different question will be answer "n" too.

Better choice in cmd to replace xcopy is: robocopy source: destination: /e [/zb]

In graphical mode, there is a app called "Teracopy" (use google to find it) that override windows explorer copy with several options (pause, ignore[all], overwrite[all], minimize,...) that I would like to recommend.

Upvotes: 1

Jeff Fischer
Jeff Fischer

Reputation: 2148

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.

Upvotes: 35

Martin Binder
Martin Binder

Reputation: 1096

xcopy cannot be configured to SKIP existing files, so you should copy using "robocopy".

Upvotes: 9

Related Questions