1 minute read

I decided to update some scripts that relied on xcopy to use robocopy instead. Xcopy and robocopy are both command-line utilities that are regularly used to move around files. The description of the robocopy command is simply:

Copies file data.

robocopy stands for Robust File Copy and is designed to be reliable and supports mirroring. The use-case I had at hand is to mirror folders with subfolders. This was previously done by using xcopy. However since xcopy isn’t designed to mirror folders like robocopy there were some inconcistencies and that’s the reason for this switch.

The xcopy command I’m about to replace is

xcopy /e /d /y <source> <dest>

The switches used here basically says to copy every folder and subfolder and all files that are newer than existing files in the destination. The /y switch suppresses a confirm-prompt when overwriting a destination file.

Going to robocopy I’m going to go for this command:

robocopy <source> <dest> /e /purge /r:5 /mt:32

/e and /purge together is almost the same as the /mir-switch that mirrors the folders, except for /e /purge:

if the destination directory exists, the destination directory security settings are not overwritten.

And this is what I want in this case. Otherwise just use /mir.

About the final switch /mt this tells robocopy to run this in multiple-threads. According to the robocopy-docs:

The /MT parameter applies to Windows Server 2008 R2 and Windows 7.

When trying out single-threaded without the /mt the elapsed time is 3 minutes and 19 seconds:

robocopy-single-thread

I’m testing this out on a later version of Windows anyway since the parameter is present. The default value when using only /mt is 8 threads, I’m going for 32 threads on a machine with two cores. The same operation gets down to 41 seconds:

robocopy-multi-thread

The switch is still supported in later versions of Windows and apparently works as expected, so I advice you to try it out if you need it anyway.

This type of mirroring can be good for several reasons and could be part of a scheduled task for example. There is also a big difference in elapsed time when comparing xcopy to robocopy. xcopy took about an hour to complete a task big task, and after the switch to use the robocopy command it completes in about two minutes, that’s really a big difference.

Leave a comment