HOWTO · Batch
XCOPY Overwrite in Batch
This tutorial teaches how to use XCOPY in batch to overwrite existing files, including how to overwrite all files without a prompt.
On this page
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] [switches]
source- location and names of the files to be copied. It must include the drive and path of the files or directories.destination- the destination of the files to be copied. It can be a drive, directory or file name.switches- options provided by thexcopycommand. They can be viewed by runningxcopy /?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
/s- copies directories, subdirectories and files within the folder to be copied, unless they are empty./i- forcesxcopyto 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
/t- copies the directory structure excluding files./e- copies all subdirectories, even if they are empty. It is used with/tand/soptions.
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
When the destination already contains a file with the same name, xcopy stops and asks what to do with that file:
Overwrite D:\testfolder\MyFile.txt (Yes/No/All)?
As shown in the image above, it is asking to overwrite the existing file or all files. Answering All applies the overwrite to every remaining file as well. When we copy many files over existing files without answering All, 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 with the above command.
Copy Files by Overwriting All Files Without Prompt in Batch
Execute the following command to copy files without a prompt to overwrite existing files.
xcopy C:\testfolder D:\testfolder /i /s /y
/y - suppresses the prompt that confirms whether to overwrite the existing files or not at the destination, so every existing file is overwritten automatically.
Copy Files Over a Network in Batch
xcopy C:\Videos "\\server\media\videos" /s /i /z /w
/s- copies directories, subdirectories and files within the folder to be copied, unless they are empty./i- forcesxcopyto 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./z- copies files over a network in restartable mode, i.e., if the connection is lost while a file is being copied, the copy of that file resumes when the connection is restored. It also shows the completion percentage./w- displays the messagePress any key to begin copying file(s)and waits for your response before the copying process starts.
So, we discussed how to overwrite all existing files without showing a prompt while copying many files, i.e., it overwrites 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. Note that Microsoft has deprecated xcopy and recommends robocopy for new scripts on current versions of Windows, but xcopy remains available and widely used in existing batch files.