The estimated reading time 2 minutes
doing on-premises project you often need to copy files from site A to site B. You need to sync folders or files or anyother things. Therefor robocopy is a small and integrated solution in windows environments. It’s integrated by default from Windows Vista and Server 2008 and above. If you have any older version (hopefully not) you can find the robocopy tool in “Windows Ressource Kit” on this LINK.
Executing this tool is quite easy, just open an normal CMD and type in robocopy /?
You can start CMD with shortcut WIN+R and type “cmd” in the run field. Now you can see the black and good old cmd.
robocopy works with a simple method. Type in robocopy then space, after this the sourcepath , another space and destination. Here we go. There are a lot of parameters you can use with robocopy which makes it really powerful.
Let’s begin with a simple mirroring of an folder:
robocopy C:\Users\%username%\Documents D:\Documents /mir
It’s also possible to backup data on fileshares like my nas system (drive Y:)
echo@off robocopy C:\Users\%username%\Documents\ Y:\documents /mir /R:2 /W:3 /LOG+:"Y:\backuplogs\log-%date:~0,2%-%date:~3,2%-%date:~6,4%-%time:~0,2%-%time:~3,2%-%time:~6,2%.log" /TEE /NDL pause
/R:2 = try it twice times if there are access denied
/W:3= wait 3 seconds before retry
/LOG+: = create logfile in the specified path
/TEE = show log in console also (only if there is a logfile in use)
/NDL = no directories in logfiles
/XD = skip special directory name
/XF =skip special file name
If you use this robocopy script it will generate a new logfile everytime robocopy is started.
here is another script to get a backup of some data with timestamp in foldername:
@echo off echo ########Backup ############## set BACKUPFOLDER="C:\temp-Backups\%DATE:~6,4%-%DATE:~3,2%-%DATE:~0,2%-%time:~0,2%-%time:~3,2%-%time:~6,2%-Backup" echo creating backupfolder %SBACKUPFOLDER% mkdir %BACKUPFOLDER% robocopy "C:\XXXXXXXXXX\XXXX" %BACKUPFOLDER% /mir /R:2 /W:3 /LOG+:"C:\log\%DATE:~6,4%-%DATE:~3,2%-%DATE:~0,2%-%time:~0,2%-%time:~3,2%-%time:~6,2%.log" /TEE /NDL
Additional Parameters which might be useful:
Lot of questions concering ntfs permissions and timestamps of files and folders, yes you can do it, but default value of robocopy is: /COPY:DAT (Data, Attributes,Timestamp)
/COPYALL (Timestamp, NTFS permissions, Attributes)
/SEC (= /COPY:DATS Data, Timestamp und NTFS Persmissions)
/DCOPY:T (copies folder timestamps)
/TIMFIX (repairs timestamps in destination folders)
NOTE: foldernames with date only works in CMD not in PowerShell.
See also my article about generating Robocopy jobs with PowerShell.