Creating a copy of a folder that contains other files and/or folders is a common practice and even a recommended one at that, especially for backup and redundancy. In Windows, one of the simplest and recommended ways to create such a copy in the command line is by using the robocopy utility.
Command
To copy a folder and include all sub-files and folders, it's best to use the following syntax:
robcopy <source> <destination> /copyall /e /r:2 /w:5
To explain each part of the command:
<source> is the location where you want to copy files from
<destination> is the location where you want to copy files to
- The
/copyall flag will instruct it to preserve all file information including attributes, timestamps, owner, permission, and audit information.
- The
/e flag will tell it to include all sub-folders and files.
- The
/r:2 flag will instruct it to retry 2 times in case of a copy error. The number can be adjusted as needed, or the option omitted entirely if you prefer. As a side note, if you choose to omit this value, the utility will [at the time of writing] default to one million retries every 30 seconds on a copy error which is not likely to be desired.
- The
/w:5 flag will instruct it to wait 5 seconds between retries. The number can be adjusted as needed, or the option omitted entirely if you prefer. As a side note, if you choose to omit this value, the utility will [at the time of writing] default to one million retries every 30 seconds on a copy error which is not likely to be desired.
Example
As an example, if one wished to backup the contents of C:\Users to the location D:\Backups\2003-05 , they could use the following command:
robocopy c:\users d:\backups\2003-05 /copyall /e /r:2 /w:5
Additional Options
The following flags can be added as desired to the command:
/v to produce verbose output, showing skipped files.
/log:<filename> to save the output to the given file name, for example: /log:robocopy.log .
/tee to output to the console window as well as the log file.
|