Solved

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

Posted on 2011-09-05
15
1,959 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
  • 5
  • 4
  • 2
  • +4
15 Comments
 
LVL 68

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
 
LVL 68

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 14

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 14

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 14

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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 68

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 14

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 14

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 68

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:
bchallis 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Synchronize a new Active Directory domain with an existing Office 365 tenant
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
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…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

910 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now