Copy files without overwriting

I just can’t 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, do not overwrite it, no matter which file is newer, and don’t ask me.”

I went through copying, moving, xcopy and robocopy, and the closest I can get is that you can tell robocopy “copy A to B, but not overwrite older files with older files”, but that doesn't work for me. I looked at xxcopy, but canceled it because I don’t want to have third-party dependencies on the Visual Studio post-build event, for which other SVN users will need to install this build tool.

I want to add a command line to the post-build event in Visual Studio 2010 so that files created from T4 templates for new EF objects are distributed across the project folders to which they belong, but the regenerated files for existing objects do not overwrite the potentially editable destination files.

Since the T4 template is being restored, the original file is always newer, and I cannot reliably use the “new” switch, I don’t think.

I use partial classes for those elements for which I can, but there are other things that I generate that cannot use partial classes (for example, creating a default EditorTemplate file or DisplayTemplate * .ascx).

Does anyone have similar problems that they solved?

+71
windows batch-file xcopy robocopy
Nov 19 '10 at 19:50
source share
12 answers
For %F In ("C:\From\*.*") Do If Not Exist "C:\To\%~nxF" Copy "%F" "C:\To\%~nxF" 
+27
Nov 19 '10 at 20:04
source share

Robocopy or "Robust File Copy" is a command-line replication command. It was 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 

Develop (using answers from Hydrargyrum, HailGallaxar and Andy Schmidt):

  • /E allows Robocopy to recursively copy subdirectories, including empty ones.
  • /XC excludes existing files with the same timestamp but a different file size. Robocopy usually rewrites them.
  • /XN excludes existing newer files than the copy in the destination directory. Robocopy usually rewrites them.
  • /XO excludes existing files older than the copy in the destination directory. Robocopy usually rewrites them.

If the Changed, Older, and Newer classes are excluded, Robocopy does exactly what the original poster wants - without having to load the scripting environment.

Links: Technet , Wikipedia
Download from: Download link for Microsoft (Link last tested March 30, 2016)

+150
Nov 19 '10 at 20:05
source share

Belisarius's decision is good.

To clarify this slightly brief answer:

  • /E allows Robocopy to recursively copy subdirectories, including empty ones.
  • /XC excludes existing files with the same timestamp but a different file size. Robocopy usually rewrites them.
  • /XN excludes existing files than a copy in the source directory. Robocopy usually rewrites them.
  • /XO excludes existing files older than the copy in the source directory. Robocopy usually rewrites them.

If the Changed, Older, and Newer classes are excluded, Robocopy does exactly what the original poster wants - without having to load the scripting environment.

+40
Dec 6 '10 at 8:17
source share

You can try the following:

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

-y just asks before overwriting, and we can pass n to all of these questions. That way, it essentially just copies non-existent files. :)

+30
May 22 '14 at 16:14
source share

Here it is in the form of a batch file:

 @echo off set source=%1 set dest=%2 for %%f in (%source%\*) do if not exist "%dest%\%%~nxf" copy "%%f" "%dest%\%%~nxf" 
+5
Nov 19 '10 at 20:06
source share

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 usually rewrites them.
  • / XO excludes existing files older than the copy in the source directory. Robocopy usually rewrites them.

This is the opposite. XN makes the "eXclude Newer" files, but excludes files that are newer than the copy in the destination directory. XO makes an "eXclude Older", but excludes files that are older than the copy in the destination directory.

Of course, do your own testing, as always.

+2
Apr 16 '13 at 12:40
source share

There is an odd way to do this with xcopy:

 echo nnnnnnnnnnn | xcopy /-y source target 

Just include as many n as the files you are copying, and answer n to all rewritable questions.

+2
Mar 23 '15 at 18:19
source share
 robocopy src dst /MIR /XX 

/ XX: eXclude "eXtra" files and directories (present at the destination, but not the source). This will prevent deletion from the destination. (this is the default value)

+2
Feb 12 '17 at 16:13
source share

Robocopy can be downloaded here for systems where it is no longer installed. (Ie Windows Server 2003.)

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

Do not forget to indicate your path to robocopy exe. You do this by right-clicking "My Computer"> "Properties"> "Advanced"> "Environment Variables", then find the system path variable and add this to the end: "; C: \ Program Files \ Windows Resource Kits \ Tools "or installed it anywhere. Remember to leave the lines of the path variable that already exist, and simply add an additional path.

Once the path is set, you can run the command suggested by it. It works great.

+1
Apr 09 '12 at 15:37
source share

This will not allow me to comment directly on incorrect messages, but let me just warn everyone that the definition of the / XN and / XO REVERSED options is compared to what was published in previous posts.

The Exclude Older / Newer parameter is compatible with the information displayed in the RoboCopy protocol: RoboCopy will iterate through SOURCE and then report whether each file in SOURCE is "OLDER" or "NEWER" than the file in the destination directory.

Therefore, / XO will exclude OLDER SOURCE files (which is intuitive), rather than being "older than the source," as stated here.

If you want to copy only new or changed source files, but avoid replacing later destination files, then / XO is the right option.

+1
May 08 '16 at 16:49
source share

A simple approach would be to use the / MIR switch to mirror two directories. Basically, it only copies the new files to the destination. In the following list, replace the source and destination on the path to your folders, the script will search for any file with any extensions.

 robocopy <source directory> <destination directory> *.* /MIR 
0
Jun 17 '16 at 10:25
source share

This is what worked for me. I use this to “add” files to another drive without overwriting.

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\ 

Check before implementation.

0
Dec 14 '16 at 3:07
source share



All Articles