XCOPY Overwrite in Batch

Siddharth Bishnoi Jul 01, 2022
  1. Copy Files in a New Folder in Batch
  2. Copy Files by Overwriting Existing Files in Batch
  3. Copy Files by Overwriting All Files Without Prompt in Batch
  4. Copy Files Over a Network in Batch
XCOPY Overwrite in Batch

Xcopy is a command-line utility used to copy one or more files or directories, including subdirectories, from one location to another. It is similar to the copy command but more powerful as it can copy entire directories from one location to another.

It is powerful enough to copy even drives. Xcopy is a fast command which can copy files and folders, overwrite existing files with or without prompt, exclude files based on name, path or extension, identify updated files, etc.

It is supported in almost all versions of Windows starting from Windows 98. It can be used as a DOS command to use xcopy in MS-DOS.

The syntax for the xcopy command is:

xcopy <source> <destination> </parameters>
  1. source - location and names of the files to be copied. It must include the drive and path of the files or directories.
  2. destination - the destination of the files to be copied. It can be a drive, directory or file name.
  3. parameters - options provided by the xcopy command. They can be viewed by running xcopy /? in the command prompt window.

There are several other commands similar to xcopy, some even more powerful than it, such as robocopy, rsync, teracopy, etc. But, we will stick with the xcopy in this article.

This tutorial will teach how to overwrite all files in the destination without a prompt while copying files using the xcopy command.

Copy Files in a New Folder in Batch

To copy files from one location to another, execute the following command.

xcopy C:\testfolder D:\testfolder /i /s

copy files using xcopy command

  1. /s - copies directories, subdirectories and files within the folder to be copied, unless they are empty.
  2. /i - forces xcopy to assume the destination to be a directory and creates a new directory. You will be prompted to enter whether the destination is a file or a directory if it is not used.

To copy only the directory tree and not the files, i.e., directory structure, execute the following command.

xcopy C:\testfolder D:\testfolder /t /e

copy directories using xcopy command

  1. /t - copies the directory structure excluding files.
  2. /e - copies all subdirectories, even if they are empty. It is used with /t and /s options.

Output:

copy directories using xcopy command(output)

As shown above, only the directory is copied and not the files.

Copy Files by Overwriting Existing Files in Batch

To overwrite existing files while copying, execute the following command.

xcopy C:\testfolder D:\testfolder /i /s

overwrite existing files(with prompt)

As shown in the image above, it is asking to overwrite the existing file or all files. When we copy many files over existing files, it will show a prompt for every file with the same name.

This is too time-consuming to confirm for every file. To avoid this, we need to use the /y option and the above command.

Copy Files by Overwriting All Files Without Prompt in Batch

Execute the following command to copy existing files without prompt to overwrite existing files.

xcopy C:\testfolder D:\testfolder /i /s /y

overwrite existing files(without prompt)

/y - avoid prompt that confirms whether to overwrite the existing files or not at the destination.

Copy Files Over a Network in Batch

xcopy C:\Videos "\\server\media\videos" /s /i /z /w
  1. /s - copies directories, subdirectories and files within the folder to be copied, unless they are empty.
  2. /i - forces xcopy to assume the destination to be a directory and creates a new directory. You will be prompted to enter whether the destination is a file or a directory if it is not used.
  3. /z - copies over a network in restart mode, i.e., if the connection is lost during copying, it is resumed when the connection is restored. It also shows the completion percentage.
  4. /w - Display the following message: Press any key to begin copying file(s). It waits for your response to start the copying process.

So, we discussed how to overwrite all existing files without showing a prompt while copying many files, i.e., it confirms to overwrite all files without showing a prompt for each file.

There are many other options that the xcopy command provides; you can check them by running xcopy /? in the command prompt.