- AccessChk: View effective permissions on files and folders - Thu, Apr 13 2023
- Read NTFS permissions: View read, write, and deny access information with AccessEnum - Wed, Mar 29 2023
- Kill Windows a process with Tskill and Taskkill - Mon, Mar 13 2023
Over the past 25 years, since Robocopy first became available as part of the Windows NT4 resource pack, it has not changed much. Some features, such as SMB compression, were added, but the usage is still the same. As Robocopy has many great features, such as the ability to copy NTFS ACLs (permissions), it is one of the best free tools for file copy/mirror operations. This Wikipedia post is a good starting point for getting more information about robocopy.
Before we get to the Robocopy examples, you should know the main limitation of robocopy—it cannot copy open or locked files, as it does not use VSS (Volume Shadow Copy Service).
Help and default options
Robocopy itself includes very detailed help, with a description of each option. Simply type robocopy /? to get help displayed.
The basic syntax for Robocopy is:
robocopy c:\temp\source c:\temp\destination
This will copy all files, excluding subfolders, from the specified source (c:\temp\source) to the specified destination (c:\temp\destination) with a bunch of default options.
Note: If you have a folder with spaces in the name, simply put quotes around the path (“c:\temp\source with spaces”).
Let's take a closer look at what the options mean:
- *.*—Defines a file filter. The first asterisk (*) represents the filename part, and the second asterisk represents the extension. In this case, the command copies all filenames with all extensions.
- /DCOPY:DA—Defines what to copy for directories. D stands for "data" and A stands for "attributes."
- /COPY:DAT—Defines what to copy for files. The extra T stands for "timestamps;" DA is the same as for directories.
- /R:1000000—Defines the number of retries on failed copies, for example, if a file is locked.
- /W:30—Defines the number of seconds to wait between retries.
As you can see, the number of retries is very large (one million), which is not practical in most cases. If there is a single locked file inside the directory, robocopy will do a million retries and will wait 30 seconds between them, amounting to almost 350 days. I guess this is not what you normally want.
Generally speaking, there are three things you will do using robocopy: copy, move, and synchronize folders and files.
The easiest way to copy a folder with all files and subfolders is to run this command:
robocopy c:\temp\source c:\temp\destination /E /DCOPY:DAT /R:10 /W:3
The /E switch tells Robocopy to copy all subfolders, including empty ones. If you don't want to copy empty subfolders, use the /S switch. Note that I have specified /DCOPY:DAT (default is DA), as I usually want the folder timestamps to be the same as the source. I also limit the number of retries and wait times. If there is a file locked, it won't usually be unlocked quickly, and it's easier for me to check the erred files manually later rather than waiting ages for the whole command to complete.
If you are about to copy a large number of files, it might be more useful to save the output to a log file so you can review the results later, even if the command prompt is closed.
robocopy c:\temp\source c:\temp\destination /E /DCOPY:DAT /R:100 /W:3 /LOG:C:\temp\robocopylog.txt
Will give you a text log file with all the information you would normally see on screen.
Note: If the destination directory (c:\temp\destination) already exists, you will see one skipped folder in the results.
In many (if not most) cases, you will need to create a completely identical copy of the source, including the NTFS owner and auditing information. For that, I add /COPYALL to the command, which is equivalent to /COPY:DATSOU.
robocopy c:\temp\source c:\temp\destination /E /COPYALL /DCOPY:DAT /R:100 /W:3
Note that this command has to be run from an elevated command prompt, unless you have turned off UAC, otherwise you'll receive this error message:
ERROR : You do not have the Manage Auditing right.
Robocopy offers two options for moving files and folders:
- /MOV—Moves files only, leaving the empty folder structure at the source
- /MOVE—Moves files and folders
Use the command below to move all files and folders, including empty ones, with all attributes. Note that the source folder will also be deleted.
robocopy c:\temp\source c:\temp\destination /E /COPYALL /DCOPY:DAT /MOVE /R:100 /W:3
As with move, there are two options to synchronize folders:
- /PURGE—Deletes the files and folders that are no longer present in the source but without subfolders.
- /MIR—Is equivalent to /PURGE /E. /MIR also copies all files in subfolders, including empty ones.
Be careful with this command, especially with source and destination folders. If you make a mistake here, you might get files lost in the specified folder.
robocopy c:\temp\source c:\temp\destination /MIR /COPYALL /DCOPY:DAT /R:100 /W:3
Sometimes you might want to copy only specific files, for example, Word documents. This can be done easily by adding the *.docx filter after the destination folder before any other options.
robocopy c:\temp\source c:\temp\destination *.doc
You might also want to exclude some files or folders from the operation. This is done using the /XF and /XF options. To exclude all .doc files, use the command below.
robocopy c:\temp\source c:\temp\destination /XF *.doc
To exclude multiple subfolders, use spaces in the command:
robocopy c:\temp\source c:\temp\destination /XD Subfolder EmptySubfolder
Robocopy includes many more filtering options, which you can find in help under the File Selection Options section.
Another great feature of Robocopy is that it works well with UNC paths and can resume operation in case of a network interruption. The command below will copy the files in restartable mode.
robocopy c:\temp\source \\MYSERVER\myshare /Z
The last, but definitely not the least, option I will cover today is the backup mode option /B. This option allows administrators to copy files in a special backup mode. This is extremely useful if you are asked to copy folders to which you don't have access. The backup mode uses the Backup and Restore security privilege to bypass the NTFS permissions and copy the files. This again requires a command prompt to be elevated. In addition, your account needs at least SeBackupPrivilege, which is usually granted to the Administrators and Backup Operators groups.
robocopy c:\temp\source c:\temp\destination /E /COPYALL /DCOPY:DAT /B
Robocopy is a very powerful tool for performing various file and folder operations. Many administrators use it on a daily basis for profile migrations and other similar operations. The big advantage is that it's part of the operating system itself. There are dozens of other options that can be used with Robocopy, but to cover them all would be beyond the scope of this post.