There are three primary commands that are useful for copying files.
Command | Description |
---|---|
Copy | Basic file copy command. You can use this when you want to merge multiple files into one. |
xcopy | Extended copy command. You can use this when you want to include subdirectories. |
robocopy | Robust file copy command. You can use this to copy folder structures and include existing permissions. |
copy
You can use the copy command for basic file copying. The format is
copy source [destination]
It can also combine multiple files into a single file. The format when copying multiple files is
copy sourceFile1 + sourceFile2 [+ sourceFileN] destinationFile
Note
You can include as many source files as desired.
The following table shows some examples using the copy command.
copy command | Comments |
---|---|
c:\>copy test.txt test2.txt
|
Creates a new file named test2.txt from an existing source file named test.txt. |
c:\>copy test1.txt
+ test2.txt
combined.txt
|
Combines test1.text and test2.txt into a single file named combined.txt. The entire contents of test1.txt are at the beginning of the file and the entire contents of test2.txt are at the end. |
c:\>copy *.txt
combined.txt
|
Copies all the .txt files in the current directory into a single file named combined.txt. |
c:\data>copy *.*
c:\archive
|
Copies all files from the current folder (c:\data in the example) to the target folder (c:\archive in the example). The *.* wildcards indicate that all files should be copied. |
xcopy
xcopy extends the basic copy command and provides additional capabilities. The most notable capability with xcopy is the capability to copy subdirectories. The basic format of the command is
xcopy source destination
xcopy Command | Comments |
---|---|
/s
c:\>xcopy c:\data d:\data /s
c:\>xcopy c:\data d:\data\ /s
|
The /s switch copies all the files, directories, and subdirectories from the source to the destination. The example copies all the files on the c: drive to the d: drive. Use the trailing backslash (\) on the destination if the destination folder doesn’t exist. If the trailing backslash on the destination is omitted (d:\data instead of d:\data\) and the destination folder doesn’t exist, you are prompted to send the data to a file or a directory. If you select a file, all the data is combined into a single file. If you choose directory, the entire folder structure is re-created. |
/e
c:\>xcopy c:\data d:\data\ /s /e
|
The /e switch includes empty folders. This copies the entire contents of the c:\data folder, including all empty subfolders, to the d: drive. |
/t
c:\>xcopy c:\data d:\data\ /s /e /t
|
The /t switch is used to copy the directory structure only. Files are not copied. |
/y
c:\>xcopy c:\data d:\data\ /s /e /y
|
The /y switch suppresses prompting to overwrite files. This is useful in scripts when you want the process automated without prompting you to take action. If the destination folder exists and the /y switch is omitted, xcopy prompts the user to overwrite the folder. |
robocopy
robocopy is a robust file copy command. It can do everything that xcopy can do and more, including copy permissions. The basic syntax is
robocopy sourceDirectory destinationDirectory
One of the most valuable features of robocopy is the capability to copy metadata associated with a file. You identify the metadata you want to copy by using specific flags, as shown in the following table. Any of the flags can be used with the /copy switch to specify what metadata to copy.
robocopy Metadata Flag | Description |
---|---|
d
c:\>robocopy c:\data
d:\data\ /copy:d
|
Data flag. The d flag identifies the file itself. |
a
c:\>robocopy c:\data
d:\data\ /copy:da
|
Attributes flag. This includes attributes such as hidden or read-only. |
t
c:\>robocopy c:\data
d:\data\ /copy:dat
c:\>robocopy c:\data
d:\data\
|
Timestamps flag. Timestamps include when the file was created and when it was modified. The d, a, and t flags are used by default. In other words, the two example commands are the same. |
s
c:\>robocopy c:\data
d:\data\ /copy:dats
|
Security flag. This includes all of the NTFS access control lists (ACL). In other words, it includes all of the assigned permissions. |
o
c:\>robocopy c:\data
d:\data\ /copy:datso
|
Owner flag. This flag enables you to retain the original owner of the file. If this isn’t used, the owner of the copied file is the user who executes the command. |
u
c:\>robocopy c:\data
d:\data\ /copy:datsou
|
Auditing information flag. This includes all of the security access control lists (SACL) that identify auditing information for files and folders. |
The following table shows some common uses of the robocopy command.
Common robocopy Commands | Description |
---|---|
c:\>robocopy c:\data
d:\data\
c:\>robocopy c:\data
d:\data\ /copy:dat
|
Copies all the files in the c:\data folder to the d: drive. It does not include subfolders. Note that this includes the data, the attributes, and the timestamps and is the same as using the /copy:dat switch. |
/s
c:\>robocopy c:\data
d:\data\ /s
|
The /s switch includes nonempty subfolders. It copies the entire contents of the c:\data folder to the d: drive. It includes all nonempty subfolders. |
/e
c:\>robocopy c:\data
d:\data\ /e
|
If you want to include empty folders, use the /e switch. It copies the entire contents of the c:\data folder (including nonempty subfolders) to the d: drive. Note that /s is implied but does not need to be included. |
/purge
c:\>robocopy c:\data
d:\data\ /e /purge
|
The /purge switch deletes files at the destination that no longer exist at the destination. |
/mir
c:\>robocopy c:\data
d:\data\ /mir
|
Using the /mir switch mirrors the source contents at the destination. This is the same as using the /e and /purge switches. |
/mov
c:\>robocopy c:\data
d:\data\ /mov
|
The /mov switch specifies that the files should be copied to the destination and then deleted from the source. This is similar to a cut and paste operation. It does not include subdirectories. |
/move
c:\>robocopy c:\data
d:\data\ /move
|
You can add an e to the /mov switch (/move) to include all subdirectories, including empty subdirectories. This works like /mov but includes all subfolders. |
/copy:copyflag(s)
c:\>robocopy c:\data
d:\data\ /copy:datsou
c:\>robocopy c:\data
d:\data\ /copy:all
|
The /copy flag enables you to include additional metadata in the copy operation. The /copy:datsou command copies all the metadata and works the same as /copy:all. |
c:\>robocopy c:\data
d:\data\ /copy:dats
c:\>robocopy c:\data
d:\data\ /sec
|
If you want to copy the permissions without the owner and auditing information, you can use the /copy:dats switch. This works the same as the /sec switch. |
The output of the robocopy command provides useful information. It shows how many files and folders were copied, whether any files were skipped, whether any failures occurred, the speed of the copy, and more, as shown in the following partial output:
C:\>robocopy c:\scripts e:\scripts\ /copy:datsou
-----------------------------------------------------------------------
ROBOCOPY :: Robust File Copy for Windows
-----------------------------------------------------------------------
Started : Sun Nov 07 18:36:22 2010
Source : c:\scripts\
Dest : e:\scripts\
Files : *.*
Options : *.* /COPYALL /R:1000000 /W:30
-----------------------------------------------------------------------
New Dir 8 c:\scripts\
100% New File 201 logon.vbs
100% New File 47 ipconfig.ps1
100% New File 482 shutdown.ps1
100% New File 321 createou.wsh
100% New File 182 createuser.wsh
100% New File 11298 processes.txt
-----------------------------------------------------------------------
Total Copied Skipped Mismatch FAILED Extras
Dirs : 1 1 0 0 0 0
Files : 8 8 0 0 0 0
Bytes : 12.2 k 12.2 k 0 0 0 0
Times : 0:00:00 0:00:00 0:00:00 0:00:00
Speed : 156637 Bytes/sec.
Speed : 8.962 MegaBytes/min.
Ended : Sun Nov 07 18:36:23 2010
As a reminder, you can capture the output of any command using the > symbol to redirect the output to a text file like this:
c:\>robocopy c:\scripts e:\scripts\ /copy:datsou > robooutput.txt