What is Robocopy?
Robocopy (Robust File Copy) is a command-line folder and file replication tool that has been included in Windows since Windows Server 2008. The purpose of this post is to provide you with a quick start guide by using Robocopy examples.
Robocopy hasn’t changed much in the 25 years since it first appeared as part of the Windows NT4 resource pack. Some features, such as SMB compression, have been added, but the user remains unchanged. Robocopy examples is one of the best free tools for file copy/mirror operations because it has many useful features, such as the ability to copy NTFS ACLs (permissions).
Let’s Explore Robocopy Examples now:
Before we get into the Robocopy examples, you should be aware of the program’s main limitation: it cannot copy open or locked files because it does not use VSS (Volume Shadow Copy Service).
Help and Preset Options
Robocopy comes with extensive documentation that describes each option. Simply enter robocopy /? to get help.
Robocopy’s basic syntax is as follows:
robocopy c:\temp\source c:\temp\destination
With a bunch of default options, this will copy all files, excluding subfolders, from the specified source (c:tempsource) to the specified destination (c:tempdestination).
[Note: If you have a folder with spaces in its name, simply wrap the path in quotes (“c:tempsource with spaces”)].
Let’s look more closely at what the options mean:
- *.* Specifies a file filter. The first asterisk (*) represents the filename portion, while the second asterisk (*) represents the extension portion. The command copies all filenames with all extensions in this case.
- /DCOPY: DA – Specifies what should be copied for directories. The letters D and A stand for “data” and “attributes,” respectively.
- /COPY: DAT—Specifies what files should be copied. The additional T stands for “timestamps,” and DA stands for directories.
- /R:1000000—Specifies the number of retries on failed copies, such as when a file is locked.
- /W:30 – Specifies the amount of time 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 within the directory, robocopy examples will perform a million retries and wait 30 seconds between them, totalling nearly 350 days. This is probably not what you’re looking for.
In general, you will use robocopy examples to copy, move, and synchronize folders and files.
-
Robocopy Examples for Copying
- To copy a folder with all files and subfolders, use the following command:
robocopy c:\temp\source c:\temp\destination /E /DCOPY:DAT /R:10 /W:3
The /E switch instructs Robocopy examples to copy all subfolders, even empty ones. Use the /S switch to avoid copying empty subfolders. I specified /DCOPY: DAT (the default is DA) because I usually want the folder timestamps to match the source. In addition, I restrict the number of retries and wait times. If a file is locked, it is unlikely to be unlocked quickly, and it is easier for me to manually check the error files later rather than waiting for the entire command to complete.
- If you are about to copy a large number of files, it may be more convenient to save the output to a log file so you can review the results later, even if the command prompt is closed. It will generate a text log file containing all of the information that you would normally see on screen.
robocopy c:\temp\source c:\temp\destination /E /DCOPY:DAT /R:100 /W:3 /LOG:C:\temp\robocopylog.txt
- You will need to create a completely identical copy of the source, including the NTFS owner and auditing information, in many (if not most) cases. You will do this by appending /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
-
Robocopy Examples for Moving
For moving files and folders, Robocopy examples provide two options:
- /MOV—Moves only files, leaving the source folder structure empty.
- /MOVE—Transfers files and folders.
Use the command below to move all files and folders with all attributes, including empty ones. It should be noted that the source folder will also be deleted.
robocopy c:\temp\source c:\temp\destination /E /COPYALL /DCOPY: DAT /MOVE /R:100 /W:3
-
Robocopy Examples for synchronisation
Similar to moving, there are two options for synchronizing folders:
- /PURGE—Deletes files and folders that no longer exist in the source but do not have subfolders.
- /MIR—Is the same as /PURGE /E. /MIR also copies all files in subfolders, including those that are empty.
Use this command with caution, especially when it comes to the source and destination folders. If you make a mistake here, files in the specified folder may be lost.
robocopy c:\temp\source c:\temp\destination /MIR /COPYALL /DCOPY: DAT /R:100 /W:3
-
Robocopy Examples for Filtering
- You may want to copy only specific files, such as Word documents, at times. This is easily accomplished by including the *.docx filter after the destination folder before any other options.
robocopy c:\temp\source c:\temp\destination *.doc
- You could also choose to exclude certain files or folders from the operation. This is accomplished by using the /XF and /XF options. Use the command below to exclude all.doc files.
robocopy c:\temp\source c:\temp\destination /XF *.doc
- Use spaces in the command to exclude multiple subfolders:
robocopy c:\temp\source c:\temp\destination /XD Subfolder EmptySubfolder
Robocopy examples have many more filtering options, which you can find in the File Selection Options section of the help.
-
Robocopy Examples for Networking
- Another outstanding feature of Robocopy examples is that it works well with UNC paths and can resume operation in the event of a network outage. The following command will copy the files in restartable mode.
robocopy c:\temp\source \\MYSERVER\myshare /Z
The Backup Mode
The last, but not least, an option I’ll go over today is the backup mode option /B. Administrators can use this option to copy files in a special backup mode. If you are asked to copy folders to which you do not have access, this is extremely useful.
The backup mode bypasses NTFS permissions and copies files by using the Backup and Restore security privilege. Again, an elevated command prompt is required. Furthermore, your account must have at least SeBackupPrivilege, which is typically granted to the Administrators and Backup Operators groups.
robocopy c:\temp\source c:\temp\destination /E /COPYALL /DCOPY:DAT /B
Conclusion
Robocopy is a highly effective tool for performing a variety of file and folder operations. Many administrators use Robocopy alternatives every day for profile migrations and other similar tasks. The main advantage is that it is built into the operating system. There are numerous other options available with Robocopy examples, but covering them all would be beyond the scope of this post.