Windows OS has a default copy command but
Xcopy has a number of advanced features which make it more useful than just Copy.
In the following table, you can find a list of the additional features offered by Xcopy.
Xcopy Command Examples & Syntax
1) Directories can be copied using Xcopy.
2) A recursive copy of a file and all its subdirectories can be performed by Xcopy in order to copy a source directory structure untouched.
3) The file name or extension can be used to exclude files from Xcopy.
4) As a result, it’s useful for incremental backups (based on archive attributes or cutoff dates).
Xcopy command to copy folders and subfolders [Files]:
Xcopy /I Source_Directory Destination_directory
If you choose this option, you would only copy the files immediately contained in the source directory to the destination. A copy of files in a subdirectory will not be performed. This switch will prevent the user from being asked (Is there a directory or file name specified on the target for the destination directory?) Read: Windows Copy Command Syntax & Examples
The following steps can be used to recursively copy a directory and its subdirectories:
Xcopy /S /I /E Source_Directory Destination_directory
It is for this reason that we need to run the following command if we would like to copy all the documents from
Xcopy /S /I /E D:\data\documents E:\Newfolder\documents
In the event that the folder
E:/Newfolder/documents do not already exist, the command creates it. The same structure is also created in the destination folder. The /I switch does not need to be added if the destination folder already exists. Unless you specify /I, a confirmation dialog will ask you if the destination is a directory or a filer. Copying files unattended or automatically would leave you in a bind.
Exclude files – Xcopy:
With Xcopy, you can exclude certain files from a copy of a directory. Use the
/EXCLUDE switch if you want to exclude files. In order to exclude files, you must list them in one file, and you must specify this file using the
Xcopy /I Sourcedir Destdir /EXCLUDE:filename
Take for example the directory
D:/docs and copy it to
E:/nextdocs/ without copying the pdf and mp3 files:
C:\>type 1.txt .pdf .mp3 C:\>Xcopy D:\docs E:\newdocs /EXCLUDE:1.txt
As well as specifying the full name of the files, you can specify their extension. File names/patterns should be separated by commas.
Duplicate hidden, system, and other files:
By excluding hidden and system files, the above command copies only the files that are visible. Adding the /H option to your command will enable you to include these files. As a result, I would issue a command.
Xcopy /S /I /E /H D:\data\documents E:\Newfolder\documents
With the /E option, empty subdirectories are copied to the destination folder. Additionally, if you add the /H option, hidden and system files will also be copied to the destination folder. You need to add the option /Y if you are overwriting an existing file so you won’t need to confirm that you want to do that.
Copies files from an archive based on its attributes:
By using the /A and /M switches, you can copy only files that have archive attributes set. If the archive attribute has not been reset, then the file has been modified.
To do this, you need to:
Xcopy /A /I /S source_directory destination_directory.
The archive attribute is maintained by the above command; it is not reset. The /M switch can be used instead of
/A to reset the archive attribute.
As a result, the command would be:
Xcopy /M /I /S source_directory destination_directory
You can also use the above commands from batch files. There are editions of Windows 7 that support this command.
Using the modified date to copy files:
Xcopy /D:dd-mm-yy /I sourcedir destinationdir
In this example, all the files that have been modified since 1 st February 2011 should be copied to the directory
Xcopy /D:01-02-11 /I E:\data E:\backup
Windows 10, 7, and 8 all come with Xcopy. I have verified that it works with Windows 10 in particular.
You may also like: Copy Directory From Command Line [Step by Step]