Copy Files and Folders in Windows CMD with Copy and Xcopy
In this tutorial, we will learn how to copy files and folders in the Windows Command prompt.
We are going to look at two cmd commands: Copy and Xcopy.
Note that the copy
command has some limitations compared to the xcopy
. For example, to copy directories or hidden files, you have to use the xcopy
command.
Copy Command
On Windows, we can use the copy
command to copy one or more files from one location to another:
copy C:\data\sales.doc C:\backup
The preceding command will copy sales.doc
from C:\data\
to C:\backup
.
Use the /y
switch to overwrite duplicate files without confirmation:
copy /y C:\data\sales.doc C:\backup
We can also save a file to a different name. For example, the following command saves file1.tx
t as file2.txt
in the same directory:
copy file1.txt file2.txt
You can also use wildcards to copy multiple files:
copy /y C:\data\* C:\backup
copy /y C:\data\*.doc C:\backup
The first command copies all files in the C:\data\
directory to C:\backup
. The second command copies all files with a .doc
extension to the C:\backup
.
We can also combine several files into one:
copy file1+file2 file3
copy error* C:\backup\report.txt
In the first line, file1 and file2 are combined to make one file named file3. In the second line, all files whose names start with "error" are copied to the C:\backup
, as a single file called report.txt.
You can get a list of all available options with the copy /?
command.
Xcopy Command
The xcopy
command offers more features. For example, with xcopy
, we can copy directories and subdirectories, as well as hidden files.
Command Options
/Y | Prompt before overwriting an existing file. |
/H | Copy hidden files/system files. |
/S | Copy directories and subdirectories. Empty directories are not included by default (use /e for that). |
/E | Include empty directories. |
/I | Create the destination folder if it does not exist. Use this option when you want to copy the parent folder itself. |
/T | Copy directory tree without files. Empty directories are not included by default. Use /e option to include empty folders. |
/P | Prompt for confirmation before creating each file. |
/Q | Quiet mode. |
/exclude | Specify a text file that contains a list of files to exclude. See the examples. |
/Z | Resume mode. Use this option when copying files over a network. |
/D:m-d-y | Copies files changed on or after the specified date. |
Examples of Using the Xcopy Command
Copy sales.doc from the current directory to C:\backup
:
xcopy sales.doc C:\backup
Copy C:\data\accounts
(all files including subdirectories) to C:\backup
:
xcopy /s /e /h /i /y C:\data\accounts C:\backup\accounts
In the following example (without /I
switch), the contents of the folder are copied but not the folder itself:
xcopy /s /e /h /y C:\data\accounts C:\backup\
Copy the directory structure of C:\OneDrive
to the backup directory:
xcopy /s /e /t /y C:\OneDrive C:\backup\
You can use wildcard characters to match patterns. The following command copies all files with a .jpg
extension:
xcopy /s /h /y C:\data\*.jpg C:\backup
Using for
loop to copy multiple files:
for %i in (sales.doc, products.doc) do xcopy /y %i C:\backup
Excluding files with xcopy
With the /exclude
, we can provide a text file that contains items we want to exclude.
xcopy /s /e /h /y /exclude:C:\Users\user1\files-to-exclude.txt C:\data\ C:\backup\
The files-to-exclude.txt
may look like the following:
.doc
sales*
In this example, we exclude items with the .doc
extension and files whose name starts with sales.
You can get a list of all available options with the xcopy /?
command.