How to use the command 'robocopy' (with examples)
- Windows
- December 25, 2023
Robocopy is a command-line tool in Windows that allows users to efficiently copy or synchronize files and directories. It offers a wide range of options, including filtering files, copying attributes and permissions, and handling errors during the copy process. Below are several use cases of the robocopy command.
Use case 1: Copy specific file types from one directory to another
Code:
robocopy path\to\source_directory path\to\destination_directory *.jpg *.bmp
Motivation:
Copying specific file types can be useful if you only want to transfer certain types of files. In this example, we are copying all .jpg
and .bmp
files from the source directory to the destination directory.
Explanation:
path\to\source_directory
: Specifies the path to the source directory.path\to\destination_directory
: Specifies the path to the destination directory.*.jpg
and*.bmp
: Specifies the file types to be copied.
Example output:
1 path\to\source_directory\example.jpg
1 path\to\source_directory\example.bmp
2 files copied
Use case 2: Copy all files and subdirectories, including empty ones
Code:
robocopy path\to\source_directory path\to\destination_directory /E
Motivation: Copying all files and subdirectories, including empty ones, can be useful when you want to replicate the entire directory structure from the source to the destination directory.
Explanation:
/E
: Copies both files and subdirectories, including empty ones.
Example output:
1 path\to\source_directory\example.jpg
1 path\to\source_directory\subdirectory\example.txt
2 files copied
Use case 3: Mirror/Sync a directory, deleting anything not in source and include all attributes and permissions
Code:
robocopy path\to\source_directory path\to\destination_directory /MIR /COPYALL
Motivation:
Mirroring or synchronizing directories helps to ensure that the destination directory is an exact copy of the source directory. The /MIR
option deletes any files in the destination that are not present in the source, while /COPYALL
copies all attributes and permissions.
Explanation:
/MIR
: Mirrors the source directory to the destination directory, deleting anything not present in the source./COPYALL
: Copies all attributes and permissions.
Example output:
1 path\to\source_directory\example.jpg
1 path\to\source_directory\subdirectory\example.txt
2 files copied
Use case 4: Copy all files and subdirectories, excluding source files that are older than destination files
Code:
robocopy path\to\source_directory path\to\destination_directory /E /XO
Motivation:
When you want to update the destination directory with newer files from the source directory, excluding any files in the source that are older than their corresponding files in the destination, you can use the /XO
option.
Explanation:
/E
: Copies both files and subdirectories./XO
: Excludes source files that are older than the destination files.
Example output:
1 path\to\source_directory\example.jpg
1 path\to\source_directory\subdirectory\example.txt
2 files copied
Use case 5: List all files 50 MB or larger instead of copying them
Code:
robocopy path\to\source_directory path\to\destination_directory /MIN:52428800 /L
Motivation: Instead of copying, you may want to generate a list of files that are larger than a specified size. This can be useful for reviewing or performing further actions on those files.
Explanation:
/MIN:52428800
: Specifies the minimum file size in bytes (50 MB in this example)./L
: Lists the files that meet the specified criteria without actually copying them.
Example output:
1 path\to\source_directory\example.jpg
1 files listed
Use case 6: Allow resuming if network connection is lost and limit retries to 5 and wait time to 15 sec
Code:
robocopy path\to\source_directory path\to\destination_directory /Z /R:5 /W:15
Motivation:
In scenarios where network connections are unreliable, the /Z
option allows robocopy to resume copying from where it left off if the connection is lost. The /R
option limits the number of retries, and the /W
option sets the wait time between retries.
Explanation:
/Z
: Allows resuming the copy operation from the point of failure./R:5
: Limits the number of retries to 5./W:15
: Specifies the wait time between retries in seconds (15 seconds in this example).
Example output:
1 path\to\source_directory\example.jpg
1 path\to\source_directory\subdirectory\example.txt
2 files copied
Use case 7: Display detailed usage information
Code:
robocopy /?
Motivation:
When you need quick access to detailed usage information, the /?
option provides a comprehensive list of all available options and their descriptions.
Example output:
-------------------------------------------------------------------------------
ROBOCOPY :: Robust File and Folder Copy
-------------------------------------------------------------------------------
...
------------------------------------------------------------------------------
Conclusion:
The robocopy command offers powerful capabilities for efficiently copying or synchronizing files and directories in Windows. With a variety of options available, users can customize their copy operations to suit their specific needs. Whether it’s copying specific file types, mirroring directories, or handling network interruptions, robocopy is a versatile tool for managing file transfers.