?
Solved

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

Posted on 2011-09-05
15
Medium Priority
?
2,172 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 988 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 988 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 988 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 612 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 612 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 400 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
The viewer will learn how to count occurrences of each item in an array.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

777 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