Robocopy: Best practices for working with this helpful Windows utility

As someone who grew up using the old DOS “copy” command, copy is often my go to when I only need to copy a few files. However, Windows includes another file copy utility called Robocopy. Robocopy is far superior to the old copy command, and is well-suited to the task of performing large or complex file copy operations.

As useful as Robocopy may be, however, it can be a bit complicated to use. Entering the Robocopy /? command reveals dozens of command line switches. If you don’t use Robocopy on a regular basis, it can sometimes be tough to figure out which switches are best suited to completing the task at hand. That being the case, I wanted to take the opportunity to explain the switches that I tend to use the most often. Keep in mind that this is by no means intended to be a comprehensive discussion of the available command line switches, but rather a discussion of some of the ones that I find to be most useful.

Copying file attributes

The Robocopy utility includes a /COPY switch that allows you to specify exactly which parts of the file you want to copy. This gives you a way of making sure that the copied data retains the original date and time stamp, security permissions, and that sort of thing.

When you use the /COPY switch, you are required to specify one or more flags that tell Robocopy what to copy. Those flags are:

D – Data
A – Attributes
T – Timestamps
S – NTFS ACL Security
O – Owner
U – Auditing information

The nice thing about having so many flags available to you is that it allows for very granular control over the copy process. The disadvantage however, is that it can be tedious to have to use so many different flags. Thankfully, there are some shortcuts available.

If you want to make sure that you retain the most commonly used components (Data, Attributes, Timestamp, and Security), you can use the /SEC switch. It does exactly the same thing as using /COPY:DATS

If you are like me and want to retain all of the file’s various attributes, then you can use the /COPYALL switch instead. This is the equivalent to using /COPY:DATSOU

Subdirectories

Shutterstock

If you are going to be copying a group of files, then there is a good chance that you will also need to copy any subdirectories that may be present. You can tell Robocopy to copy these subdirectories by including the /S switch. However, I recommend using the /E switch instead.

The difference between these two switches is that if you use the /S switch, then Robocopy will only copy subdirectories containing data, but will ignore any empty subdirectories. While that may be fine in some situations, my philosophy has always been that even if a subdirectory is empty, there is a reason why it exists. Perhaps an application occasionally looks to that subdirectory to see whether or not a particular file exists. Using the /E switch instead of the /S switch ensures that the resulting copy will have a directory structure that perfectly mimics that of the original data.

Preventing errors

Microsoft

It’s no fun to start a large copy operation, and then come back a few hours later to discover that the operation has failed for some reason and that you have to start over again. Fortunately, Robocopy provides some tools for avoiding these frustrating situations.

The first of these options is something called Restartable Mode. Restartable Mode can be enabled using the /Z switch, and is well suited to situations in which you need to copy large files over a potentially unreliable network. If the file transfer fails, then Restartable Mode allows the transfer to be resumed from the point of failure, rather than requiring it to be restarted from the beginning.

A second option is to use Backup Mode. Backup Mode can be enabled by using the /B switch, and is useful for overcoming permissions related errors. If you are copying user data, then there is a chance that you might not have permission to access all of the user’s files. Normally, this would cause the file copy process to fail.

Backup Mode overcomes this problem by treating the file copy process as a backup operation, essentially ignoring any permissions related issues. In order to use Backup Mode however, you do have to be using Robocopy from within an administrative command prompt and you need to have backup privileges.

The Restartable Mode and the Backup Mode options are mutually exclusive, which means that you can choose to use one or the other, but not both. Even so, there is a way to get the best of both worlds. Rather than using the /Z switch or the /B switch, use /ZB instead. This tells Robocopy to use Restartable Mode, but to switch to Backup Mode if it encounters a permissions problems.
While I am on the subject of error prevention, I want to quickly mention the /TIMEFIX and /SECFIX switches. These switches tell Robocopy to fix time errors and security errors on files. Robocopy can even fix these errors on files that it skips.

Performance

One last thing that I wanted to mention is that there is Robocopy includes a switch that you may be able to use to decrease the amount of time that the copy process takes to complete. The switch is called /MT, and it is used to specify the number of threads used by the copy process. By default, Robocopy distributes the copy process across eight threads, but you can increase the thread count to as much as 128.

Obviously, increasing the thread count won’t help in every situation. If the storage device is running at its maximum I/O capacity for instance, then using extra threads isn’t going to do anything to improve performance. In some cases though, using extra threads can help.

Robocopy: A lot of uses

While it is easy to think of Robocopy as a tool for copying files from Point A to Point B, it can also be used for migrations. Robocopy has the ability for example, to monitor a source for changes and then replicate those changes to a destination. While there are admittedly some third-party tools that really take the copy process to the next level, Robocopy is an excellent choice if you want to stick to using native tools.

Featured image: Shutterstock

Brien Posey

Brien Posey is a freelance technology author and speaker with over two decades of IT experience. Prior to going freelance, Brien was a CIO for a national chain of hospitals and healthcare facilities. He has also served as a network engineer for the United States Department of Defense at Fort Knox. In addition, Brien has worked as a network administrator for some of the largest insurance companies in America. To date, Brien has received Microsoft’s MVP award numerous times in categories including Windows Server, IIS, Exchange Server, and File Systems / Storage. You can visit Brien’s Website at: www.brienposey.com.

Share
Published by
Brien Posey

Recent Posts

Will IPv6 ever kill IPv4? If so, when?

IPv6 and its vast pool of Internet address spaces has long been expected to kill off IPv4. But the old…

13 hours ago

Another tool for your toolbox: Using Capacity Planner for Hyper-V Replica

For those who use the Hyper-V replica feature, a tool from Microsoft can help you analyze the infrastructure required to…

17 hours ago

Create a Log Analytics query without using your keyboard (No experience necessary!)

Never created a query in Log Analytics? It’s easy. In the case shown here, it’s so easy you don’t need…

20 hours ago

Can I have your attention: Add PowerShell on-screen alerts to your scripts

While there is no native command for pop-up messages in PowerShell, you can generate PowerShell on-screen alerts and pop-up boxes…

2 days ago

How spammers harvest email addresses — and what you can do about it

Spammers are pretty smart and have thought up many ingenious ways of getting hold of your email address. Is there…

2 days ago

How to create an Azure disaster recovery site using Azure Site Recovery

Here’s how to replicate an application running in one of the Azure regions to another region with just a few…

2 days ago