Copy files without overwrite

WindowsBatch FileXcopyRobocopy

Windows Problem Overview


I just can't seem to find a way on the command line to say "copy all the files from directory A to directory B, but if the file already exists in directory B, don't overwrite it, no matter which file is newer, and don't prompt me."

I have been through copy, move, xcopy & robocopy, and the closest I can get is that you can tell robocopy "copy A to B, but don't overwrite newer files with older files," but that doesn't work for me. I looked at xxcopy, but discarded it, as I don't want to have a third-party dependency on a Visual Studio post-build event that will require other SVN users to have that tool installed in order to do the build.

I want to add a command line to the post-build event in Visual Studio 2010 so that the files that are generated from the T4 templates for new EF model objects get distributed to the project folders to which they belong, but regenerated files for existing objects don't overwrite potentially edited destination files.

Since the T4 template regenerates, the source file is always newer, and I can't use the "newer" switch reliably, I don't think.

I use partial classes for those items for which I can, but there are other things I generate that can't use partial classes (e.g. generating a default EditorTemplate or DisplayTemplate *.ascx file).

Any one have any similar problems they have solved?

Windows Solutions


Solution 1 - Windows

>Robocopy, or "Robust File Copy", is a command-line directory replication command. It has been available as part of the Windows Resource Kit starting with Windows NT 4.0, and was introduced as a standard feature of Windows Vista, Windows 7 and Windows Server 2008.

   robocopy c:\Sourcepath c:\Destpath /E /XC /XN /XO

To elaborate (using Hydrargyrum, HailGallaxar and Andy Schmidt answers):

  • /E makes Robocopy recursively copy subdirectories, including empty ones.
  • /XC excludes existing files with the same timestamp, but different file sizes. Robocopy normally overwrites those.
  • /XN excludes existing files newer than the copy in the destination directory. Robocopy normally overwrites those.
  • /XO excludes existing files older than the copy in the destination directory. Robocopy normally overwrites those.

With the Changed, Older, and Newer classes excluded, Robocopy does exactly what the original poster wants - without needing to load a scripting environment.

References: Technet, Wikipedia
Download from: Microsoft Download Link (Link last verified on Mar 30, 2016)

Solution 2 - Windows

You can try this:

echo n | copy /-y <SOURCE> <DESTINATION>

-y simply prompts before overwriting and we can pipe n to all those questions. So this would in essence just copy non-existing files. :)

Solution 3 - Windows

Belisarius' solution is good.

To elaborate on that slightly terse answer:

  • /E makes Robocopy recursively copy subdirectories, including empty ones.
  • /XC excludes existing files with the same timestamp, but different file sizes. Robocopy normally overwrites those.
  • /XN excludes existing files newer than the copy in the source directory. Robocopy normally overwrites those.
  • /XO excludes existing files older than the copy in the source directory. Robocopy normally overwrites those.

With the Changed, Older, and Newer classes excluded, Robocopy does exactly what the original poster wants - without needing to load a scripting environment.

Solution 4 - Windows

For %F In ("C:\From\*.*") Do If Not Exist "C:\To\%~nxF" Copy "%F" "C:\To\%~nxF"

Solution 5 - Windows

Here it is in batch file form:

@echo off
set source=%1
set dest=%2
for %%f in (%source%\*) do if not exist "%dest%\%%~nxf" copy "%%f" "%dest%\%%~nxf"

Solution 6 - Windows

There is an odd way to do this with xcopy:

echo nnnnnnnnnnn | xcopy /-y source target

Just include as many n's as files you're copying, and it will answer n to all of the overwrite questions.

Solution 7 - Windows

I just want to clarify something from my own testing.

@Hydrargyrum wrote:

  • /XN excludes existing files newer than the copy in the source directory. Robocopy normally overwrites those.
  • /XO excludes existing files older than the copy in the source directory. Robocopy normally overwrites those.

This is actually backwards. XN does "eXclude Newer" files but it excludes files that are newer than the copy in the destination directory. XO does "eXclude Older", but it excludes files that are older than the copy in the destination directory.

Of course do your own testing as always.

Solution 8 - Windows

robocopy src dst /MIR /XX

/XX : eXclude "eXtra" files and dirs (present in destination but not source). This will prevent any deletions from the destination. (this is the default)

Solution 9 - Windows

Robocopy can be downloaded here for systems where it is not installed already. (I.e. Windows Server 2003.)

http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=17657 (no reboot required for installation)

Remember to set your path to the robocopy exe. You do this by right clicking "my computer"> properties>advanced>"Environment Variables", then find the path system variable and add this to the end: ";C:\Program Files\Windows Resource Kits\Tools" or wherever you installed it. Make sure to leave the path variable strings that are already there and just append the addtional path.

once the path is set, you can run the command that belisarius suggests. It works great.

Solution 10 - Windows

It won't let me comment directly on the incorrect messages - but let me just warn everyone, that the definition of the /XN and /XO options are REVERSED compared to what has been posted in previous messages.

The Exclude Older/Newer files option is consistent with the information displayed in RoboCopy's logging: RoboCopy will iterate through the SOURCE and then report whether each file in the SOURCE is "OLDER" or "NEWER" than the file in the destination.

Consequently, /XO will exclude OLDER SOURCE files (which is intuitive), not "older than the source" as had been claimed here.

If you want to copy only new or changed source files, but avoid replacing more recent destination files, then /XO is the correct option to use.

Solution 11 - Windows

A simple approach would be to use the /MIR option, to mirror the two directories. Basically it will copy only the new files to destination. In next comand replace source and destination with the paths to your folders, the script will search for any file with any extensions.

robocopy <source directory> <destination directory> *.* /MIR

Solution 12 - Windows

This is what has worked for me. I use this to "add" files over to the other drive, with no overwrites.

Batch file: robocopy-missingfiles.bat

@echo off
echo Copying 
echo      "%1"
echo   to "%2"
echo.
echo Press Cntr+C to abort
Pause
echo.
@echo on
robocopy %1 %2 /Xo /XN /XC /J /SL /S /MT:8 /R:1 /W:1 /V /DCOPY:DAT /ETA /COPY:DATO /FFT /A-:SH /XD $RECYCLE.BIN "System Volume Information"

Example:

robocopy-missingfiles.bat f:\Working-folder\ E:\Backup-folder\

Do test before implementation.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTonyView Question on Stackoverflow
Solution 1 - WindowsDr. belisariusView Answer on Stackoverflow
Solution 2 - Windowssachin11View Answer on Stackoverflow
Solution 3 - WindowsHydrargyrumView Answer on Stackoverflow
Solution 4 - WindowsStuView Answer on Stackoverflow
Solution 5 - WindowsGabeView Answer on Stackoverflow
Solution 6 - WindowsAdam RuthView Answer on Stackoverflow
Solution 7 - WindowsHailGallaxarView Answer on Stackoverflow
Solution 8 - Windowsbe_good_do_goodView Answer on Stackoverflow
Solution 9 - WindowsGregView Answer on Stackoverflow
Solution 10 - WindowsAndy SchmidtView Answer on Stackoverflow
Solution 11 - WindowsIonut V.View Answer on Stackoverflow
Solution 12 - WindowsRameshView Answer on Stackoverflow