Show Menu
Cheatography

Robocopy By Example Cheat Sheet (DRAFT) by

Robocopy script to copy data via DOS command script

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Reference

Options Summary

Source options
/E
copy subfolders including empty subfolders
/COPYALL
copy ALL file info (DATSOU)
Copy options
/L
list only - don't actually copy
/R:n
number of retries, default is 1 million
/W:n
wait time, default is 30 seconds
Dest­ination options
/MIR
mirror - tend to avoid using
Logging options
/NP
no progress displayed, i.e. no %age copied
/LOG:­file
overwrite log file
/LOG+­:/file
append to log file
/FP
include full path of files in output
/NFL
no file list in log
/NDL
no directory list is log
/TEE
output to console and log file
Advanced options
/XF file [file]
exclude files listed
/XD dir [dir]
exclude direct­ories listed
/V
verbose output, shows skipped files
/MT[:n]
Multit­hre­aded, default is 8 - don't normally use

Requir­ements and Recomm­end­ations

The scripts, options, filenames are merely personal choices.

Source and Dest­ina­tion folders can be either local filesystem references (e.g.D­:\p­ath­\to­\di­rec­tory) or
UNC path (e.g. \\Serv­er­\sha­re­\pat­h\t­o\d­ire­ctory).

Log file suffix either Date stamp, Date and Time stamp or Job Id.
The log file path (e.g. C:\sup­por­t\l­ogs\) should exist. Robocopy makes a log file if it doesn't exist but does not create a log directory.

Should always use the log only (/L) option until ready to perform the copy at which point remove /L from the command options.

Base Command

robocopy <source> <destination> /COPYALL /L /E /R:1 /W:1 /FP /V /NP /TEE /LOG:C:\support\logs\robocopy-<suffix>.log

Simple Batch Version

@echo off
SET SDIR=<type or paste Absolute directory path or Full UNC path>
SET DDIR=<type or paste Absolute directory path or Full UNC path>
 :: File suffix either datestamp, datetimestamp or job id - uncomment as required
SET FILESUFFIX=%date:~10,4%%date:~7,2%%date:~4,2%
::SET FILESUFFIX=%date:~10,4%%date:~7,2%%date:~4,2%-%time:~0,2%%time:~3,2%%time:~6,2%
::SET FILESUFFIX=jobid
SET LOGFILE=C:\support\logs\robocopy-%FILESUFFIX%.log
:: Common parameters: /L,/V,/B,/MIR, /MT:# - add/remove as required
:: Remove /L to perform copy
SET MAIN_OPTIONS=/COPYALL /L /E /R:1 /W:1 /FP /V /NP
SET FILE_OPTIONS=/XD "RECYCLER" "System Volume Information" /
:: Default is to append to log file "+"
:: /TEE is not require if running as scheduled task
SET LOG_OPTIONS=/TEE /LOG+:"%LOGFILE%"
robocopy.exe "%SDIR%" "%DDIR%" %MAIN_OPTIONS% %FILE_OPTIONS% %LOG_OPTIONS%

Expanded Batch Version

@echo off

:: After completing job, uncomment the following 2 lines so the script cannot be run accidentally
:: echo Script has been actioned, remove/comment 'exit command' if you really want to run
:: exit /b

:: Data migration - <JOBID>
:: Source
:: * S:\Source\path\to\directory
:: * \\SOURCESERVER>\Users$\username
:: * \\SOURCESERVER>\share\path\to\location
:: Destination
:: * D:\Destination\path\to\directory
:: * \\DESTINATIONSERVER\Users$\username
:: * \\DESTINATIONSERVER\share\path\to\location

SET JOB=JOBID
:: change from "listonly" to run robocopy command
SET TYPE=listonly
::SET TYPE=run
:: Either host could be %computername% for more flexibility
SET SHOST=SERVERNAME
SET DHOST=SERVERNAME

:: SET ISOFORMAT=time || SET ISOFORMAT=date
SET ISOFORMAT=date
if %ISOFORMAT%==date (
  SET FILESTAMP=%date:~10,4%%date:~7,2%%date:~4,2%
else (
  SET FILESTAMP=%date:~10,4%%date:~7,2%%date:~4,2%-%time:~0,2%%time:~3,2%%time:~6,2%
)

:: If migrating user home folder set USER and then generate directories.
:: Otherwise can simply use static UNC or absolute paths 
SET USER=FirstName.LastName
SET SDIR=\\%SHOST%\Users$\%USER%\<SRC FOLDER>
SET DDIR=\\%DHOST%\Users$\%USER%\<DST FOLDER>
:: Change drive letter as required
::SET SDIR=\\%SHOST%\d$\<SRC FOLDER>
::SET DDIR=\\%DHOST%\d$\<DST FOLDER>

:: Note: /L is used for testing, and log filename will be appended with "-test"
:: LOGDIR must exist, change as appropriate
SET LOGDIR=C:\support\logs
if %TYPE%==listonly (
  SET LOGFILE=%JOB%-%FILESTAMP%-test.log
  SET LOGONLY=/L
) ELSE (
  SET LOGFILE=%JOB%-%FILESTAMP%.log
  SET LOGONLY=
)

:: Robocopy options - for a single folder
:: Common parameters: /L,/V,/B,/MIR, /MT:# - add/remove as required
SET MAIN_OPTIONS=/COPYALL %LOGONLY% /E /R:1 /W:1 /FP /V /NP
SET FILE_OPTIONS=/XD "RECYCLER" "System Volume Information" /XF ~.* thumbs.db desktop.ini
:: Default is to append to log file "+"
:: /TEE is not require if running as scheduled task
SET LOG_OPTIONS=/TEE /LOG+:"%LOGDIR%\%LOGFILE%"
robocopy.exe "%SDIR%" "%DDIR%" %MAIN_OPTIONS% %FILE_OPTIONS% %LOG_OPTIONS%