Use the robocopy command with the /E or /S switch to recursively copy all subdirectories from a source folder to a destination. This built-in Windows utility handles complex directory structures efficiently, preserving attributes and timestamps while skipping empty folders if desired.
Understanding Robocopy Basics
Robocopy (Robust File Copy) is a command-line directory replication tool included in Windows Vista and later versions, as well as Windows Server operating systems. It is significantly more robust than the traditional copy or xcopy commands, offering better error handling, multi-threaded copying, and resume capabilities. For tech support professionals, mastering this tool is essential for migrating data, backing up systems, or synchronizing large file sets across network shares.
The basic syntax requires at least three arguments: the source path, the destination path, and optionally, a list of files to copy. If no file specification is provided, Robocopy defaults to copying all files. The command structure is straightforward:
robocopy <Source> <Destination> [file[s]...]
While simple copies are possible, the true power of Robocopy lies in its extensive array of switches that control how data is transferred. Without these switches, Robocopy only copies files and does not replicate the directory structure by default. To achieve recursive copying, you must explicitly instruct the tool to include subdirectories.
Recursive Copying with /E and /S
To copy an entire folder tree, including all subdirectories, you need to use one of two primary switches: /E or /S. Understanding the difference between them is critical for ensuring your backup or migration process works as intended.
- /E: Copies all subdirectories, including empty ones. This is the most common choice when you want an exact mirror of the source structure, regardless of whether the folders contain files.
- /S: Copies subdirectories but excludes empty directories. Use this if you want to clean up the destination by removing folder structures that do not hold any data.
For example, to copy every file and folder from C:\Data\Projects to D:\Backup\Projects, including empty folders, you would run:
robocopy C:\Data\Projects D:\Backup\Projects /E
If you prefer to omit empty folders from the destination, replace /E with /S. This distinction prevents clutter in your target drive while still moving all substantive data. Many technicians find /E safer because it preserves the exact hierarchy, which can be useful for restoring specific nested configurations later.
Essential Flags for Reliable Transfers
Beyond recursion, several other flags are standard practice in professional tech support workflows. These options ensure data integrity, handle network interruptions gracefully, and provide detailed logging for troubleshooting.
The /MIR flag mirrors a directory tree. It is equivalent to using /E combined with /PURGE, which deletes files and directories in the destination that no longer exist in the source. Be extremely cautious with /MIR as it can inadvertently delete data on the target drive if used incorrectly. For safe recursive copying without deletion, stick to /E.
Adding /Z enables restartable mode. If the copy operation is interrupted due to a network drop or system crash, you can rerun the same command, and Robocopy will resume from where it left off rather than starting over. This is invaluable for large transfers over unstable connections.
Logging is another crucial component. Using /LOG+:path appends output to a text file, allowing you to review exactly what was copied, skipped, or failed after the job completes. This is vital for audit trails and diagnosing why certain files might not have moved.
Handling Permissions and Attributes
By default, Robocopy attempts to preserve security settings (ACLs), timestamps, and file attributes. If you are copying between different domains or user profiles, you might encounter access denied errors. In such cases, adding /COPY:DAT limits the copy to Data, Attributes, and Timestamps, skipping Security descriptors. This often resolves permission issues while maintaining file integrity.
For more information on measuring performance during these transfers, see our guide on How to Measure Robocopy's Copy Speed and Files Skipped/Copied?. Monitoring speed helps identify bottlenecks in your storage infrastructure.
Common Scenarios and Best Practices
Tech support scenarios often involve specific constraints. For instance, copying to a portable USB drive may result in unexpected hidden or system folders appearing. If you encounter issues where the destination becomes corrupted or filled with system files, refer to our article on Unaccountably, Folder Robocopied to Portable USB Drive on Laptop PC Becomes System + Hidden - What to Do? for troubleshooting steps.
Another common concern is preventing accidental overwrites. Robocopy has sophisticated logic for handling existing files. By default, it skips files that already exist in the destination. If you need to force overwrites, use /XF to exclude specific files or adjust the copy behavior with /FFT for FAT time tolerance. However, for general recursive backups, the default skip behavior is usually preferred to avoid losing newer data on the target.
When dealing with non-empty folders that need to be moved rather than copied, be aware that Robocopy’s move functionality (/MOVE) will fail if the destination folder contains items. Always ensure the destination root is empty before initiating a move operation to avoid errors related to deleting non-empty folders.
Frequently Asked Questions
Does Robocopy copy empty folders by default?
No, Robocopy does not copy empty folders by default. You must explicitly add the /E switch to include all subdirectories, even those that are empty. Without this switch, only files and their immediate parent directories are replicated.
What is the difference between /E and /MIR?
The /E switch copies all subdirectories including empty ones but does not delete anything from the destination. The /MIR switch mirrors the source to the destination, meaning it deletes files and folders in the destination that no longer exist in the source. Use /MIR with caution as it is destructive to the destination.
How can I resume a failed Robocopy transfer?
Add the /Z switch to your command line. This enables restartable mode, which allows Robocopy to pick up where it left off after a network interruption or system reboot. Without /Z, the tool typically restarts the entire copy process from the beginning.
Why does Robocopy skip some files during copying?
Robocopy skips files that already exist in the destination and have identical or newer timestamps. This is designed to prevent unnecessary overwrites. If you need to overwrite existing files, you must use additional switches like /XO (exclude older) removal or simply accept that the default behavior protects newer data.
Can Robocopy preserve file permissions during the copy?
Yes, by default Robocopy attempts to preserve security descriptors (ACLs). However, if you lack administrative privileges or are copying across different domains, this may cause errors. You can control this behavior using the /COPY flag, specifying which attributes to copy, such as /COPY:DAT for data, attributes, and timestamps only.