Robocopy
DISCLAIMER: The information in this guide is provided "as is" without any guarantee of completeness, accuracy, timeliness, or of the results obtained from the use of this information. The author assumes no responsibility for any errors or omissions in the content. It is meant for general information purposes only and should not be used as a substitute for professional advice. The author is not responsible for any damages caused by the use of this information. By using this guide, you agree to hold the author harmless from any and all claims, damages, or expenses that may arise from your use of the information.
Robocopy Command
File/Folder Synchronization (From SOURCE to DESTINATION)
The following examples will mirror/synchronize files from the source to the destination via Robocopy's /MIR switch. The /MIR switch will instruct Robocopy to copy files/folders from the source that do not exist in the destination and also delete files/folders in the destination that do not exist in the source. Use caution with Robocopy's /MIR switch and verify your source and destination locations and the order that they are placed in the Robocopy command. Failure to do so could result in the loss of data. Ensure that there are backups in place before using Robocopy.
Replace all <PLACEHOLDER> text with their respective values. (e.g., <PASSWORD> = YourPassword)
Example1: Syntax
ROBOCOPY <Source> <Destination> /MIR /DCOPY:T /COPYALL /SECFIX /XJD /R:0 /W:0 /MT:8
Example2: Drive Letters
ROBOCOPY X:\ Y:\ /MIR /DCOPY:T /COPYALL /SECFIX /XJD /R:0 /W:0 /MT:8
Example3: Drive Letters w/ Subfolders (with blank spaces in destination)
ROBOCOPY X:\From_Source\ "Y:\To Destination\" /MIR /DCOPY:T /COPYALL /SECFIX /XJD /R:0 /W:0 /MT:8
Example4: UNC/Network Paths (with blank spaces in destination)
ROBOCOPY \\OldServer-FS1\From_Source\ "\\NewServer-FS2\To Destination\" /MIR /DCOPY:T /COPYALL /SECFIX /XJD /R:0 /W:0 /MT:8
Robocopy Switches
| /L | List only (What If) - don’t copy, timestamp or delete any files. |
| /MIR |
Replicate data, copying all files in source that is not in the destination and delete files in destination that is not in source (equivalent to /E /PURGE).
|
| /E | Copy subfolders, including empty subfolders. |
| /PURGE | Delete dest files/folders that no longer exist in source. |
| /DCOPY:T | Copy directory timestamps. |
| /COPYALL |
Copy ALL file info (equivalent to /COPY:DATSOU).
|
| /COPY:DAT |
What to copy (default is /COPY:DAT). Copy Flags:
|
| /XF |
Exclude directories matching given names/paths.
|
| /XD |
Exclude files matching given names/paths/wildcards.
|
| /XJF |
Exclude junction points from source files.
|
| /XJD |
Exclude junction points from source directories.
|
| /Z |
Copy files in Restartable mode (survive network glitch). This option is useful when transfering files (especially large files) over the network or unstable connections as it will continue where the transfer left off if there are connection issues.
|
| /B | Copy files in Backup mode. Backup mode allows access to a file without worrying about permissions. |
| /ZB |
Copy file in Restartable mode; if access denied use Backup mode.
|
| /R:n |
Number of Retries on failed copies - default is 1 million. |
| /W:n |
Wait time between retries - default is 30 seconds. |
| /MT:n |
Multithreaded copying, n = no. of threads to use (1-128). Default = 8 threads, not compatible with /IPG and /EFSRAW.
|
| /LOG:file |
Output status to LOG file (overwrite existing log). |
| /LOG+:file |
Output status to LOG file (append to existing log). |
| /TEE |
Output to console window, as well as the log file. |
| /ETA |
Show Estimated Time of Arrival of copied files. |
Easy RoboCopy (NTFS to NTFS)
ROBOCOPY.EXE \\angelo-fs1\d$ D:\ /E /COPYALL /MIR /XJ /XF Thumbs.db datto.* /XD $Recycle.Bin "System Volume Information" /R:0 /W:0 /V /TEE /LOG:c:\Robocopy-D_Drive.txt /MT:4
Easy RoboCopy (NTFS to Non-NTFS)
ROBOCOPY.EXE \\10.10.10.19\TData \\10.10.10.98\TData /E /MIR /XJ /XD $Recycle.Bin "System Volume Information" /R:0 /W:0 /V /TEE /LOG:c:\robocopy_tdata.txt /MT:2
Scripts
ROBOCOPY.EXE "D:\Accounting" "\\10.144.194.24\d$\Accounting" /E /COPYALL /MIR /XJ /XF Thumbs.db datto.* /XD $Recycle.Bin "System Volume Information" /R:0 /W:0 /V /TEE /LOG:"C:\SniderIT\Migration Logs\Accounting.log" /MT:4
Sequentially run a group of Robocopy commands (.cmd files) using a script.
After configuring the Robocopy command in EasyRobocopy, save the command in a .cmd file using the EasyRobocopy interface:
Ensure that all .cmd files are located within the same folder. Afterwards, create the following script (e.g., Robocopy.bat), review/update and run the following script to start the Robocopy:
@echo off
setlocal enabledelayedexpansion
for /f "tokens=2,3,4 delims=/ " %%i in ("%date%") do (
set "currentYear=%%k"
set "currentYear=!currentYear:~-2!"
set "currentDate=!currentYear!!date:~-7,2!!date:~4,2!"
)
for /f "tokens=1,2 delims=: " %%i in ("%time: =0%") do (
set /a "hours=%%i"
set "minutes=%%j"
set "ampm=AM"
if !hours! geq 12 (
if !hours! gtr 12 set /a hours-=12
set "ampm=PM"
) else (
if !hours! equ 0 set /a hours=12
)
)
if !hours! lss 10 set hours=0!hours!
set "baseLogFile=%~dp0\%~n0_%currentDate%_%hours%%minutes%%ampm%.log"
set "logFile=%baseLogFile%"
set /a suffix=1
:checkLogFile
if exist "%logFile%" (
set "logFile=%~dp0\%~n0_%currentDate%_%hours%%minutes%%ampm%_!suffix!.log"
set /a suffix+=1
goto :checkLogFile
)
echo List of .cmd files to be processed:
echo List of .cmd files to be processed: >> "%logFile%"
set /a counter=1, totalSeconds=0
for /f "delims=" %%A in ('dir /b "%~dp0\*.cmd"') do (
echo !counter! - %%A
echo !counter! - %%A >> "%logFile%"
set /a counter+=1
)
set "lastScript="
set /a counter=1
for /f "delims=" %%A in ('dir /b "%~dp0\*.cmd"') do (
set "lastScript=%%A"
echo --------------------------------------------------------------
echo -------------------------------------------------------------- >> "%logFile%"
echo PROCESSING #!counter! - Starting "%%A"...
echo PROCESSING #!counter! - Starting "%%A"... >> "%logFile%"
for /f "tokens=1-4 delims=:.," %%a in ("!time!") do (
set /a "start=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)-361200"
)
echo Start Time: !currentDate:~4,2!/!currentDate:~-2,2!/!currentYear! !hours!:!minutes! !ampm! >> "%logFile%"
call "%%A" || (
echo Error: Failed to execute "%%A".
echo Error: Failed to execute "%%A". >> "%logFile%"
)
for /f "tokens=1-4 delims=:.," %%a in ("!time!") do (
set /a "end=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)-361200"
)
set /a elapsed=end-start
if !elapsed! lss 0 set /a elapsed+=86400
set /a totalSeconds+=elapsed
echo Finished processing #!counter! - "%%A". >> "%logFile%"
echo End Time: !currentDate:~4,2!/!currentDate:~-2,2!/!currentYear! !hours!:!minutes! !ampm! >> "%logFile%"
set /a ss=elapsed%%60, elapsed/=60, mm=elapsed%%60, hh=elapsed/60, dd=hh/24
if !ss! lss 10 set ss=0!ss!
if !mm! lss 10 set mm=0!mm!
if !hh! lss 10 set hh=0!hh!
if !dd! lss 10 set dd=0!dd!
echo Duration: !dd! days !hh!:!mm!:!ss! >> "%logFile%"
set /a counter+=1
)
echo ============================================================== >> "%logFile%"
if defined lastScript (
echo Last processed: !lastScript!
echo Last processed: !lastScript! >> "%logFile%"
)
set /a ss=totalSeconds%%60, totalSeconds/=60, mm=totalSeconds%%60, hh=totalSeconds/60, dd=hh/24
if !ss! lss 10 set ss=0!ss!
if !mm! lss 10 set mm=0!mm!
if !hh! lss 10 set hh=0!hh!
if !dd! lss 10 set dd=0!dd!
echo Total duration: !dd! days !hh!:!mm!:!ss!
echo Total duration: !dd! days !hh!:!mm!:!ss! >> "%logFile%"
endlocal
pause
Sources
KB Meta
| Page Includes | @9#bkmrk-callout-danger-NoResponsibilityDisclaimer-5wod5ufe |
| Page Includes | @9#bkmrk-callout-info-ReplaceAllPlaceholderText-86k2hi4r |


