Introduction
Robocopy (Robust Copy) is a command line tool for copying files in Windows. It replaces the older ‘copy’ command. It contains many new features and can copy files both to an external hard drive or a remote computer.
Before you can copy files to an external hard drive, you need to create a folder on the destination drive first and set the permission on that folder to ‘Everyone’ (see below). I like to somewhat mirror the folder structure of the source and destination files. It makes for an easy reference, when you need to look for the files on the backup.
Command Layout
The three primary copy switches are /E, /S, and /MIR. They will copy just the data, attributes, and timestamps (/COPY:DAT), and not file permissions. The default behavior (not using any switches) of robocopy is to copy only newer files and overwrite the older files at the destination.
The basic format of the command.
robocopy <source directory> <destination directory> <file to copy> <switches>
If you are copying files you are not the owner of, you will need to run the the command prompt as an Administrator and use the /B switch to override the file and folder permissions. You will typically want to use the /Z if coping files over a network and you want to exclude Hidden and System files. The /TEE switch is only necessary if it is an attended backup. Finally, I like to drop a log file in the backup folder in case I need to review anything.
Copy Switches
- /L Test the command before you execute it.
- /S Do not copy empty directories.
- /E Copy all directories, including empty ones.
- /MIR Sync a directory. (Caution! If a file is deleted in source, will be deleted in destination).
- /B Copy in backup mode. Copy all files regardless of permissions. (Needed if copying files other than your own. Run command prompt as an Administrator).
Network Switches
- /Z Use Restart mode, Start copy where you left off. (Needed when possible network interference).
- /ZB Use Restart mode & Backup mode. (Needed when coping over a network).
- /W:0 Wait time between retries. (Needed when copying over a network).
- /R:1 The number of retries, if the file is locked. (Needed when coping over a network).
Exclude Switches
- /XA:H Exclude files with the attribute Hidden. (Typically want to use this switch)
- /XA:S Exclude files with the attribute System. (Typically always want to use the switch).
- /XA:SH Exclude System and Hidden files (Needed when coping a User’s Profile).
- /XJ exclude junction points. (Needed when coping a User’s Profile).
- /FTT format in FAT32 File system. (Needed if backing up files to a Linux host).
- /XD exclude directory. Use if you want to exclude some Hidden files and not others.
Logging Switches
- /NFL Don’t log file names of copied files.
- /NDL Don’t log directory names. (Include if using a log file).
- /NP Suppress % copied output to a log file. (Include if using a log file).
- /LOG:/F:\backup.log Writes output to a log file.
- /LOG+:F:\file.log Append output to an existing log file.
- /TEE Write output to the console window AND the log file.
Get Help
robocopy /?
RoboCopy Examples
Copy one or two files. If you have spaces in the file path, be sure to enclose in quotes.
robocopy "C:\users\username\desktop\folder 1" D:\backup file1.txt
robocopy "C:\users\username\desktop\folder 2" D:\backup file1.txt file2.pdf
Let’s copy all files that end with pdf or begin with Sam. The * is a wildcard.
robocopy "C:\users\username\desktop\folder 1" "D:\backup\folder 2" *.pdf
robocopy "C:\users\username\desktop\folder 1" "D:\backup\folder 2" Sam.*
Copy a Desktop folder, all files & folders on the desktop, & make a log file. Also, show the results on the screen as the command is running.
robocopy C:\Users\John\Desktop D:\backup\Desktop /MIR > C:\backup.log /TEE
Copy a directory and all subdirectories and files to a remote Windows computer.
robocopy C:\Data \\PCName\c$\Data /MIR /R:2 /W:1
Copy a directory and subdirectories and files, but excluding system & hidden files, to a remote Linux PC.
robocopy C:\Data \\NAS\data /MIR /W:0 /R:1 /XA:SH /FFT
Mirror a user’s profile & make a log file. You need to exclude junction points (they are kind of like a soft link or a shortcut) or the profile could get stuck in a copy loop. Exclude the system files and exclude the “AppData” folder. This will keep any other Hidden files.
robocopy "C:\Users\John" "F:\backup\Users\John" /MIR /XA:S /XD "c:\Users\John\AppData" /XJ /TEE /NP /LOG:F:\backup\backup.log
Prepare for a Backup to an External Drive
Begin by plugging in a USB stick into any open port on the PC. In our example, it shows up as “Drive F”. Now, set up a receiving folder on the USB stick called D:\backups. Set the permissions on the folders to ‘Everyone’ by right clicking on each the folders, go to properties, security tab, select add group or username, and select check names. Be sure to verify that the Everyone has all the allow permissions.
If you make any folders under the new backup folder, should inherit the same permissions as the backups folder (i.e. Everyone). Now, when we copy any folders or files to these destination folders, they too will inherit the same permissions (i.e. Everyone).
If you don’t do this and if you need to access the files on a PC other than the original computer, you may not be able to open the files. This is because you are not logged in as the original user that ran the backup. But, with folder permissions set to Everyone, you can go and plug the external drive or USB stick into any computer, and you will have full access to the copied files.

The Backup Script
We will backup the contents of a generic folder and two user’s profiles. It will copy to both an external hard drive and then also back up files to a remote windows device.
Let’s open notepad and type out some commands. We will assume that we are logged in as a user called Jack and it is an Admin account. We will start by coping all files in a folder called data. Since we don’t know who owns this folder, we will use the /B switch to overwrite any potential permissions issues. Finally, we will make a log file but exclude the % copied for each file. As this unnecessary clogs up the log output.
rem backup.bat
robocopy C:\Data F:\backups\data /MIR /B /NP /LOG:F:\backup.log
Next, we can now copy Jack and Jill’s profiles. Jack’s profile, we will exclude all hidden files, system files, and junction points. Since his “AppData” folder is hidden, we do not need to specifically exclude it.
In Jill’s profile, we have several hidden files that we want to copy that contain her SSH keys. So, we will not create a blanket exclusion for all hidden files. Instead, we will exclude her “AppData” folder specifically. Finally, we will add to the previously created log file, but exclude the % total copied , as it just clogs up the log file anyway.
robocopy C:\Users\Jack F:\backups\Jack /MIR /XA:SH /XJ /NP /LOG+:F:\backup.log
robocopy C:\Users\Jill F:\backups\Jill /MIR /XA:S /XD C:\Users\John\AppData /B /XJ /NP /LOG+:F:\backup.log
Now, lets copy the same files to a remote computer for a second backup. As before, we will use the /MIR switch to make an exact duplicate folder tree, /ZB to run in restart and backup mode and exclude the System, Hidden, Junction Points, and call out Jill’s “AppData” folder. Finally, we use the /R and /W in case there are unforeseen network issues.
rem copy to remote systems.
robocopy C:\data \\PCName\c$\backups\data /MIR /ZB /XA:SH /XJ /R:1 /W:1
robocopy C:\Users\Jack \\PCName\c$\backups\Jack /MIR /ZB /XA:SH /XJ /R:1 /W:1
robocopy C:\Users\Jill \\PCName\c$\backups\Jill /MIR /ZB /XA:S /XD C:\Users\Jill\AppData /XJ /R:1 /W:1
Save and Execute the Script
Save the script to Jack’s desktop as backup.bat. To run the script, open a command prompt as an Admin, and navigate to Jack’s desktop. Type the script name and hit enter. To run the script nightly, you can set up a scheduled task to execute. If you move the script around any, Be sure to set it to run under a user with Admin permissions.
References
https://ss64.com/nt/robocopy.html
http://caughtinpixels.com/how-to-create-a-backup-script-using-robocopy/