Table of Contents
Use Robocopy to copy files to another drive
Robocopy (Robust File Copy) is a command-line tool built into Windows, but it has been around for years, and it’s a powerful and flexible tool to migrate files extremely fast.
robocopy "C:\MDaemon" "E:\MDaemon" /E /Z /ZB /R:5 /W:5 /TBD /NP /V /MT:16 /LOG:C:\log.txt
Robocopy has many features that you can use, and in the command shown in this guide, we’re using the following options to make the copy reliable and fast.
- /S — Copy subdirectories, but not empty ones.
- /E — Copy Subdirectories, including empty ones.
- /Z — Copy files in restartable mode.
- /ZB — Uses restartable mode. If access is denied, use backup mode.
- /R:5 — Retry 5 times (you can specify a different number, the default is 1 million).
- /W:5 — Wait 5 seconds before retrying (you can specify a different number, the default is 30 seconds).
- /TBD — Wait for share names To Be Defined (retry error 67).
- /NP — No Progress – don’t display percentage copied.
- /V — Produce verbose output, showing skipped files.
- /MT:16 — Do multithreaded copies with n threads (default is 8).
Perhaps the most important switch to pay attention to is /MT, an option that enables Robocopy to copy files in multithreaded mode. Typically, when you copy files from File Explorer, you only copy one file at a time.
If you do not set a number when using the “/MT” switch, then the default number will be “8,” which means that Robocopy will try to copy eight files at the same time, but the tool supports “1” to “128” threads.
Use Robocopy to copy files over the network
robocopy "D:\data" "\\10.10.5.89\data" /E /Z /ZB /R:5 /W:5 /TBD /NP /V /MT:16
-------------------------------------------------------------------------------
ROBOCOPY :: Robust File Copy for Windows
-------------------------------------------------------------------------------
Started : Tuesday, May 16, 2023 1:31:12 PM
Source : D:\data\
Dest : \\10.10.5.89\data\
Files : *.*
Options : *.* /TBD /V /S /E /DCOPY:DA /COPY:DAT /ZB /NP /MT:16 /R:5 /W:5
------------------------------------------------------------------------------
New File 6137 D:\data\DSM.ovf
New File 261.3 m D:\data\DSM_DS3617xs_23739.pat
New File 19.8 m D:\data\synoboot.vmdk
------------------------------------------------------------------------------
Total Copied Skipped Mismatch FAILED Extras
Dirs : 1 1 0 0 0 0
Files : 3 3 0 0 0 0
Bytes : 281.26 m 281.26 m 0 0 0 0
Times : 0:02:01 0:00:30 0:00:00 0:00:26
Speed : 9,767,611 Bytes/sec.
Speed : 558.907 MegaBytes/min.
Ended : Tuesday, May 16, 2023 1:32:08 PM
The output with /MT:16
Total Copied Skipped Mismatch FAILED Extras
Dirs : 129 128 1 0 0 0
Files : 1748 1748 0 0 0 0
Bytes : 1.369 g 1.369 g 0 0 0 0
Times : 0:01:21 0:01:16 0:00:00 0:00:05
Speed : 19,339,555 Bytes/sec.
Speed : 1,106.618 MegaBytes/min.
The output with /MT:32
Total Copied Skipped Mismatch FAILED Extras
Dirs : 129 129 0 0 0 0
Files : 1748 1748 0 0 0 0
Bytes : 1.369 g 1.369 g 0 0 0 0
Times : 0:00:22 0:00:21 0:00:00 0:00:01
Speed : 68,556,535 Bytes/sec.
Speed : 3,922.836 MegaBytes/min.
https://www.softwaretesttips.com/robocopy-gui/
Examples for copying
1️⃣ Copy all files and subdirectories, including empty directories:
robocopy "C:\Databases" "D:\Backup" /E /ZB /R:5 /W:5
- /E: Copies subdirectories. This option automatically includes empty directories.
- /ZB: Copies files in restartable mode. If file access is denied, switches to backup mode.
- /R:2 /W:5: Retry two times and waiting 5 seconds between each retry.
2️⃣ Copy all files and subdirectories, excluding empty directories:
robocopy "C:\Databases" "D:\Backup" /E /S /ZB /R:5 /W:5
- /E: Copies subdirectories. This option automatically includes empty directories.
- /S: Copies subdirectories. This option automatically excludes empty directories.
- /ZB: Copies files in restartable mode. If file access is denied, switches to backup mode.
- /R:2 /W:5: Retry two times and waiting 5 seconds between each retry.
3️⃣ Copy all files and subdirectories, excluding some directories and files by name:
robocopy "C:\Databases" "D:\Backup" /E /S /ZB /R:5 /W:5 /XD "202302*" "202303*"
robocopy "C:\Databases" "D:\Backup" /E /S /ZB /R:5 /W:5 /XF "*.wim"
- XD/: Exclude certain directories matching a specific name
- XF/: Excludes files that match the specified names or paths. Wildcard characters (* and ?) are supported.
4️⃣ Copy files matching a specific file name string or extension. For example, we can filter to copy the *.txt, *.log, *.dat…
robocopy "C:\Databases" "D:\Backup" /E /S /ZB /R:5 /W:5 "*.txt"
4️⃣ Copy files and folders based on the file age:
robocopy "C:\Databases" "D:\Backup" /E /S /ZB /R:5 /W:5 /MAXAGE:30
- /MAXAGE: Specifies the maximum file age (to exclude files older than n days or date)
5️⃣ Copy files and folders based on the timestamp attribute:
robocopy "C:\Databases" "D:\Backup" /E /S /ZB /R:5 /W:5 /MAXAGE:20230615
- /MAXAGE: Specify in YYYMMDD format the older date a file can have before it’s copied.
6️⃣ Asynchronous Copying: By default, robocopy only processes 8 files at a time. However, you can force robocopy to copy more files than at once by using the /MT option”
robocopy "C:\Databases" "D:\Backup" /E /S /ZB /R:5 /W:5 /MT:32
- /MT: Specify the number of threads robocopy will use to copy files. The maximum is 128
7️⃣ Copy and redirecting the output log to a file:
robocopy "C:\Databases" "D:\Backup" /E /S /ZB /R:5 /W:5 /LOG:C:\Logs\copy.log
If you’d like to keep the contents of any existing log file and append results to a file, you can use the + operator as shown below.
robocopy "C:\Databases" "D:\Backup" /E /S /ZB /R:5 /W:5 /LOG+:C:\Logs\copy.log
Examples for moving
1️⃣ Mirror the contents of the “Source” folder to the “Backup” folder then delete any files in the destination that don’t exist in the source:
robocopy "C:\Source" "D:\Backup" /MIR /R:2 /W:5 /LOG:C:\Logs\backup.log
2️⃣ Move files and subdirectories, excluding empty directories, and exclude files older than 7 days:
robocopy "C:\Source" "D:\Backup" /S /MAXAGE:7 /MOV /LOG:C:\Logs\backup.log
Scheduling Robocopy
Using the /RH option, you can tell robocopy to only run during a specific time. This is great if you have a maintenance window or a time when everyone has gone home for the day.
You can specify a start time and an end time in the format HHMM-HHMM. For example, to invoke robocopy but only allow it to run between the hours of 5PM and 9AM as defined by the system clock, run:
robocopy "C:\Databases" "D:\Backup" /E /S /ZB /R:5 /W:5 /RH:1700-0900
You’ll see that if you invoke robocopy outside of those hours, it will tell you the current time and wait for the start time to run.
-------------------------------------------------------------------------------
ROBOCOPY :: Robust File Copy for Windows
-------------------------------------------------------------------------------
Started : Monday, June 19, 2023 1:43:58 PM
Source : C:\Databases\
Dest : D:\Backup\
Files : *.*
Options : *.* /S /E /DCOPY:DA /COPY:DAT /ZB /R:5 /W:5 /RH:1700-0900
------------------------------------------------------------------------------
Hours : Paused at 13:43 until 17:00...
Alternatively, you can use the Task Scheduler to create a task to run it automatically.
To use Robocopy, open the command prompt and type “robocopy [source] [destination] [file name or extension]”. For example, to copy all .txt files from the C: drive to the D: drive, you would type “robocopy C:\ D:\ *.txt”. Robocopy is a powerful command-line tool that can handle complex file-copying tasks and preserve file attributes.
Alternatively, you could consider using efficient solution tools like Gs Richcopy 360 and Teracopy. These tools have a user-friendly interface and are designed to handle large file transfers efficiently. They offer features such as parallel transfers, pause and resume options, and error recovery mechanisms, making file copying tasks faster and more reliable.