Keeping files in sync

Copy and sync

Copy operation needs two things: source and destination. The situation is simple when the destination is empty. But what if it is not? In all cases, we want to use the fact that some data already exists in the destination, and we do not want to copy them again. There are two main scenarios:

And here is how to do it.

Synchronize directories in Windows

Copying files is my hobby and I once found a great tool: robocopy.

It can handle both copy scenarios. I use it with PowerShell, and I recommend using $PSScriptRoot variable.

Mirror entire directory:

robocopy "K:\work" "J:\work" /e /mir

There’s a catch when mirroring entire disks: Windows is funny when it comes to the Recycle Bin directory, so exclude it:

robocopy "K:" "J:" /xd "K:\`$RECYCLE.BIN" /e /mir

Copy directory to where the script is located (do not remove anything):

robocopy "D:\work" "$PSScriptRoot\work" /e

Remember that if the script you are running like this is located in the destination directory, it may be removed when mirroring, if it is not in the source directory.

Rsync examples coming soon.