Solved

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

Posted on 2011-09-05
15
2,012 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 69

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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 69

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 69

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 69

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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

Suggested Solutions

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This article will help you understand what HashTables are and how to use them in PowerShell.
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)
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

837 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