Sub topic 3
Copying data: copy, xcopy, and robocopy
This chapter assumes you can open Command Prompt, move between folders with cd, and read a command's help with /?. If you cannot, review the earlier chapters on Command Prompt basics and folder navigation first.
Windows ships three command-line tools for copying files: copy, xcopy, and robocopy. They overlap, and the A+ Core 2 exam expects you to know which one fits a given job.
The three tools at a glance
copy is a built-in command of the Command Prompt interpreter. It handles single files or a group of files in one folder. xcopy is an older external program that adds support for subfolders and some attributes. robocopy ("Robust File Copy") is the current tool for large or repeated jobs. It retries on failure, copies in a way that survives network interruptions, and can mirror whole folder trees.
| Feature | copy | xcopy | robocopy |
|---|---|---|---|
| Copies subfolders | No | Yes, with /S or /E | Yes, with /S or /E |
| Copies empty folders | No | Yes, with /E | Yes, with /E |
| Retries on error | No | No | Yes, with /R and /W |
| Mirrors a tree (deletes extras at the destination) | No | No | Yes, with /MIR |
| Typical use | One file or a few files | Folder trees in older scripts | Backups, migrations, large copies |
copy: simple file copies
The syntax is copy source destination. If the destination is a folder, the file keeps its name. If the destination is a file name, the copy is renamed.
copy C:\Data\report.docx D:\Backup\
copy C:\Data\report.docx D:\Backup\report-old.docx
copy C:\Data\*.txt D:\Backup\The first line copies one file and keeps its name. The second renames the copy. The third uses the wildcard * to copy every .txt file in C:\Data, but not files in subfolders.
Two behaviors cause common mistakes:
copyoverwrites an existing file at the destination and asks for confirmation first. Add/Yto suppress the prompt, or/-Yto force it.copydoes not copy folders. If you give it a folder as the source, it copies only the files directly inside, and it skips subfolders.
Because copy does not preserve security settings such as NTFS permissions on the copied file, the new file inherits permissions from the destination folder. Remember this for questions about permissions after a copy.
xcopy: folder trees
xcopy copies files and directory trees. The switches below appear most often.
| Switch | Meaning |
|---|---|
| /S | Copy subfolders, but skip empty ones |
| /E | Copy subfolders, including empty ones |
| /H | Include hidden and system files |
| /K | Keep the read-only attribute |
| /O | Copy ownership and permission (ACL) information |
| /X | Copy audit settings (implies /O) |
| /Y | Overwrite without asking |
| /D | Copy only files that are newer than the destination copy |
An example that copies a whole folder tree, including empty and hidden items, without prompts:
xcopy C:\Data D:\Backup /E /H /K /YIf the destination does not exist, xcopy asks whether it is a file or a directory. Adding a trailing backslash (D:\Backup\) or the /I switch tells it the destination is a directory and avoids the question.
Microsoft has marked xcopy as superseded by robocopy. It still works on current versions of Windows, and old scripts and exam questions still use it.
robocopy: the tool for serious jobs
The syntax is robocopy source destination [files] [options]. Note that source and destination are folders, not files. To copy specific files, name them after the two folders.
robocopy C:\Data D:\Backup /E /R:2 /W:5This command copies C:\Data and all subfolders, including empty ones (/E). It retries a failed file twice (/R:2) and waits 5 seconds between retries (/W:5).
The default retry settings are 1,000,000 retries with a 30-second wait. A locked file can therefore stall a job for a very long time, so set /R and /W yourself.
Useful robocopy options
| Option | Meaning |
|---|---|
| /E | Copy subfolders, including empty ones |
| /S | Copy subfolders, excluding empty ones |
| /R:n | Number of retries on failed copies |
| /W:n | Seconds to wait between retries |
| /MIR | Mirror: make the destination match the source, including deleting extra files |
| /Z | Restartable mode, which resumes an interrupted copy of a large file |
| /COPYALL | Copy data, attributes, timestamps, security, owner, and auditing information |
| /XD | Exclude the named directories |
| /XF | Exclude the named files |
| /L | List only: show what would happen without copying anything |
| /LOG:file | Write the output to a log file |
Robocopy returns an exit code that is a combination of flags, not a simple pass or fail. Codes below 8 mean the run did not hit a failure. For example, 0 means nothing needed copying, 1 means files were copied, and 8 or higher means at least one failure occurred. A script should test for a value of 8 or more, not for non-zero.
Why /MIR needs care
/MIR makes the destination an exact copy of the source. Any file in the destination that is not in the source is deleted. If you swap source and destination by mistake, you can erase the data you meant to protect. Run the command with /L first to preview the actions.
robocopy C:\Data D:\Backup /MIR /L
robocopy C:\Data D:\Backup /MIR /R:2 /W:5 /LOG:C:\Logs\backup.logThe first command only lists what would happen. Run the second only after the preview looks correct.
Choosing between them
Common errors
- Access denied. The command needs an elevated Command Prompt, or your account lacks permission on the source or destination. Open Command Prompt as administrator and try again.
- Files missing after xcopy. Hidden and system files are skipped unless you add
/H. Empty folders are skipped with/S, so use/E. - Robocopy copies the wrong thing. Source and destination are folders. Giving it a file path as the source produces an error.
- Permissions change after copying.
copyand plainxcopydo not carry NTFS permissions. Usexcopy /Oorrobocopy /COPYALLwhen the permissions must survive. - Robocopy seems to hang. A locked file with the default retry settings keeps it waiting. Set
/R:2 /W:5.
Exercises
Exercise
A technician must copy C:\Projects, including empty subfolders, to an external drive E:\ and wants the command to retry a locked file only 3 times with a 10-second pause. Write the command.
Show answer
robocopy C:\Projects E:\Projects /E /R:3 /W:10
Exercise
A user runs copy C:\Data D:\Backup, where C:\Data contains several subfolders. Only the files directly in C:\Data arrive. Why, and which command fixes it?
Show answer
copy does not copy subfolders. Use xcopy C:\Data D:\Backup\ /E, or robocopy C:\Data D:\Backup /E, to copy the whole tree including empty folders.
Exercise
Before running robocopy C:\Data D:\Backup /MIR, how can you preview what it will do, and why does it matter?
Show answer
Add /L to list the actions without copying or deleting anything. It matters because /MIR deletes files in the destination that are not in the source, so a reversed source and destination could erase the original data.