Robocopy - How to copy All Files & Folders and Run Command Again and Only Copy Changed Files and Keep Permissions

Printer-friendly versionPDF version
Robocopy - How to copy All Files & Folders and Run Command Again and Only Copy Changed Files and Keep Permissions

This tutorial describes how to use robocopy to copy all files and folders from a source to a destination. With the same command, you can run it again later, and it will only copy files that have changed to the destination. Also, if files were deleted from the source, it will remove them from the destination as well. This command will also keep all of the NTFS permissions and file attributes.

A typical use scenario for this command is copying a large file share. You can run the command once to copy the bulk amount of the data, then you can run it again later, and it will only copy changed files, which will make it much faster.

robocopy C:\source C:\dest /TEE /LOG+:c:\robolog.txt /MIR /copyall /zb /w:1 /r:2 /xo

First, I'll show you examples of this command in action, and then I'll break the command down to show you what each part does. (Make sure to launch the command prompt as administrator)

In the image below, you see a source and destination directory. The source has multiple folders with data in them. The destination is empty.

Robocopy - How to copy All Files & Folders and Run Command Again and Only Copy Changed Files and Keep Permissions - 1

After running the robocopy command above, while using C:\ITswapshop.com\source as the source folder, and C:\ITswapshop.com\destination as the destination folder, you can see the destination folder now looks identical to the source:

Robocopy - How to copy All Files & Folders and Run Command Again and Only Copy Changed Files and Keep Permissions - 2

Now I'm going to delete a file from the source, and then add a different file to the source. See what happens when I run the command again:

Robocopy - How to copy All Files & Folders and Run Command Again and Only Copy Changed Files and Keep Permissions - 3

The file that was deleted from the source, is also deleted from the destination. Also, the newly added file to the source is copied to the destination. All of the files that haven't been modified are skipped.

Now, let's break down the command so we can know exactly what each switch does:

robocopy source destination - source and destination can be a local path (C:\Users\MyAccount), a mapped drive (U:\sharedfiles), or a UNC path (\\server01\share\data). Make sure to put them in quotes if the contain spaces.

/TEE - This will display the output of the command in the console window, and it will write it to a log for further review.

/LOG+c:\robolog.txt - This tells the command where to write the logs. The + sign tells it to append the log file, instead of overwriteing it.

/MIR - This will copy all files and subfolders, and it will remove files/folders from the destination, if they no longer exist on the source.

/COPYALL - This will copy all of the NTFS permissions and attributes (security permissions, timestamps, owner info...)

/ZB - This uses restartable mode when copying files. If a file is in use, it will retry after a set amount of time. If access is denied, it will try to copy in backup mode.

/W:1 - This specifies to wait 1 second between retries when copying files.

/R:2 - This specifies the number of retries on failed copies.

/XO - This excludes older files/folders if the destination file/folder exists and is the same date

 

I hope this helps some of you out. If so, please leave a comment below letting other readers know it worked for you.

Thank you. Very useful for me.

use robocopy

I use the above type script for most of my copy needs, the /SEC switch retains NTFS perms.

SORC & DEST can be either DRIVE:\PATH or \\SERVER\SHARE\PATH up to you :)

I would reccomend that you do not MOVE the data but copy it, just in case the sky falls in and you need to go back...i currently use gs rich copy 360 enterprise for these type of work..because i have some issues with robocopy like file eroors etc.

Awesome...exactly what I needed. Thank you!

Post new comment