Robocopy Examples
Robocopy Examples
Robocopy has many command line options and it can be overwhelming to know which commands to use.
In this post, we will take a look at how to ues robocopy to copy, mirror, purge Files and Folders.
Useage:
Usage :: ROBOCOPY source destination [file [file]...] [options]
source :: Source Directory (drive:\path or \\server\share\path).
destination :: Destination Dir (drive:\path or \\server\share\path).
file :: File(s) to copy (names/wildcards: default is "*.*").
Naming Convention:
- Source - src
- Destination - dst
1. Test File Copy Without Changes
Test a file copy with the /L
command.
/L :: List only - don't copy, timestamp or delete any files.
robocopy c:\src c:\dst /l
Output:
robocopy D:\DCIM C:\Users\rnall\Pictures\!Sort\DCIM /L
-------------------------------------------------------------------------------
ROBOCOPY :: Robust File Copy for Windows
-------------------------------------------------------------------------------
Started : Saturday, February 11, 2023 3:01:28 PM
Source = D:\SRC\
Dest : C:\DST
Files : *.*
Options : *.* /L /DCOPY:DA /COPY:DAT /R:1000000 /W:30
------------------------------------------------------------------------------
0 D:\DCIM\
------------------------------------------------------------------------------
Total Copied Skipped Mismatch FAILED Extras
Dirs : 1 0 1 0 0 0
Files : 0 0 0 0 0 0
Bytes : 0 0 0 0 0 0
Times : 0:00:00 0:00:00 0:00:00 0:00:00
Ended : Saturday, February 11, 2023 3:01:28 PM
Another test and include the /e
option.
/E :: copy subdirectories, including Empty ones.
robocopy c:\src c:\dst /e /l
Output:
... TRUNCATED OUTPUT ...
New File 12.1 m 20200614_103501.jpg
New File 7.3 m 20200614_103503.jpg
New Dir 16 D:\SRC\Camera\Move\
New File 2.0 m 19700102_010635.jpg
New File 455720 20121021_195230.jpg
... TRUNCATED OUTPUT ...
------------------------------------------------------------------------------
Total Copied Skipped Mismatch FAILED Extras
Dirs : 4 1 3 0 0 0
Files : 2639 1731 908 0 0 0
Bytes : 12.844 g 8.655 g 4.189 g 0 0 0
Times : 0:00:02 0:00:00 0:00:00 0:00:02
Ended : Saturday, February 11, 2023 3:05:33 PM
2. Copy Contents Exclude Empty Subdirectories
Copy all the files and folders from c:\src
to the folder c:\dst
. This will exclude any empty subdirectories.
/S :: copy Subdirectories, but not empty ones.
robocopy c:\src c:\dst /s
3. Copy Contents Include Empty Subdirectories
Copy all the files and folders from c:\src
to the folder c:\dst
. This will include any empty subdirectories.
/E :: copy subdirectories, including Empty ones.
robocopy c:\src c:\dst /e
4. Copy Contents With Security (ACL Permissions)
Keep permissions with /copy:DATS option. This does not copy owner information.
/COPY:copyflag[s] :: what to COPY for files (default is /COPY:DAT).
robocopy c:\src c:\dst /e /copy:DATS
Verify file permissions clis icacls
icacls Music
Music SNOW\grawity:(I)(F)
CREATOR OWNER:(I)(OI)(CI)(IO)(F)
SNOW\grawity:(I)(OI)(CI)(IO)(F)
NT AUTHORITY\SYSTEM:(I)(OI)(CI)(F)
(OI)
- object inherit
files will inherit this ACE
(CI)
- container inherit
containers – i.e. folders – will inherit this ACE
(IO)
- inherit only
Show owner with dir /Q c:\src
5. Copy Files With Security Including Owner
To include the owner of a file or folder use the /copy:DATSO command.
robocopy c:\src c:\dst /e /copy:DATSO
6. Copy All File Information
Copy all file properties use /copyall
, this is equivalent to /copy:DATSOU
. Based on preference to use /copyall
instead of listing each property type.
/COPYALL :: COPY ALL file info (equivalent to /COPY:DATSOU).
robocopy c:\src c:\dst /e /copyall
Copy the following file properties:
- D – Data
- A – Attributes
- T – Timestamps
- S – NTFS access control list (ACL)
- O – Owner information
- U – Auditing information
7. Mirror Source and Destination Directory
Mirror the source and destination directory.Changes made in the source directory will be mirrored in the destination. For example, add/remove a folder in the source, the same will occur in the destination.
/MIR :: MIRror a directory tree (equivalent to /E plus /PURGE).
robocopy c:\src c:\dst /e /mir
8. Mirror Directory With All File Properties
Does not mirror the source directory file properties (ACL, owner, timestamp, etc). To mirror and include file properties use this command.
robocopy c:\src c:\dst /e /mir /copyall
9. Purge Files and Folders From The Destination Directory
Deleted a bunch of files/folders from the source and want to remove them from the destination folder, use the /purge option.
robocopy c:\src c:\dst /e /purge
10. Copy Files Over The Network
Copying files over the network is the same as copying locally. There are some additional commands that can help with the transfer of files over the network.
/E :: copy subdirectories, including Empty ones.
/Z :: copy files in restartable mode.
robocopy c:\src \\srv\dst /z /e
11. Copy Files Over the Network with File Properties
Copy the ACL and other file properties use /copyall
. To copy just the ACL use /copy:DATS
.
robocopy c:\src \\srv\dst /z /e /copyall
12. Save Output to a Log File
Write the robocopy output to a log file use /log:logfile
. This will overwrite the log file each time the command is run .
robocopy c:\src \\srv\dst /z /e /log:c:\logs.txt
13. Append To An Existing Log File
Use the /log+:logfile
command to append an existing log file. This will be added to the log file each time robocopy is ran.
robocopy c:\src \\srv\dst /z /e /log+:c:\logs.txt
14. Multi-threaded File Copy (Increase Copy Speed)
By default, robocopy will use 8 threads. To increase the threads use the /mt
command. The below example will use 32 threads.
robocopy c:\scr \\srv\dst /z /e /mt:32
15. Include Verbose Logging
Verbose logging will show skipped files. If copying critical files or migrating to a new server include verbose logging in the output or even better, include it in a log file.
/V :: produce Verbose output, showing skipped files.
robocopy c:\src \\srv\dst /v
Include verbose logging with a log file.
robocopy c:\src \\srv\dst /v /log:c:\logs.txt
18. Copy Files That Start With
Copy files that start with a specific character or word. For example, copy all files that start with the word “file”.
robocopy C:\src \\srv\dst file*
19. Exclude a Directory
To exclude a directory use the /XD
option.
robocopy C:\src \\srv\dst /XD /e "c:\src\accounting"
20. Copy Files That are at least 14 Days Old
This will copy files that have a timestamp that is at least 14 days old.
robocopy C:\src \\srv\dst /minage:14
21. Copy Files with Specific Size
This will copy files that do not exclude 2000 bytes.
robocopy C:\src \\srv\dst /S /MAX:2000
Summary
That is a good start. To learn more about the many robocopy commands check out the Microsoft Syntax documentation.
Next in the Series:
Related Posts
2023 Phoenix VMUG UserCon
Introduction: The recent 2023 Phoenix VMUG UserCon brought together some like-minded people in the field, with discussions ranging from VMware technologies to best practices for optimizing existing systems.
Read moreRed Hat User Group Insights, Ansible Automation Platform, and ITSM Integration
Introduction: This blog post aims to summarize the key takeaways from this informative workshop. At the recent Red Hat User Group workshop on Red Hat Insights, Red Hat Ansible Automation Platform, and their integration with management (ITSM) systems, such as ServiceNow, provided valuable insights into how these technologies work together.
Read moreBackup Website to Amazon S3 Shell Script
Getting Started Amazon Simple Storage Service (Amazon S3) is an cloud based object storage device. It is a low cost storage widely used for the backup or static website content.
Read more