---
title: "The Ultimate Guide to Copying Files and Folders Using Xcopy"
description: "When you need to copy large sets of files, do you still use the copy command? Why not give Xcopy a spin and find out how it is a better tool for the job? Learn how to use Xcopy with lots of examples in this ultimate guide."
canonical: "https://adamtheautomator.com/xcopy/"
---

# The Ultimate Guide to Copying Files and Folders Using Xcopy

> When you need to copy large sets of files, do you still use the copy command? Why not give Xcopy a spin and find out how it is a better tool for the job? Learn how to use Xcopy with lots of examples in this ultimate guide.

Source: https://adamtheautomator.com/xcopy/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![The Ultimate Guide to Copying Files and Folders Using Xcopy](https://adamtheautomator.com/wp-content/uploads/2021/06/Xcopy-The-Ultimate-Guide.jpg)

# The Ultimate Guide to Copying Files and Folders Using Xcopy

[![](https://secure.gravatar.com/avatar/375d1594d4bdf138ea2280401ba7f36513ca9e5ef259e44e5c5359d2127cf297?s=192&d=mm&r=g)Joydip Kanjilal](https://adamtheautomator.com/author/joydip-kanjilal/)3 June 202115 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[File Management](/tag/file-management/)

Table of Contents

*   [Prerequisites](#h-prerequisites)
*   [Understanding the Xcopy Command](#h-understanding-the-xcopy-command)
*   [Benefits](#h-benefits)
*   [Limitations](#h-limitations)
*   [Xcopy Syntax Reference](#h-xcopy-syntax-reference)
*   [Source Options](#h-source-options)
*   [Destination Options](#h-destination-options)
*   [Copy Options](#h-copy-options)
*   [Working with Xcopy: Usage Examples](#h-working-with-xcopy-usage-examples)
*   [Listing Files to Copy](#h-listing-files-to-copy)
*   [Copying a Single File](#h-copying-a-single-file)
*   [Copying Files to a New Folder](#h-copying-files-to-a-new-folder)
*   [Copying All Files and Folders Recursively](#h-copying-all-files-and-folders-recursively)
*   [Including Hidden and System Files](#h-including-hidden-and-system-files)
*   [Preserving the Read-Only File Attribute](#h-preserving-the-read-only-file-attribute)
*   [Filtering Files to Copy](#h-filtering-files-to-copy)
*   [Excluding Files and Folders from the Copy](#h-excluding-files-and-folders-from-the-copy)
*   [Performing a Differential Copy](#h-performing-a-differential-copy)
*   [Copying Files Based on the Archive Attribute](#h-copying-files-based-on-the-archive-attribute)
*   [Retaining File Owner and ACL Information](#h-retaining-file-owner-and-acl-information)
*   [Copying Files and Folders Over the Network](#h-copying-files-and-folders-over-the-network)
*   [Copy Files Using Unbuffered I/O](#h-copy-files-using-unbuffered-i-o)
*   [Copying Files with Verification](#h-copying-files-with-verification)
*   [Copying Encrypted Files](#h-copying-encrypted-files)
*   [Copying Folder Structures](#h-copying-folder-structures)
*   [Common Errors](#h-common-errors)
*   [Invalid Number of Parameters](#h-invalid-number-of-parameters)
*   [Cannot Perform a Cyclic Copy](#h-cannot-perform-a-cyclic-copy)
*   [Parse Error](#h-parse-error)
*   [Exit Codes](#h-exit-codes)
*   [Scripting with Xcopy](#h-scripting-with-xcopy)
*   [Xcopy Batch Script to Copy Data with Error Handling](#h-xcopy-batch-script-to-copy-data-with-error-handling)
*   [Xcopy Batch Script to Move Data](#h-xcopy-batch-script-to-move-data)
*   [Conclusion](#h-conclusion)

The [Xcopy](https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/xcopy) tool, which stands for _extended copy,_ is a popular command-line utility for copying massive amounts of data in Windows. Compared to the traditional [`copy`](https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/copy) command, Xcopy has more features and switches that give you better control when copying or moving files and folders.

Not a reader? Watch this related video tutorial!

**_Not seeing the video? Make sure your ad blocker is disabled._**

In this guide, you will learn how to use Xcopy for various file copy scenarios. From copying a single file, multiple directories, applying filters or exclusions, and more. By the end, you will have gained the knowledge to use Xcopy in your day-to-day file copying tasks.

Let’s begin!

## Prerequisites

To follow along with the examples in this guide, you’ll need the following.

*   A Windows (server or client) computer. Xcopy comes built-in to Windows, and you don’t need to download anything else. This guide will use Windows 10 Build 1909.
*   Xcopy works on the command prompt or PowerShell, and this article assumes that you already have one open.

## Understanding the Xcopy Command

Xcopy is a command-line utility, which has been available out of the box since Windows 98. You can find this tool under the _%WINDIR%\\system32_ folder with the executable name `xcopy.exe`.

Compared to the Windows `copy` command, Xcopy is much more efficient in copying files and directories. In addition, Xcopy has more options that make it more customizable and lets you control the file copy behavior.

### **Benefits**

There are several benefits or advantages of using Xcopy that you will learn as you progress in this guide. But below are some of the benefits of using Xcopy.

*   Faster copy operation on large sets of files and directories.
*   [Simplifies application deployment](https://docs.microsoft.com/en-us/troubleshoot/visualstudio/general/deploy-aspnet-app-xcopy-command#the-advantages-of-xcopy-deployment).
*   Can replicate the source directory structure as is.
*   Copy files while retaining the owner and [access control list (ACL)](https://docs.microsoft.com/en-us/windows/win32/secauthz/access-control-lists) information.
*   Copy and overwrite read-only files.
*   Can exclude files based on the file name, extension, or path.
*   Can identify updated files, which is useful for differential backups.
*   Integrate and use with scripts.

### Limitations

As awesome as Xcopy is, it is not without limitations. Although these limitations may not be a deal-breaker, knowing what they are would be good before diving in.

*   Cannot copy open files.
*   No support for the [Windows Volume Shadow Copy Service (VSS)](https://docs.microsoft.com/en-us/windows-server/storage/file-server/volume-shadow-copy-service). This lack of VSS support makes Xcopy not suitable for backing up live operating system volumes.
*   Insufficient memory error when the length of the path plus file name exceeds 254 characters.

## Xcopy **Syntax Reference**

Xcopy lets you perform various file and folder copy operations. But first, you’ll need to understand its syntax and options. There are a lot of options that would change how Xcopy operates. To help you make sense of these options, the tables that follow will cover them in detail.

The first path designation refers to the source file(s); the second path designation refers to the destination file(s).

```powershell
xcopy [source] [destination] [options]
```

*   `[source]` – Pathname for the file(s) to copy (accepts wildcards). You should specify the drive, path, and the files you would want to copy.
*   `[destination]` – Pathname for the new file(s). If you don’t specify the destination path, the command will copy the files to the same path as the source.
*   `[options]` – Can be any of the following options in the tables below. You can also view these options by running the `xcopy /?` command or visiting the [Xcopy online documentation](https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/xcopy).

### Source Options

<table><tbody><tr><td>Switch</td><td>Explanation</td></tr><tr><td>/A</td><td>Copy files that have the archive <a href="https://en.wikipedia.org/wiki/File_attribute" target="_blank" rel="noreferrer noopener">attribute</a>.</td></tr><tr><td>/M</td><td>Copy files with the archive attribute set, then switches off the archive attribute at the destination. Use this option when creating routine backups.</td></tr><tr><td>/H</td><td>Copy hidden and system files and directories. The default value is N.</td></tr><tr><td>/D:mm-dd-yyyy</td><td>Only copies that files with modification date on or after the given date. Without using this option, the command will copy only the files whose date/time is newer than the destination.</td></tr><tr><td>/U</td><td>Only copies the files that already exists at the destination.</td></tr><tr><td>/S</td><td>Copies folders and subfolders recursively excluding the empty one.</td></tr><tr><td>/E</td><td>Copies folders and subfolders recursively including the empty one.</td></tr><tr><td>/EXCLUDE:file1[+file2][+file3]…</td><td>Specify the text file(s) containing a list of strings to match absolute file paths to exclude.</td></tr></tbody></table>

### Destination Options

<table><tbody><tr><td>Switch</td><td>Explanation</td></tr><tr><td>/I</td><td>If the destination does not exist, using this option assumes that the destination is a directory and creates it. If you omit this option, the command prompts to confirm if the destination is a directory or a file.</td></tr><tr><td>/R</td><td>Overwrites read-only files.</td></tr><tr><td>/T</td><td>Creates a recursive folder structure at the destination without copying the files. This option does not include copying empty folders. To include empty folders, use this option together with the /E option.</td></tr><tr><td>/K</td><td>Preserves the file attributes during the copy. Otherwise, the command will reset read-only attributes.</td></tr><tr><td>/N</td><td>Copies files using their <a href="https://docs.microsoft.com/en-us/windows/win32/fileio/naming-a-file#short-vs-long-names" target="_blank" rel="noreferrer noopener">short file names</a>.</td></tr><tr><td>/O</td><td>Copies files including ownership and <a href="https://docs.microsoft.com/en-us/windows/win32/secauthz/access-control-lists" target="_blank" rel="noreferrer noopener">access control list (ACL)</a> information.</td></tr><tr><td>/X</td><td>Copies files including <a href="https://docs.microsoft.com/en-us/windows/security/threat-protection/auditing/apply-a-basic-audit-policy-on-a-file-or-folder" target="_blank" rel="noreferrer noopener">file audit settings</a> and ACL information (implies using /O).</td></tr></tbody></table>

### Copy Options

<table><tbody><tr><td>Switch</td><td>Explanation</td></tr><tr><td>/W</td><td>Prompts for a key press before the copy process can start.</td></tr><tr><td>/P</td><td>Prompts (Y/N) before creating each file.</td></tr><tr><td>/Y</td><td>Suppresses the confirmation to overwrite destination file if exists.</td></tr><tr><td>/-Y</td><td>Prompts to confirm overwriting an existing destination file.</td></tr><tr><td>/V</td><td>Verifies that the source and destination file sizes are identical.</td></tr><tr><td>/C</td><td>Ignores errors and forces the command to continue copying.</td></tr><tr><td>/B</td><td>Copies only the symbolic link but not the link target file.</td></tr><tr><td>/G</td><td>Copies encrypted files to a destination that does not support encryption.</td></tr><tr><td>/J</td><td>Copies files without using the <a href="https://docs.microsoft.com/en-us/windows/win32/fileio/file-caching" target="_blank" rel="noreferrer noopener">buffer</a>. This switch useful when copying very large files.</td></tr><tr><td>/Q</td><td>Suppresses the display of file names while copying.</td></tr><tr><td>/F</td><td>Displays the full source and destination file paths while copying.</td></tr><tr><td>/L</td><td>Only displays which files to copy without copying them. This switch can be useful when testing Xcopy commands with multiple options to assess the expected result.</td></tr><tr><td>/Z</td><td>Copies files in restartable mode. Using this option, the command gracefully stops copying files during network interruption. The copying will resume after re-estabslishing the network connection.</td></tr></tbody></table>

## Working with Xcopy: Usage Examples

Now that you’re familiar with the Xcopy syntax and options, it’s time to begin actually using it by combining one or more options together. You’ll explore many different scenarios in the following sections to use Xcopy to copy files and folders.

### Listing Files to Copy

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 Xcopy options.

Using the `/L` switch with Xcopy, you can simulate what would have happened when you issued the command by listing which files it would copy. This way, you can confirm whether your command will copy all the files that you intended.

```powershell
XCOPY C:\Workarea\XCopyDemo C:\Workarea\XCopyDemoFolder /L
```

### **Copying a Single File**

The most basic operation you can do with Xcopy is copying a file from a source to a destination without using any options. For example, the command below will copy a text file to a destination directory, and the file name will be the same.

```powershell
XCOPY C:\Workarea\XCopyDemo\NewFile.txt C:\Workarea\XCopyDemoFolder
```

You can also copy the source file and change the destination filename by running the command below.

```powershell
XCOPY C:\Workarea\XCopyDemo\NewFile.txt C:\Workarea\XCopyDemoFolder\AnotherFile.txt
```

### **Copying Files to a New Folder**

With Xcopy, you can copy files and create the destination directory on the fly. To do so, you need to append the `/I` option to the Xcopy command.

For example, the command below copies the files from the _C:\\Workarea\\Demo_ folder to the _D:\\Workarea_ folder. If the destination folder does not exist, Xcopy creates the destination folder using the `/I` option.

```powershell
XCOPY C:\Workarea\Demo D:\Workarea /I
```

### Copying All Files and Folders Recursively

Aside from copying files from one folder to another, Xcopy also lets you copy folders and files recursively. And there are two ways you can do a recursive copy—with and without empty folders.

To copy all files and directories while ignoring empty directories, append the `/S` option to the end of the Xcopy command, as shown below.

```powershell
XCOPY C: D: /S
```

On the other hand, to include empty directories during the recursive copy, add the `/E` option instead to the command.

```powershell
XCOPY C: D: /E
```

### Including **Hidden and System Files**

By default, Xcopy does not include hidden and system files in copy operations. But if you need to force Xcopy to include hidden and system files, add the `/H` switch to the command.

The command below copies all files recursively, including hidden and system files. This command also ignores errors, creates the destination folders, and overwrites existing files.

```powershell
XCOPY C:\Workarea\Documents D:\Workarea\Documents /S /H /C /Y
```

### Preserving the Read-Only File Attribute

Another attribute that Xcopy can handle and preserve is the file’s read-only attribute. By default, when Xcopy copies a read-only file, it removes the file’s read-only attribute at the destination. To stop Xcopy from removing the read-only attribute, append the `/K` switch to the command.

For example, the command below copies a read-only file to another location, and the resulting destination file will still have a read-only attribute. This command will also overwrite the destination file if the file exists.

```powershell
XCOPY C:\Workarea\XCopyDemo\NewFile.txt D:\Workarea /I /K /Y
```

As a side-effect of preserving the file as read-only, Xcopy cannot overwrite the same file in future copy operations. But, you can force Xcopy to overwrite read-only files by adding the `/R` switch.

```powershell
XCOPY C:\Workarea\XCopyDemo\NewFile.txt D:\Workarea /K /R /Y
```

### Filtering Files to Copy

Suppose there are multiple files in the source folder and subfolders you want to copy. Xcopy allows you to input wildcards as a way to filter which files to copy.

For example, the command below copies only the files with the _.cs_ extension recursively from the root of the _C:_ drive to the root of the _E:_ drive. This command will also ignore errors and overwrites existing files without prompting for confirmation.

```powershell
XCOPY C:\*.cs E:\ /S /C /Y
```

### **Excluding Files and Folders from the Copy**

A powerful feature of Xcopy is its ability to exclude files from the copy process. To use this feature, you can leverage the `/EXCLUDE` switch. This switch accepts the names of the file(s) that contain the exclusion lists.

First, you need to create or have a file containing the exclusion list. For example, you can have a text file called _Exclude.txt_ that contains the following entries. As you can see, the exclusion file can have specific file names, file extensions, and folders as entries.

```powershell
xyz.txt
Exclude.txt
.pdf
.png
\exclude\
```

To run Xcopy with exclusions, run the command below and specify the full path of the exclusion file to the `/EXCLUDE` switch. This command uses the _C:\\Workarea\\XCopyDemo\\Exclude.txt_ file.

```powershell
XCOPY C:\Workarea\XCopyDemo C:\Workarea\Backup /S /Y /Exclude:C:\Workarea\XCopyDemo\Exclude.txt
```

The result? Xcopy runs to copy files but skips the _xyz.txt_, _Exclude.txt_, all files with _.pdf_ and _.png_ extensions, and all files under the _exclude_ directory.

### Performing a Differential Copy

When you need to backup files from one location to another, Xcopy has an option that lets you perform a differential backup. Instead of copying all files, a differential backup only copies the files where the modified date falls on or after a date that you specify to the `/D:m-d-y` switch.

For example, to copy only the files with the modified date on or after April 01, 2021, run the command below. Additionally, this command performs a recursive copy, ignores errors, and overwrites existing files at the destination.

```powershell
XCOPY C:\Workarea\XCopyDemo C:\Workarea\Backup /D:04-01-2021 /S /C /Y
```

If you do not specify a date with the `/D` option, Xcopy will copy only the source files that are newer than the destination files.

### **Copying Files Based on the Archive Attribute**

Typically, backup programs remove a file’s archive attribute after a backup operation. After modifying a file (i.e., edit and save), Windows automatically resets the files archive attribute.

If you create a script to backup files using Xcopy, you can use the archive attribute to determine whether to copy or backup the file.

To copy files with the archive attribute, you can take advantage of the `/A` and `/M` switches. Which one of these switches to use depends on whether you want to preserve the file’s archive attribute or not.

The following command performs a recursive copy of files with the archive attribute only. The destination files will retain the archive attribute after the copy due to the `/A` switch.

```powershell
XCOPY C:\Workarea\Demo D:\Workarea\Test /A /S /Y
```

In contrast, to remove the source file’s archive attribute after copying to the destination, specify the `/M` switch instead.

```powershell
XCOPY C:\Workarea\Demo D:\Workarea\Test /M /S /Y
```

### Retaining File Owner and ACL Information

Imagine creating a backup of an entire profile folder. Each file may have different owners or unique permissions. Should you need to restore those files, you’d want the same owners and [permissions](https://adamtheautomator.com/ntfs-permissions/) intact.

Related:[How To Manage NTFS Permissions With PowerShell](https://adamtheautomator.com/ntfs-permissions/)

This way, the whole process would seem seamless, and the users will still have the same access to the files they had before. But first, you need to make sure that you’re backing up those files while retaining the existing owners and access.

To copy files and retain the owner and ACL information, add the `/O` parameter to the `xcopy` command, as shown below.

```powershell
XCOPY C:\Workarea D:\Work /O /I /Y
```

Apart from retaining the owner and ACL information, you can also include the files’ [audit settings](https://www.lepide.com/how-to/track-who-read-files-on-your-windows-file-servers.html). Copying audit settings can be useful if your organization applies [group policy objects (GPO)](https://docs.microsoft.com/en-us/previous-versions/windows/desktop/policy/group-policy-objects) to audit files.

To include file audit settings when copying files with Xcopy, use the `/X` switch. The `/X` switch implies the `/O` switch, too, so you don’t have to use both switches at the same time.

Related:[What is Group Policy and How Does it Work? (In Detail)](https://adamtheautomator.com/what-is-group-policy/)

```powershell
XCOPY C:\Workarea D:\Work /X /I /Y
```

### **Copying Files and Folders Over the Network**

Not only can Xcopy copy files between locations on the same or different disks, but it can also copy files over the network. Unfortunately, copying files over the network is not always reliable. Network connections may suffer from short interruptions and, in some cases, total loss of connection.

Luckily, you can run Xcopy in restartable mode. Meaning, even if the copy progress stops due to a network error, the copy can resume after re-establishing the network connection. To run Xcopy in restartable mode, you’ll need to add the `/Z` switch to the command.

For example, the command below performs a recursive file copy from the _C:\\Workarea_ folder to a network location. The `/Z` parameter makes Xcopy run in restartable mode.

```powershell
XCOPY C:\Workarea "\\SERVER01\Backup" /S /Z
```

> _Using the `/Z` switch, Xcopy also displays the file copy progress in percentage._

### **Copy Files Using Unbuffered I/O**

Buffered I/O corresponds to how the operating system buffers disk reads and writes in the file system cache. While buffering speeds up subsequent reads and writes to the same file, it comes at a cost. As a result, unbuffered I/O (or a raw file copy) is the preferred method for copying a large file.

This copy method reduces the file system cache overhead and prevents the file system cache from being easily flushed with large amounts of file data.

To copy large files using unbuffered I/O, you can append the `/J` switch to the Xcopy command, as shown below.

```powershell
XCOPY C:\Workarea\abc.mp4 D:\Temp /I /J
```

### **Copying Files with Verification**

Like any other tasks, copying files may not always return 100% successful results. Some files may become corrupted during transfer even if there are no visible errors.

With Xcopy, you can use the `/V` switch to verify that the destination and source files are identical based on their size after copying. Identical source and destination files indicate that the copy was successful and that file is intact.

The command below copies all files from `C:\Workarea\XCopyDemo` to `C:\Workarea\Backup` and verifies each file using the `/V` switch. The `/F` switch displays the source and destination files’ full path.

```powershell
XCOPY C:\Workarea\XCopyDemo C:\Workarea\Backup /F /H /S /X /V
```

### **Copying Encrypted Files**

Xcopy also supports copying encrypted files to destinations that do not support encryption. Using the `/G` switch, Xcopy copies encrypted source files and creates decrypted destination files.

This copy mode is helpful, especially when backing up encrypted files to network shares or non-[Encrypting File System (EFS)](https://www.techopedia.com/definition/3356/encrypting-file-system-efs) volumes.

```powershell
Xcopy C:\Workarea\XCOPYDemo C:\Workarea\XCOPYDemoFolder /S /G
```

### Copying Folder Structures

In some situations, backup scripts or programs may require you to pre-provision the destination before copying the files from the source. When needed, you can use Xcopy to replicate the source folder structure without the content.

To do so, run the Xcopy command with `/T` switch. Using the `/T` switch will cause Xcopy to copy only the directory tree structure to the destination but ignores the empty directories.

You can also add the `/E` switch to the command to include empty directories, as shown in the example command below.

```powershell
Xcopy C:\Workarea\XCOPYDemo C:\Workarea\XCOPYDemoFolder /T /E
```

## Common Errors

If something can go wrong, it probably will. The same is the case when you’re copying files regardless of the volume. Below are the common Xcopy errors that you might encounter.

### Invalid Number of Parameters

This error typically occurs when the command you’ve entered contains space characters in it. To avoid this error, when typing a source or destination path that contains space characters, you should surround the path with double-quotes.

For example, if the path is _C:\\Documents and Settings_, enter the path as _“C:\\Documents and Settings”._

### Cannot Perform a Cyclic Copy

This error happens if the source directory you are copying also includes the target directory. In this case, Xcopy would enter a loop, causing an error. When using Xcopy, the destination must be a fully independent directory or disk if you copy all directories and subdirectories.

### Parse Error

This error occurs when you’ve typed wrong or invalid syntax in the command. As an example, you might encounter this error when you’ve specified your path in quotes and but the ending quotes are missing.

For example, the command below has a missing closing double-quote character in the destination path.

```powershell
Xcopy C:\Workarea "C:\Workarea\XCopy Demo /S /I /E /Y
```

## **Exit Codes**

Xcopy returns an exit code for an operation, which you can use to determine if the operation was successful. Exit codes are useful, especially if your task or script takes actions based on the exit code it receives.

The command shell will save exits code in the `%ErrorLevel%` variable. Your code can then examine the value of this variable to determine the outcome of the Xcopy operation.

Below is a table that lists all the Xcopy exit codes.

<table><tbody><tr><td>Exit Code</td><td>Purpose</td></tr><tr><td>0</td><td>This exit code means that there were no errors.</td></tr><tr><td>1</td><td>This exit code means that Xcopy did not find any files to copy.</td></tr><tr><td>2</td><td>This exit code means that the Xcopy was terminated by pressing CTRL+C.</td></tr><tr><td>4</td><td>This exit code means that an initialization error happened either because of insufficient memory or disk space or because you’ve entered invalid syntax.</td></tr><tr><td>5</td><td>This exit code means that there was a disk write error.</td></tr></tbody></table>

## Scripting with Xcopy

Apart from using Xcopy interactively, you can reap its benefits better if you use it to automate tasks with scripts. Below are a couple of examples that demonstrate how you can use Xcopy in scripts.

### Xcopy Batch Script to Copy Data with Error Handling

This example batch script copies all files recursively with error-handling logic. And based on the error or exit code, the script returns a message showing the reason for the error.

To create the batch file, open a text or code editor of your choice, copy the code below, paste it to your editor, and save the file as _copycomplete.bat_.

```powershell
@echo off
rem This batch file copies all source
rem files in all directories and sub-directories from the source
rem path (%1) to the destination path (%2)

xcopy %1 %2 /s /e

if errorlevel 4 goto lowmemory
if errorlevel 2 goto abort
if errorlevel 0 goto exit

:lowmemory
echo Insufficient memory to copy files or
echo invalid drive or command-line syntax.
goto exit

:abort
echo You pressed CTRL+C keys due to which the copy operation has been aborted.
goto exit

:exit
```

Next, to run the script, invoke its name in the command prompt or PowerShell, followed by the source and destination folders.

```powershell
pycomplete.bat xcopydemo xcopydemofolder
```

![Running an Xcopy batch file](https://adamtheautomator.com/wp-content/uploads/2021/06/Untitled-2021-06-03T110624.029.png)

Running an Xcopy batch file

### Xcopy Batch Script to Move Data

Xcopy has no built-in functionality to move files and folders from the source to the destination. But, as a workaround, you can create a script that would Xcopy the files first and then delete the files from the source.

The code below will copy the files to the destination. And if the copy process was successful, the script will delete the files at the source. Copy the code below and save it in a new file called _movefiles.bat_.

```powershell
Rem Batch file to move files

Xcopy /D /V %1 %2

Rem If the Xcopy process is successful (0), then delete the files at the source.
if errorlevel 0 (
echo Xcopy process completed successfully
del /Q %1
exit /B
)

Rem Errorlevel is not 0
echo The copy process has failed.
```

Next, to execute the _movefiles.bat_ batch file, invoke its name in the command prompt or PowerShell followed by the source and destination paths, as you did in the previous example.

```powershell
movefiles.bat xcopydemo xcopydemofolder
```

## Conclusion

If your job involves copying files in bulk or creating file backups, Xcopy is an excellent tool to help make your job easier. Xcopy delivers accurate and fast file copy results with many options to customize its behavior to fit your requirements.

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fxcopy%2F&text=The%20Ultimate%20Guide%20to%20Copying%20Files%20and%20Folders%20Using%20Xcopy)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fxcopy%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fxcopy%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2022/11/How-to-Install-NitroShare-for-Cross-Platform-File-Transfer.jpg)

### [Install NitroShare and Streamline Your File Transfers Today](/nitroshare/)

Simplify cross-platform file transfers with NitroShare. This tutorial guides you through installation and use for fast, effortless file sharing between different operating systems.

![](https://adamtheautomator.com/wp-content/uploads/2021/06/winSCP-command-line.jpg)

### [WinSCP Command-Line Simplified : Your Complete Reference](/winscp-command-line/)

Explore the depths of WinSCP Command-Line in our guide. Master file transfer and automation with expert tips, techniques, and practical examples for users at all levels.

![](https://adamtheautomator.com/wp-content/uploads/2023/03/seafile.jpg)

### [Get Started With Seafile Self-Hosted Cloud Storage](/seafile/)

Get started with your own cloud storage solution. Seafile offers a self-hosted alternative with easy setup, secure file sharing, and data privacy.

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
