xcopy help

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
daviddc114
Posts: 19
Joined: 23 Jul 2013 12:45

xcopy help

#1 Post by daviddc114 » 23 Jul 2013 14:56

i want to copy files but keep duplicates. Heres what i have so far

xcopy /-y C:\Users\David\Desktop\batch\1\* /e C:\Users\David\Desktop\batch\2
pause

however i am trying to keep the duplicates and merge them instead, i dont care about renaming if i have to i just want to keep the duplicates

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

Re: xcopy help

#2 Post by penpen » 25 Jul 2013 13:08

I don't understand, what you want to do.
If you use the command lines:

Code: Select all

xcopy /-y C:\Users\David\Desktop\batch\1\* /e C:\Users\David\Desktop\batch\2
pause

then the originals are in C:\Users\David\Desktop\batch\1, and
the duplicates are in C:\Users\David\Desktop\batch\2.
So keeping the duplicates is just the wished behavior of xcopy.
And what do you want to merge... and what merge operation do you want to use?

penpen

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: xcopy help

#3 Post by foxidrive » 25 Jul 2013 21:36

This should copy all files in the source tree to the tree you defined, adding -new to the end of each filename if it already exists.

It relies on the fact that folder 1 and folder 2 are in the same subdirectory.


Code: Select all

@echo off
set "source=C:\Users\David\Desktop\batch\1"
set "target=C:\Users\David\Desktop\batch\2"
for /f "delims=" %%a in ('xcopy /l /e /y "%source%\*.*" "%target%\" ^|find ":"') do (
if exist "%%~dpa..\2\%%~na%%~xa" (
if not exist "%%~dpa..\2\%%~na-new%%~xa" (copy "%%a" "%%~dpa..\2\%%~na-new%%~xa") else (echo "%%~dpa..\2\%%~na-new%%~xa" already exists&pause)
) else (
copy "%%a" "%%~dpa..\2\%%~na%%~xa"
)
)
pause

brinda
Posts: 78
Joined: 25 Apr 2012 23:51

Re: xcopy help

#4 Post by brinda » 26 Jul 2013 07:46

foxidrive,

was wondering if there is a way the files could be renamed to their original on xcopy - capitalization.

source - FileA.txt
target - filea.txt

after xcopy , target becomes newFileA.txt.[added new - is using your existing script]. Same file name capitalization as in source and target which is FileA.txt

source - FileA.txt
target - newFileA.txt

Windows does not recognize capital or lower filename case if they are same .only the content inside are checked.

The only option i could think at the moment is to delete the file at destination and when the new files from source is copied the filename case would match.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: xcopy help

#5 Post by foxidrive » 26 Jul 2013 21:28

The new files that are copied to the target will have the capitalisation from the source files.
Any existing files in the target will remain as they are.

How do you want to change that?

Do you want the existing files in the target to be renamed to match the source? This extra line below in blue should do that:


if exist "%%~dpa..\2\%%~na%%~xa" (
ren "%%~dpa..\2\%%~na%%~xa" "%%~na%%~xa"

brinda
Posts: 78
Joined: 25 Apr 2012 23:51

Re: xcopy help

#6 Post by brinda » 27 Jul 2013 07:12

foxidrive,

thanks. it helps. :D Need to learn more on modifiers usage. thanks again

Post Reply