Xcopy is a Windows utility that allows you to make an exact duplicate of one or more files and folders in your computer. The two most common ways of using Xcopy are: 1) To move the original file elsewhere on your hard drive, 2) To create a backup copy of it. You can also use Xcopy for many other purposes like copying disk partitions, etc., but this guide will focus primarily on how to use Xcopy as tool for making backups
The “how to use xcopy to copy folders” is a command-line tool that allows users to copy files and folders. It can be used in many different ways, such as copying from one folder to another, or from a local drive to a remote server.
In Windows, the Xcopy program, which stands for extended copy, is a popular command-line utility for copying large quantities of data. Xcopy includes additional capabilities and switches than the typical copy command, giving you greater flexibility when copying or moving files and directories.
You’ll learn how to utilize Xcopy for a variety of file copy situations in this article. Creating a Copy of a Single File, several folders, setting filters or exclusions, and more are all possible with this tool. You will be able to utilize Xcopy in your day-to-day file copying chores by the Conclusion of this tutorial.
Let’s get started!
You’ll need the following items to follow along with the examples in this tutorial.
- A Windows PC (server or client). You don’t need to download anything extra since Xcopy is integrated into Windows. Windows 10 Build 1909 will be used in this tutorial.
- The command prompt or PowerShell are both supported by Xcopy, and this tutorial assumes that you already have one open.
The Xcopy Command: An Overview
Xcopy is a command-line program that has been included with Windows since version 98. The executable name of this utility is xcopy.exe, and it may be found in the percent WINDIR percent system32 subdirectory.
Xcopy is far more efficient in copying files and directories than the Windows copy command. Xcopy also provides additional settings, making it more customisable and allowing you to manage the file copy behavior.
As you go through this article, you will learn about numerous perks or advantages of utilizing Xcopy. However, some of the advantages of utilizing Xcopy are listed below.
- Larger groups of files and directories may be copied faster.
- Deployment of applications is made easier.
- The source directory structure may be replicated exactly as it is.
- Keep the owner and access control list (ACL) information while copying files.
- Read-only files should be copied and overwritten.
- Files may be excluded based on their name, extension, or path.
- Updated files may be identified, which is important for differential backups.
- Integrate with scripts and utilize them.
Xcopy, as fantastic as it is, is not without flaws. Even if these limits aren’t a deal-breaker, understanding what they are before plunging in might be beneficial.
- Open files cannot be copied.
- The Windows Volume Shadow Copy Service isn’t supported (VSS). Xcopy is not ideal for backing up live operating system volumes due to its lack of VSS support.
- When the length of the path plus the file name exceeds 254 characters, an insufficient memory error occurs.
Syntax Reference for Xcopy
Xcopy allows you to copy files and folders in a variety of ways. However, you must first learn its syntax and choices. There are a slew of settings that can alter the way Xcopy works. The tables that follow will go through each of these choices in depth to help you understand them.
The first path designates the source file(s), while the second path designates the destination file (s).
xcopy [source] [destination] [options]
- [source] – The file(s) to copy’s pathname (accepts wildcards). You’ll need to provide the disk, path, and files you’d want to transfer.
- [destination] – The new file’s pathname (s). If the destination path is not specified, the command will transfer the files to the same location as the source.
- [options] — Any of the choices listed in the tables below. Running the xcopy /? command or checking the Xcopy online manual will also show you these choices.
|/A||Files with the archive property should be copied.|
|/M||Copy files with the archive attribute enabled, then disable the attribute at the destination. When generating routine backups, choose this option.|
|/H||Hidden and system files and folders should be copied. N is the default value.|
|/D:mm-dd-yyyy||Only copies files that were modified on or after the specified date. The command will only copy files whose date/time is newer than the destination if this argument is not used.|
|/U||Only files that already exist at the destination are copied.|
|/S||Recursively copies folders and subfolders, omitting the empty one.|
|/E||Recursively copies folders and subfolders, including the empty one.|
|/EXCLUDE:file1[+file2] [+file3]…||Specify the text file(s) containing a list of strings that correspond to absolute file paths that should be ignored.|
|/I||This option expects that the destination is a directory and creates it if the destination does not exist. Whether you don’t provide this, the command will ask if the destination is a directory or a file.|
|/R||Read-only files are overwritten.|
|/T||Without copying the files, creates a recursive folder structure at the destination. Copying empty directories is not included in this option. Use this option in conjunction with the /E option to include empty directories.|
|/K||During the copy, the file characteristics are preserved. Otherwise, read-only attributes will be reset by the command.|
|/N||Files are copied using their short file names.|
|/O||Copies files, including information on ownership and access control lists (ACLs).|
|/X||File audit settings and ACL information are copied (implies using /O).|
Options for Copying
|/W||Before the copy process can begin, it requires a key push.|
|/P||Before generating each file, a prompt (Y/N) appears.|
|/Y||If the target file already exists, this option disables the confirmation to replace it.|
|/-Y||Prompts you to confirm that you want to overwrite an existing destination file.|
|/V||Checks sure the size of the source and destination files are the same.|
|/C||Allows the command to continue copying despite failures.|
|/B||Only copies the symbolic link, not the destination file.|
|/G||Copies encrypted data to a location where encryption isn’t supported.|
|/J||Without utilizing the buffer, copies files. When transferring really huge files, this choice comes very handy.|
|/Q||When copying, this option disables the display of file names.|
|/F||While copying, the complete source and destination file paths are shown.|
|/L||Only shows which files should be copied without actually copying them. This flag is helpful for evaluating the anticipated outcome of Xcopy commands with many arguments.|
|/Z||Copies files in a manner that may be restarted. When this option is selected, the command gracefully pauses copying files in the event of a network outage. After re-establishing the network connection, the copying will continue.|
Working with Xcopy: Examples of Use
Now that you’re acquainted with the Xcopy syntax and options, you can start putting them to use by combining one or more of them. In the following sections, you’ll learn how to use Xcopy to copy files and folders in a variety of settings.
Copying Files List
If you have a file server containing a huge amount of files that you want to copy, perhaps to a backup location, testing out your Xcopy command first would be ideal. One situation when you’d want to test Xcopy is when you’re combining multiple XOptions for Copying.
You may use the /L switch with Xcopy to mimic what would have occurred if you had given the command by listing the files that would be copied. This manner, you can double-check if your command copies all of the files you specified.
C:WorkareaXCopyDemo C:WorkareaXCopyDemoFolder /L XCOPY C:WorkareaXCopyDemo
Creating a Copy of a Single File
Copying a file from a source to a destination without using any arguments is the most basic operation you can do with Xcopy. The command below, for example, will transfer a text file to a target directory with the same name.
XCOPY C:WorkareaXCopyDemoNewFile.txt C:WorkareaXCopyDemoFolder.txt C:WorkareaXCopyDemoFolder.txt C:WorkareaXCopyDemoFolder.tx
You may also use the command below to copy the source file and alter the destination filename.
XCOPY C:WorkareaXCopyDemoNewFile.txt C:WorkareaXCopyDemoFolder.txt C:WorkareaXCopyDemoFolder.txt C:WorkareaXCopyDemoFolder.txAnotherFile.txt
Copying Files to a New Folder is a process that involves copying files to a new folder.
You can copy files and create a target directory on the fly using Xcopy. To do this, add the /I option to the Xcopy command.
The command below, for example, moves the contents of the C:WorkareaDemo folder to the D:Workarea folder. If the target folder does not already exist, Xcopy uses the /I option to create it.
C:WorkareaDemo D:Workarea /I XCOPY C:WorkareaDemo D:Workarea
Recursively copying all files and folders
Apart from transferring data from one location to another, Xcopy also allows you to recursively copy directories and files. You may make a recursive copy in two ways: with or without empty directories.
Add the /S option to the end of the Xcopy command to copy all files and directories while disregarding empty directories, as illustrated below.
However, if you want to include empty directories in the recursive copy, use the /E option instead.
Files that are hidden and system files are included.
Xcopy does not include hidden or system files in copy operations by default. Add the /H option to the command if you require Xcopy to include hidden and system files.
The command below copies all files recursively, Files that are hidden and system files are included.. This command also ignores errors, creates the destination folders, and overwrites existing files.
C:WorkareaDocuments D:WorkareaDocuments /S /H /C /Y XCOPY C:WorkareaDocuments D:WorkareaDocuments
The Read-Only File Attribute Must Be Preserved
The read-only property of a file is another characteristic that Xcopy can understand and keep. When Xcopy copies a read-only file, the read-only attribute is removed at the destination by default. Add the /K parameter to the command to prevent Xcopy from deleting the read-only property.
The command below, for example, moves a read-only file to another place, and the destination file retains the read-only characteristic. If the target file already exists, this command will replace it.
D:WorkareaXCopyDemoNewFile.txt XCOPY C:WorkareaXCopyDemoNewFile.txt /I /K /Y
As a result of keeping the file read-only, Xcopy won’t be able to overwrite it in future copy operations. By using the /R flag, you may compel Xcopy to overwrite read-only files.
C:WorkareaXCopyDemoNewFile.txt D:Workarea /K /R /Y XCOPY C:WorkareaXCopyDemoNewFile.txt
Filtering and Copying Files
Let’s say you wish to copy several files from the source folder and subfolders. You may use wildcards in Xcopy to filter which files you want to copy.
The command below, for example, recursively transfers just the files with the.cs extension from the root of the C: drive to the root of the E: disk. This command also ignores errors and overwrites existing files without asking for permission.
E: /S /C /Y XCOPY C:*.cs
Files and Folders Aren’t Included in the Copy
Xcopy’s ability to omit files from the copy process is a significant tool. The /EXCLUDE option may be used to enable this capability. The names of the file(s) containing the exclusion lists are accepted by this switch.
To begin, you must first generate or have access to a file containing the exclusion list. You may, for example, have a text file named Exclude.txt with the following contents. As can be seen, the exclusion file may include entries for certain file names, file extensions, and directories.
Exclude.txt.pdf.png xyz.txt xyz.txt xyz.txt xyz.txt xyz.t
To use Xcopy with exclusions, use the command below and use the /EXCLUDE switch of give the complete path to the exclusion file. The file C:WorkareaXCopyDemoExclude.txt is used in this command.
XCOPY C:WorkareaXCopyDemo /Exclude: C:WorkareaBackup /S /Y C:WorkareaXCopyDemoExclude.txt
What’s the end result? Xcopy copies files but skips xyz.txt, Exclusion.txt, all.pdf and.png files, and all items in the exclude directory.
A Differential Copy is a copy of a copy of a copy of a copy of
When you need to backup data from one place to another, you may use Xcopy’s differential backup feature. A differential backup replicates just the files whose modified date is on or after the date you specify with the /D:m-d-y option, rather than all files.
For example, execute the command below to copy just the files updated on or after April 1, 2021. This command also does a recursive copy at the destination, ignores errors, and overwrites existing files.
C:WorkareaXCopyDemo C:WorkareaBackup /D:04-01-2021 /S /C /Y XCOPY C:WorkareaXCopyDemo C:WorkareaBackup
If you don’t use the /D option to provide a date, Xcopy will only copy source files that are newer than the destination files.
Using the Archive Attribute to Copy Files
After a backup procedure, most backup tools delete the archive property from a file. When you update and save a file, Windows automatically resets the file’s archive property.
If you use Xcopy to backup files, you may use the archive property to specify whether the file should be copied or backed up.
The /A and /M options may be used to copy files that have the archive attribute. Whether you wish to keep the file’s archive attribute or not determines which of these settings to use.
The following command copies files with the archive attribute exclusively in a recursive manner. Because of the /A option, the archive attribute will remain on the target files after the copy.
C:WorkareaDemo XCOPY /A /S /Y D:WorkareaTest
Instead, use the /M option to remove the archive property from the source file once it has been copied to the destination.
C:WorkareaDemo XCOPY /M /S /Y D:WorkareaTest
Keeping ACL and File Owner Information
Consider making a backup of a profile folder’s contents. It’s possible that each file has a separate owner or set of rights. If you ever need to restore those files, you’ll want to make sure they have the same owners and rights.
How To Use PowerShell To Manage NTFS Permissions
This will make the whole procedure seem smooth, and users will have the same access to their data as previously. But first, make sure you’re backing up those data while keeping the current owners and access in place.
Add the /O argument to the xcopy command to copy files while keeping the owner and ACL information, as demonstrated below.
C:Workarea D:Work /O /I /Y XCOPY
You may also provide the audit settings in addition to the owner and ACL information. If your company uses group policy objects (GPOs) for audit files, copying audit settings might be handy.
Use the /X option to incorporate file audit settings when copying files with Xcopy. You don’t have to use both switches at the same time since the /X switch also implies the /O switch.
What is a Group Policy, and How Does It Work? (In Depth)
C:Workarea D:Work /X /I /Y XCOPY C:Workarea D:Work /X /I /Y
Using a Network to Transfer Files and Folders
Xcopy can copy data not just between places on the same or other disks, but also over a network. Unfortunately, transferring data over a network isn’t always secure. Short disruptions and, in certain situations, complete disconnections are possible with network connections.
Fortunately, Xcopy may be operated in a restartable mode. That is, even if the copy process is halted due to a network problem, the copy may be resumed if the network connection is restored. You’ll need to add the /Z switch to Xcopy’s command to make it restartable.
The command below, for example, copies files from the C:Workarea folder to a network destination in a recursive manner. The /Z option causes Xcopy to operate in a restartable mode.
C:Workarea “SERVER01Backup” /S /Z XCOPY
Xcopy additionally shows the file copy progress in % when the /Z option is used.
Using Unbuffered I/O to Copy Files
The operating system buffers disk reads and writes in the file system cache, which is referred to as buffered I/O. Buffering improves the performance of successive reads and writes to the same file, but it comes with a price. As a consequence, when copying a big file, unbuffered I/O (or a raw file copy) is the recommended option.
This copy strategy decreases file system cache overhead and prevents huge volumes of file data from being readily flushed from the cache.
You may use the /J option with the Xcopy command to copy big files using unbuffered I/O, as illustrated below.
C:Workareaabc.mp4 D:Temp /I /J XCOPY
Copying and Verifying Files
Copying files, like any other job, is not always guaranteed to be successful. Even if there are no evident faults, certain files may get corrupted during transmission.
The /V option in Xcopy may be used to check if the destination and source files are similar in size after copying. The fact that the source and destination files are identical indicates that the copy was successful and that the file is unaltered.
The following command transfers all files from C:WorkareaXCopyDemo to C:WorkareaBackup and uses the /V switch to verify each file. The /F switch reveals the complete path of the source and destination files.
C:WorkareaXCopyDemo C:WorkareaBackup /F /H /S /X /V XCOPY C:WorkareaXCopyDemo
Copying Files That Are Encrypted
Xcopy also supports Copying Files That Are Encrypted to destinations that do not support encryption. Using the /G switch, Xcopy copies encrypted source files and creates decrypted destination files.
When backing up encrypted data to network shares or non-Encrypting File System (EFS) volumes, this copy option comes in handy.
C:WorkareaXCOPYDemo C:WorkareaXCOPYDemoFolder /S /G Xcopy C:WorkareaXCOPYDemo
Creating Folder Structures from Scratch
Backup scripts or applications may need you to pre-provision the destination before transferring the data from the source in specific circumstances. You may use Xcopy to duplicate the original folder structure without the content if necessary.
To do so, use the /T switch with the Xcopy command. When you use the /T option, Xcopy copies just the directory tree structure to the destination, leaving empty directories alone.
You may also include empty directories by using the /E flag, as demonstrated in the sample command below.
C:WorkareaXCOPYDemo Xcopy /T /E C:WorkareaXCOPYDemoFolder
If anything can go wrong, it will most likely do so. This is also true when copying files, regardless of their size. The following are some of the most prevalent Xcopy errors.
The number of parameters is incorrect.
When the command you’ve typed includes space characters, you’ll see this error. When inputting a source or destination path that includes space characters, you should use double-quotes to prevent this issue.
For example, if the path is C:Documents and Settings, type “C:Documents and Settings” in the path field.
A Cyclic Copy is not possible.
If the source directory you’re copying also contains the destination directory, you’ll receive this error. Xcopy would then become stuck in a loop, resulting in an error. If you copy all directories and subdirectories using Xcopy, the destination must be a completely independent directory or disk.
Error in parsing
When you input incorrect or invalid syntax in a command, you’ll see this error. For example, you can get this error if your route is enclosed in quotations but the closing quotes are missing.
In the target path of the command below, for example, a closing double-quote character is omitted.
“C:WorkareaXCopy Demo /S /I /E /Y” Xcopy C:Workarea
Codes of Exit
Xcopy returns an exit code for an operation, which you can use to determine if the operation was successful. Codes of Exit are useful, especially if your task or script takes actions based on the exit code it receives.
The exit code will be saved in the percent ErrorLevel percent variable by the command shell. The value of this variable may then be examined by your code to ascertain the result of the Xcopy operation.
Below is a table that lists all the Xcopy Codes of Exit.
|0||There were no issues, as shown by this exit code.|
|1||This exit code indicates that Xcopy was unable to locate any files to copy.|
|2||The Xcopy was ended by hitting CTRL+C, as shown by this exit code.|
|4||This exit code indicates that an initialization issue occurred due to a lack of memory or storage space, or because you used incorrect syntax.|
|5||There was a disk write error, as indicated by this exit code.|
Using Xcopy for scripting
Apart from using Xcopy interactively, you may get the most out of it by utilizing scripts to automate operations. A few of examples of how to utilize Xcopy in scripts are provided below.
Xcopy Batch Script to Copy Data and Handle Errors
This sample batch script recursively copies all files and includes error-handling logic. The script then produces a message with the explanation for the issue depending on the error or exit code.
To make the batch file, open your favorite text or code editor, copy the code below, paste it into your editor, and save it as copycomplete.bat.
@rem rem rem rem rem rem rem This batch file transfers all source rem files from the source rem path (percent 1) to the destination path in all folders and subdirectories ( percent 2) xcopy 1% 2 /s /e /s /e /s /e /s /e /s if the error level is 4 lowmemory.com if the error level is 2 If the errorlevel is zero, you must abort. go to exit:lowmemory echo To transfer files or echo improper drive or command-line syntax, there is insufficient RAM. echo goto exit:abort The copy procedure was stopped because you hit the CTRL+C keys. go to exit:exit if you’re looking for a quick way out.
To launch the script, type its name followed by the source and destination directories in the command prompt or PowerShell.
xcopydemo xcopydemofolder pycomplete.bat
Xcopy batch file execution
To Move Data, Use the Xcopy Batch Script
Xcopy does not have any built-in capabilities for moving files and folders from one location to another. However, as a workaround, you may write a script that first Xcopies the files before deleting them from the source.
The following code will copy the files to the specified location. The script will then remove the files at the source if the copy operation was successful. Copy and paste the code below into a new file named movefiles.bat.
Rem To relocate files, use a batch file. percent 1 percent 2 Xcopy /D /V Rem Delete the files at the source if the Xcopy procedure was successful (0). if errorlevel 0 (del /Q percent 1 exit /B) if errorlevel 1 (echo Xcopy process finished successfully) Rem echo Errorlevel is not 0 The copying operation has failed.
Then, as in the previous example, run the movefiles.bat batch file by typing its name at the command prompt or PowerShell, followed by the source and destination folders.
xcopydemo xcopydemofolder movefiles.bat
If you have a job that requires you to copy files in bulk or create file backups, Xcopy is a great tool to have. Xcopy provides precise and quick file copy results, as well as a variety of choices for customizing its behavior to meet your needs.
Frequently Asked Questions
How do I use xcopy to copy all files and folders?
A: xcopy is a command line utility that can be used to copy all files and folders in one or more directories. You can use it in batch mode by piping arguments into the syntax of xcopy followed by your desired destination path, like so:
How do I copy files using xcopy?
A: You use the xcopy command to copy files.
Is xcopy faster than copy?
A: Faster is a relative term. It depends on the task at hand and how much time you have to complete it.
- xcopy all files and folders
- xcopy command
- xcopy options
- xcopy command to copy all files and folders with permissions
- xcopy file or directory