Page 1 of 1
Copy files do not overwrite existing
Posted: 30 May 2009 15:49
by zombie_funk
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:
What would I need to do to make sure that it doesn't overwrite files that already exist?
Thanks
Posted: 30 May 2009 23:27
by RElliott63
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
Posted: 31 May 2009 13:04
by zombie_funk
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?
Posted: 31 May 2009 15:41
by RElliott63
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
Posted: 01 Jun 2009 11:35
by avery_larry
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.
Re: Copy files do not overwrite existing
Posted: 13 Dec 2010 10:38
by ChickenSoup
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\*
Re: Copy files do not overwrite existing
Posted: 24 May 2020 17:22
by Samir
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.
Re: Copy files do not overwrite existing
Posted: 25 May 2020 01:31
by penpen
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
Re: Copy files do not overwrite existing
Posted: 25 May 2020 22:21
by Samir
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.
Re: Copy files do not overwrite existing
Posted: 27 May 2020 01:43
by penpen
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
Re: Copy files do not overwrite existing
Posted: 30 May 2020 02:38
by Samir
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.
Re: Copy files do not overwrite existing
Posted: 01 Jun 2020 10:29
by pieh-ejdsch
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
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