What is Windows Robocopy

Windows Robocopy was introduced with the Windows Server 2003 Resource Kit initially and then was included in-box in every Windows installation since Windows Vista. It is an extraordinary tool that beats xcopy by far,and has a lot of advanced features that make it deserve its reputation.

Robocopy has been around as an independent tool for a little while (primarily in the Windows Resource Kits), but finally in Windows Vista. Someone in the Windows product team wanted to give this little tool the recognition it deserves, and as a result it exists in the \windows\system32 directory on every Windows installation.

Why you should use Windows Robocopy?

Why is Robocopy is so cool? Well, it is hard to find a more flexible and configurable tool with over 80 switches that can do as well as Windows Robocopy. Do you remember times when you want to move a big directory from drive to another, or across the network, and you are using normal copy and past?. Chances are that the Windows explorer copy window will take for ever to count files or do other things before starting the real copy.

What if the network went down during your network copy? You have to re-do the copy and only handle those not copied yet. Well, Windows Robocopy has a resume functionality and retry mechanism to overcome those limitations. This is why I call it a robust utility. You can configure Windows Robocopy so that it will retry the copy operation x times with a y seconds interval. It can also be configured to pick up where the file transfer left off.

Furthermore, it is perfectly optimized for mirroring large file shares on slow WAN links. It only copies files that have been changed, which is perfect for occasions when you want to pull down a daily build where only 30% of the files have been changed since the last copy operation. You can set it to monitor a resources and keep the destination updated with changes on a repeated schedule.

Windows Robocopy can also do network throttling, so that the link is not monopolized by the copy process. I frankly used this tool since 2008, and back then, the internet was not as stable as nowadays, and copying large files between two locations was a big challenge, and someone should stay back at office all night to make sure it worked. Robocopy was a magical tool for me back then.

Windows Robocopy in action

It is important to note the Windows Robocopy will only take folder paths, not a file path, in its parameters. So for example, suppose you have a folder with three files [ doc1, doc2, doc3], and you wish to use Windows Robocopy to move just one file [say doc1] to another place. Well, you have to put doc1 in a separate folder, as Windows Robocopy can only take folder paths as a source and destination parameters.
Another thing to consider her, is that Windows Robocopy will only copy a file, if the source and destination copies have different time stamps or different file sizes. This is the default behavior for the tool and it is intended to save you time and resources.

Robocopy  source_folder  destination_folder [file(s)_to_copy] [options]

 

Notes:

  • Robocopy will fail to copy files that are ‘locked’ by other users or applications.
  • Robocopy will accept UNC path names including UNC path names over 256 characters long.
  • /B (backup mode) will allow Robocopy to override file and folder permission settings (ACLs).
  • To run Robocopy under a non-administrator account, will require backup files privilege. To copy security information, auditing privilege is also required. In all cases, you need at least read access to the files and folders.
  • Robocopy ‘Jobs‘ and the ‘Monitor source‘ options provide an alternative to setting up a Scheduled task to run a batch file with a Robocopy commands.
  • Be careful of spaces in source and destination folder paths. If your source folder is D:\my files, then put that path between quotes like “D:\my files”.
  • To limit the network bandwidth used by Windows Robocopy, specify the Inter-Packet Gap parameter /IPG:n. This will send packets of 64 KB each followed by a delay of n Milliseconds.

Windows Robocopy trick

The Windows Robocopy /e switch, will instruct Robocopy to ask for all subdirectories to be copied, even if they are empty. Robocopy will only copy the changed files, and will give you a report showing how many files were copied and how many were skipped.

There is one possible flaw in this arrangement. If you deleted a file from the source folder, then you copy it over an existing backup, the target folder will still contain the old file. This might not be what you want, especially if you are a developer and this is source code. Old files hanging around, are a bad idea. Robocopy has an answer for this too using the Mirror switch

Robocopy c:\mysourcedir d:\mytargetdir /mir

Windows Robocopy my favorite switch

My favorite switch for day to day operations is the /e switch. This will copy all contents including empty directories of SourceFolder to DestinationFolder:

Robocopy C:\SourceDir C:\DestDir /E

Also to move files over 14 days old (note the MOVE option will fail if any files are open and locked), I use the  /move /minage:14 switches:

Robocopy C:\SourceFoldern D:\DestinationFolder /move /minage:14

References