104

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?

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Tony
  • 1,121
  • 2
  • 10
  • 6
  • Are you familiar with `cmd.exe`'s `for` command? If not, I recommend you run `for /?` and use that. – Gabe Nov 19 '10 at 19:54

12 Answers12

218

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)

msb
  • 2,689
  • 1
  • 26
  • 36
Dr. belisarius
  • 59,172
  • 13
  • 109
  • 187
  • By 'exclude' do you mean 'skip' or 'delete/remove'? – airstrike Oct 14 '13 at 17:37
  • @Andre Terra: Skip, normally. If you're using the /MIR flag, it might remove them. I haven't tested that. – Hydrargyrum Dec 04 '13 at 23:33
  • With /MIR, files that don't exist in the source are indeed removed. But when also using /XN and /XO, files that exist in both places are simply skipped (not sure whether size is checked). This is ideal for my situation where I need to copy either a newer or older version of my application but where the dateModified stamps of the libraries the application uses are incorrect yet the file names themselves contain their version number. – Henno Vermeulen Aug 05 '15 at 11:18
  • 1
    Is it necessary to include `/e`? What if you use `/s` instead? I don't want a bunch of empty sub-folders. – Arete Feb 04 '20 at 20:03
  • /S instead of /E is totally fine. It does as it lists in the documentation (don't include emtpy subdirs). – ricekab Feb 24 '21 at 10:56
48

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.

Hydrargyrum
  • 2,752
  • 2
  • 23
  • 35
47

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. :)

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
sachin11
  • 967
  • 3
  • 11
  • 16
  • 2
    Beautifully simple. – Holf Nov 01 '16 at 14:25
  • 6
    why does it answer all the questions when we only pipe once? – phuclv May 27 '17 at 11:48
  • Awesome, I went with this option. Before thinking of piping, I googled for a parameter I thought I was missing and then I came across this page. Trying this solution I noticed that when piping, copy apparently enables the /Y parameter, so that's why /-Y must be added. – Andrew Jul 07 '17 at 20:46
  • 2
    @phuclv It seems `copy /-y` just automatically assumes No once stdin runs out of input. You can omit the first explicit `n`, or pipe `< nul` into it, and the same thing happens. – mwfearnley Nov 08 '19 at 10:03
40
For %F In ("C:\From\*.*") Do If Not Exist "C:\To\%~nxF" Copy "%F" "C:\To\%~nxF"
Stu
  • 14,881
  • 4
  • 37
  • 73
  • 5
    This worked for me, but I had to use two percent symbols instead of one as I was running this from inside a batch file rather than the command line. I replaced instances of %F with %%F and instances of %~nxF with %%~nxF. – Owen Jun 17 '16 at 07:37
  • 2
    What does `~nx` in `%~nxF` exactly mean? – Serge Rogatch Aug 27 '16 at 17:21
  • @stu, it only says that for `~nxF` the variable `F` turns into a file name and an extension. – Serge Rogatch Aug 29 '16 at 13:07
  • Yes, so effectively, give me the filename+extension, without the path -- which is what you need to do the test in the target folder and specify the target filename. The point is that %F includes the path, and you don't want it there. – Stu Aug 30 '16 at 13:27
6

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"
Gabe
  • 79,868
  • 10
  • 131
  • 226
4

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
  • 2
    Regarding `copy`, see [this answer](https://stackoverflow.com/a/23812179/158074), specifically @mwfearnley comment: `copy /-y` automatically assumes `n` once stdin is exhausted, so you can e.g. `echo n` only once and it still works. – rsenna Nov 14 '19 at 12:07
3

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.

ASGM
  • 9,243
  • 25
  • 46
  • 2
    Is this an answer to the original question, or a comment on someone else's answer? – ASGM Apr 16 '13 at 13:06
  • 1
    Is Hydrargyrum's answer that has +25 votes as of time of writing an answer to the original question, or a comment on someone else's answer? – mo. May 09 '13 at 22:28
3
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)

be_good_do_good
  • 3,615
  • 3
  • 25
  • 37
1

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.

Greg
  • 733
  • 6
  • 8
1

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.

0

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
Ionut V.
  • 84
  • 6
0

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.

Ramesh
  • 1