Solved

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

Posted on 2011-09-05
15
1,934 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

705 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

20 Experts available now in Live!

Get 1:1 Help Now