Copy files do not overwrite existing

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
zombie_funk
Posts: 2
Joined: 30 May 2009 15:41

Copy files do not overwrite existing

#1 Post by zombie_funk » 30 May 2009 15:49

Hi there,
In the spirit of DIY I would like to create a batch file to backup my music library, however I would like to it to only add new files to the backup.

something like:

Code: Select all

COPY E:\*.mp3 F:\music\

What would I need to do to make sure that it doesn't overwrite files that already exist?

Thanks

RElliott63
Expert
Posts: 80
Joined: 04 Feb 2009 10:03

#2 Post by RElliott63 » 30 May 2009 23:27

Use the copy /-Y option

Something like:

COPY /-Y E:\*.mp3 F:\music\

This will prompt you to overwrite or not. That way, you can copy them all, or overwrite as it's copying. But, it won't overwrite unless you tell it to.

-Rick

zombie_funk
Posts: 2
Joined: 30 May 2009 15:41

#3 Post by zombie_funk » 31 May 2009 13:04

Thanks Rick,
That worked great for initially copying my files over but when I tried my first "Backup" it prompted me to overwrite each file individually.

Code: Select all

COPY /-Y E:\*.mp3 F:\music\
E:\01 Limehouse Blues.mp3
Overwrite F:\MP3Mass\01 Limehouse Blues.mp3? (Yes/No/All):


I would like the script to only copy new files over (files that do not exist in the backup folder, to save time)

Perhaps I need to send the output to a loop that returns "no"?
Or maybe I should compare the files in the two locations first, then only copy new files over with a more elaborate script?

RElliott63
Expert
Posts: 80
Joined: 04 Feb 2009 10:03

#4 Post by RElliott63 » 31 May 2009 15:41

OK.. try this then:

XCopy /-U E:\*.mp3 F:\music\

The /U option will only copy files that already exist. The /-U should copy only those that do not already exist.

-Rick

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#5 Post by avery_larry » 01 Jun 2009 11:35

If you want to take the time to learn it, robocopy is an excellent tool designed just for this type of thing.

From the resource toolkit.

ChickenSoup
Posts: 79
Joined: 13 Dec 2010 10:32

Re: Copy files do not overwrite existing

#6 Post by ChickenSoup » 13 Dec 2010 10:38

Old topic, but I thought I would post.

pipe an 'n' to each question on the overwrite. the /-y is required in batch files or it assumes /y.

Code: Select all

echo n|copy /-y c:\source\* c:\destination\*

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Copy files do not overwrite existing

#7 Post by Samir » 24 May 2020 17:22

ChickenSoup wrote:
13 Dec 2010 10:38
Old topic, but I thought I would post.

pipe an 'n' to each question on the overwrite. the /-y is required in batch files or it assumes /y.

Code: Select all

echo n|copy /-y c:\source\* c:\destination\*
This works great for copy, but does not for xcopy. :( Anyone have a solution for xcopy? I have a xcopy /d that keeps thinking that all the files are new.

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Copy files do not overwrite existing

#8 Post by penpen » 25 May 2020 01:31

That command works on my (actual) win10, but i noticed another nitpick, if the target does not exist xcopy asks if it should create a directory or file (so i added a 'd'); also it is localized (which might cause isues):

Code: Select all

Z:\>>>"test.hta" echo(

Z:\>echo dn|xcopy /-y /d test.hta test2.hta
Ist das Ziel test2.hta ein Dateiname
oder ein Verzeichnisname
(D = Datei, V = Verzeichnis)? d
Z:test.hta
1 Datei(en) kopiert

Z:\>echo dn|xcopy /-y /d test.hta test2.hta
0 Datei(en) kopiert

Z:\>>>"test.hta" echo(

Z:\>echo dn|xcopy /-y /d test.hta test2.hta
Z:\test2.hta überschreiben (Ja/Nein/Alle)? d
Z:\test2.hta überschreiben (Ja/Nein/Alle)? n
0 Datei(en) kopiert
penpen

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Copy files do not overwrite existing

#9 Post by Samir » 25 May 2020 22:21

Thank you penpen. :) Did you try a solution with wildcards and see if the echo n works? That's where it seemed to break down for me.

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Copy files do not overwrite existing

#10 Post by penpen » 27 May 2020 01:43

Using wildcards also seems to work fine:

Code: Select all

Z:\>del *.hta *.tmp

Z:\>>>"test.hta" echo(

Z:\>>>"test2.hta" echo(

Z:\>>>"test3.hta" echo(

Z:\>echo dndndn|xcopy /-y /d *.hta *.tmp
Z:test.hta
Z:test2.hta
Z:test3.hta
3 Datei(en) kopiert

Z:\>echo dndndn|xcopy /-y /d *.hta *.tmp
0 Datei(en) kopiert

Z:\>>>"test.hta" echo(

Z:\>>>"test2.hta" echo(

Z:\>echo dndndn|xcopy /-y /d *.hta *.tmp
Z:\test.tmp überschreiben (Ja/Nein/Alle)? d
Z:\test.tmp überschreiben (Ja/Nein/Alle)? n
Z:\test2.tmp überschreiben (Ja/Nein/Alle)? d
Z:\test2.tmp überschreiben (Ja/Nein/Alle)? n
0 Datei(en) kopiert

Code: Select all

Z:\>del *.hta *.tmp

Z:\>>>"test.hta" echo(

Z:\>>>"test2.hta" echo(

Z:\>>>"test3.hta" echo(

Z:\>echo dndndn|xcopy /-y /d test?.hta *.tmp
Z:test.hta
Z:test2.hta
Z:test3.hta
3 Datei(en) kopiert

Z:\>echo dndndn|xcopy /-y /d test?.hta *.tmp
0 Datei(en) kopiert

Z:\>>>"test.hta" echo(

Z:\>>>"test2.hta" echo(

Z:\>echo dndndn|xcopy /-y /d test?.hta *.tmp
Z:\test.tmp überschreiben (Ja/Nein/Alle)? d
Z:\test.tmp überschreiben (Ja/Nein/Alle)? n
Z:\test2.tmp überschreiben (Ja/Nein/Alle)? d
Z:\test2.tmp überschreiben (Ja/Nein/Alle)? n
0 Datei(en) kopiert
Could you give an example where it fails for you?

penpen

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Copy files do not overwrite existing

#11 Post by Samir » 30 May 2020 02:38

I forgot exactly what I used, but will post it once I remember again. I'll also get the os version as I think that's a factor. 8)

pieh-ejdsch
Posts: 239
Joined: 04 Mar 2014 11:14
Location: germany

Re: Copy files do not overwrite existing

#12 Post by pieh-ejdsch » 01 Jun 2020 10:29

First you create a list of the files to be excluded.
Only those that are in the source and the destination are listed. So the command backwards

Code: Select all

xcopy /LUY target source
Just a bit with showing the source files.
You add this to your correct command as an exclusion list.

Code: Select all

@echo off
setlocal
set prompt=$G$S
set        "From=D:\A_test"
set      "copyTo=D:\0\*.*"
set "excludeList=excludeList"

>"%excludeList%" (
 for /f "delims=> tokens=2" %%i in ('xcopy /luyf "%copyTo%" "%from%"') do @for /f "tokens=*" %%i in ("%%i") do @echo %%i
)
@echo on
xcopy /EXCLUDE:%excludeList% /L "%from%" "%copyTo%" 
pause
Phil

Post Reply