How to overwrite existing files in batch?

Batch FileOverwrite

Batch File Problem Overview


The following command copies and moves a file but I also need it to overwrite the file it's replacing.

xcopy /s c:\mmyinbox\test.doc C:\myoutbox

Batch File Solutions


Solution 1 - Batch File

Add /Y to the command line

Solution 2 - Batch File

You can use :

copy /b/v/y

See SS64 on COPY.

Solution 3 - Batch File

Add /y to the command line of xcopy:

Example:

xcopy /y c:\mmyinbox\test.doc C:\myoutbox

Solution 4 - Batch File

you need to simply add /Y

xcopy /s c:\mmyinbox\test.doc C:\myoutbox /Y

and if you're using path with spaces, try this

xcopy /s "c:\mmyinbox\test.doc" "C:\myoutbox" /Y

Solution 5 - Batch File

If the copy command is run from within a batch job you do not need to use the /Y switch: it will overwrite existing files.

Solution 6 - Batch File

For copying one file to another directory overwriting without any prompt i ended up using the simply COPY command:

copy /Y ".\mySourceFile.txt" "..\target\myDestinationFile.txt"

Solution 7 - Batch File

A command that would copy in any case

xcopy "path\source" "path\destination" /s/h/e/k/f/c/y

Solution 8 - Batch File

You can refer Windows command prompt help using following command : xcopy /?

Solution 9 - Batch File

If destination file is read only use /y/r

xcopy /y/r source.txt dest.txt

Solution 10 - Batch File

Here's what worked for me to copy and overwrite a file from B:\ to Z:\ drive in a batch script.

echo F| XCOPY B:\utils\MyFile.txt Z:\Backup\CopyFile.txt /Y

The "/Y" parameter at the end overwrites the destination file, if it exists.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMalView Question on Stackoverflow
Solution 1 - Batch FileEugene Mayevski 'CallbackView Answer on Stackoverflow
Solution 2 - Batch FileBenoitView Answer on Stackoverflow
Solution 3 - Batch Filechristopher.wr.schillView Answer on Stackoverflow
Solution 4 - Batch FileAlokView Answer on Stackoverflow
Solution 5 - Batch FileKim MasonView Answer on Stackoverflow
Solution 6 - Batch FileRuwenView Answer on Stackoverflow
Solution 7 - Batch FileRaj Nandan SharmaView Answer on Stackoverflow
Solution 8 - Batch FileabanmitraView Answer on Stackoverflow
Solution 9 - Batch FileProggearView Answer on Stackoverflow
Solution 10 - Batch FileGTodorovView Answer on Stackoverflow