Solved

Windows batch script : xcopy / copy without overwriting existing destination files

Posted on 2011-09-05
15
2,072 Views
Last Modified: 2013-05-02

I would like to do
"xcopy/e/c/h drive1:\dir1  drive2:\dir2 " 
without overwriting destination files (if the destination files are present / existing )

Would the following Windows batch script works or kindly provide any other way
as I don't see any xcopy option that will serve this purpose:

myscript.bat
==========
xcopy/e/c/h drive1:\dir1  drive2:\dir2
n
n
n
...
...
endless number of n  (for No)
...
...

but the above is not feasible if there are hundreds of thousands of
existing destination files
0
Comment
Question by:sunhux
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +4
15 Comments
 
LVL 70

Expert Comment

by:Qlemo
ID: 36484784
You can use the /d switch with XCOPY, which will overwrite only files if they are older then the source.
0
 
LVL 5

Expert Comment

by:truinx
ID: 36484847
add the option /I
0
 
LVL 5

Expert Comment

by:truinx
ID: 36484852
Take note that the destination should be a directory.
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 70

Expert Comment

by:Qlemo
ID: 36484869
truinx,
/i is for skipping the confirmation whether the target is a folder or a file - not appliable here.
0
 
LVL 15

Assisted Solution

by:Giovanni Heward
Giovanni Heward earned 247 total points
ID: 36484984
Sounds like this is what you're looking for...

Remember to modify the drive1:\dir1 (two locations) and drive2:\dir2 (one location) prior to running the script...

 
@echo off
setlocal enabledelayedexpansion
for /f "delims=?" %%f in ('dir "drive1:\dir1\*.*" /s/a-d/b') do (
	set file=%%f
	set dest=!file:drive1:\dir1=drive2:\dir2!
	if not exist "!dest!" (
		md "!dest!"
		rd "!dest!"
		set /p p=Copying "!file!" to "!dest!"... <nul
		copy /v "!file!" "!dest!">nul 2>&1||echo FAILED!
		if exist "!dest!" echo OK.
	)
)

Open in new window

0
 
LVL 15

Assisted Solution

by:Giovanni Heward
Giovanni Heward earned 247 total points
ID: 36484998
Of course you may incorporate XCOPY in lieu of COPY to perform enhanced copy functions...  (copy hidden and system files, file ownership and ACL information, file audit settings, etc)
0
 
LVL 15

Assisted Solution

by:Giovanni Heward
Giovanni Heward earned 247 total points
ID: 36485102
This version you can call from a batch file and use directory from the command line...

batch file syntax: call script.bat "drive1:\dir1" "drive2:\dir2"

command line syntax: script.bat "drive1:\dir1" "drive2:\dir2"

@echo off
setlocal enabledelayedexpansion
if [%1]==[] (
	echo useage: %0 "drive1:\dir1" "drive2:\dir2"
	pause
	goto :eof
)
if exist "%~1"	(
	for /f "delims=?" %%f in ('dir "%~1\*.*"/s/a-d/b') do (
		set file=%%f
		set dest=!file:%~1=%~2!
		if not exist "!dest!" (
			md "!dest!"
			rd "!dest!"
			set /p p=Copying "!file!" to "!dest!"... <nul
			copy /v "!file!" "!dest!">nul 2>&1||echo FAILED!
			if exist "!dest!" echo success.
		)
	)
) else (
	echo %1 source location not found, aborting...
	pause
)

Open in new window

0
 
LVL 70

Assisted Solution

by:Qlemo
Qlemo earned 153 total points
ID: 36485164
x66_x72_x65_x65,
What is that "md, rd" sequence for?

If using something along explicit checking, I would instead use a exclusion file list:
@echo off
setlocal EnableDelayedExpansion
set src=Drive1:\dir1
set dst=Drive2:\dir2

del %tmp%\file.$$$ 2>nul
(for /F "tokens=*" %%F in ('dir /b/s %src%') do (
   set file=%%F
   echo !file:%dst%=%src%!
)) > %tmp%\file.$$$
xcopy /s/e/c/h "%src%" "%dst%" /exclude:%tmp%\file.$$$
del %tmp%\file.$$$ 2>nul

Open in new window

0
 
LVL 15

Expert Comment

by:Giovanni Heward
ID: 36485187
@Qlemo: The copy command will fail if the underlying directory structure doesn't exist-- those commands create it...

For example:  

md X:\1\2\3\4\5\6\7\8\9\10\file.ext
rd X:\1\2\3\4\5\6\7\8\9\10\file.ext
Leaves X:\1\2\3\4\5\6\7\8\9\10 directory structure intact, so command:

copy X:\1\2\3\4\5\6\7\8\9\10\file.ext Y:\1\2\3\4\5\6\7\8\9\10\file.ext succeeds whereas it would fail ("The system cannot find the path specified.") if any sub-folders didn't exist...

0
 
LVL 15

Expert Comment

by:Giovanni Heward
ID: 36485201
Of course a simple way to avoid overwriting files would be...
echo n|copy "x:\folder\file.ext" "y:\folder\file.ext" /v/-y

Open in new window



Though not necessarily practical with 1000's of files...
0
 
LVL 70

Assisted Solution

by:Qlemo
Qlemo earned 153 total points
ID: 36485214
Tricky, but not necessary if we stick with xcopy.

BTW, is using RoboCopy an option? It has switches like /XC to exclude files (/XC = "exclude changed", that is existing, but different).
0
 
LVL 8

Accepted Solution

by:
Brent Challis earned 100 total points
ID: 36489141
I am currently using a batch file to back up files while I am travelling which copies through sub directory structures and only copies newer or non-existent files..  I am not proplted, but here is a pause statement when finished so I can review:

@echo off
@echo Backing up files for Europe 2011 trip

@echo Backing up EUR2011 Directory
xcopy D:\EUR2011\*.* F:\EUR11Backup\EUR2011 /S /E /D /Y

@echo Backing up Europe2011 Directory
xcopy D:\Europe2011\*.* F:\EUR11Backup\Europe2011 /S /E /D /Y

@echo Backing up Photos Directory
xcopy D:\Photos\*.* F:\EUR11Backup\Photos /S /E /D /Y

@echo Done.
pause
0
 
LVL 2

Expert Comment

by:nat_nz
ID: 36501038
cmd=xcopy /s /c /d /e /h /i /r /y
0
 

Author Closing Comment

by:sunhux
ID: 36585327
ok
0
 

Expert Comment

by:robboh1
ID: 39134461
echo n | xcopy /s /c /e /h /i /r
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A procedure for exporting installed hotfix details of remote computers using powershell
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question