How to copy ONLY those files that are not in the destination?

32

12

Is it possible to use robocopy to copy only the files that do not exist in destination?

robocopy has a /is (include same files) switch. What I am looking for is an /xs switch. If a file exists in both the source and the destination I don't want it to be copied. The criteria is file name only, regardless of modified day, size, etc. For example, even if the source has a newer file (based on modified date) than the same file in the destination, I don't want it to be copied. Possible to do this? Thanks.

yky

Posted 2012-08-25T02:30:50.477

Reputation: 321

Answers

38

Just use the /xc (exclude changed) /xn (exclude newer) and /xo (exclude older) flags:

robocopy /xc /xn /xo source destination 

Harry Johnston

Posted 2012-08-25T02:30:50.477

Reputation: 5 054

4Use /FFT if you copy between different filesystems (NTFS, FAT). It should prevent Robocopy from thinking that a file is older/newer while it is actually the same date and time. – Martin – 2014-10-22T09:44:08.977

@HarryJohnston, I have a script that runs in a .bat file and it seems to copy the files but overwrites the file in the destination folder. What I want to know is how do I get this script to move the files and in the destination folder add a timestamp as part of the destination file name ? START /WAIT robocopy %EFM_EXPORT% %EFM_TEMP% *.CFX /S /NP /R:5 /W:5 /XX /XD *PR *PA *TA *DC *Bypass /MOV /LOG:%EFM_MOVE% – DJ KRAZE – 2016-09-01T16:52:17.813

@DJKRAZE: I don't think you can do that with robocopy; there's no provision for renaming the files as they are copied/moved. You could probably do it in batch language if you wanted (with the move command providing the underlying functionality) though personally I'd prefer almost anything else, Powershell or cscript perhaps if you don't want to use a compiled language. But if you have to use batch there are plenty of batch-language experts on Stack Overflow. – Harry Johnston – 2016-09-01T21:26:17.560

I think I will do this with PowerShell as a last resort but C# I can have this done in 5 mins.. thanks.. I was just trying to help someone – DJ KRAZE – 2016-09-01T21:57:09.487

5Worth adding the /s switch as well otherwise it won't enter directories if they already exist. – Tom Carpenter – 2017-07-16T18:03:06.800

@Tom, the OP didn't say whether he wanted a recursive copy or not. It isn't always desirable. – Harry Johnston – 2017-07-16T21:28:14.030

@HarryJohnston Sure, I was just noting it in the comments for anyone finding the answer later on as I did. – Tom Carpenter – 2017-07-16T22:12:32.050

I like to suggest: robocopy /xc /xn /xo /s /R:0 /W:0 source destination – Codebeat – 2019-11-01T02:04:37.273

@Codebeat, I recommend /R:0 only under special circumstances. Normally, if a network glitch causes a file copy to fail, you want it to be retried. – Harry Johnston – 2019-11-01T10:43:12.703

That does not work (at least if you have other options as well - i.e. /b /e /copyall). Robocopy looks at timestamp and I believe even if the last accessed time stamp is different, it will copy. If there is a way, would love to know about it. – None – 2012-11-21T02:49:26.273

The /xn and /xo options should prevent robocopy from copying because of a different timestamp. Worked fine when I tested it. Robocopy does not pay attention to the last accessed timestamp. Your answer will probably be removed (because it isn't an answer) but you can email me if you want to discuss this. – Harry Johnston – 2012-11-21T03:41:28.360

-1

If you're copying from left to right, you want to only copy orphaned files on the left to the right side.

I use a pay tool called Beyond Compare that handles these type of scenarios. RoboCopy is great for mapped drives or even UNC paths, but my two use cases involves:

  • FTP to copy new files to my web site
  • move/archive files to DropBox

That, I unfortunately can not do with RoboCopy.

Sun

Posted 2012-08-25T02:30:50.477

Reputation: 5 198