155

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 
1
  • According to here ss64.com/nt/… xcopy has been deprecated. Although it's still ships - robocopy is the successor. Commented Feb 28, 2022 at 12:07

10 Answers 10

179

Add /Y to the command line

Sign up to request clarification or add additional context in comments.

3 Comments

How to submit F = File , D = directory ?
xcopy /s/Y c:\mmyinbox\test.doc C:\myoutbox (Y is a CAPITAL.)
49

You can use :

copy /b/v/y 

See SS64 on COPY.

5 Comments

im new to batch whats b/v/y stand for?
use copy /? to have help! /b means binary file, /v means check, /y is force. ss64.com is a very good reference otherwise.
sorry i just realised i have spaces in my foldernames i ussually use underscores, How is this handld properly?
enclose your arguments inside "'s. If you have a " inside an argument which is enclosed (which is never the case for filenames) double it.
@Mal also you can do dir /X in folder above to get the directory name with a ~ in it instead. The you don't need the quotes if you use that abridged variant. Source: superuser.com/questions/179449/…
39

Add /y to the command line of xcopy:

Example:

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

3 Comments

Is there any more information you can add that isn't already in another answer? This doesn't really need to be posted as a new answer otherwise.
use a trailing slash for the target path, otherwise it will give error if target folder doesnt exist
@Michelle the most upvoted answer is a boomer response that doesn't give the full command that we need to execute. This is a chad answer that you can just copy and paste, the best answer
25

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 

1 Comment

Is there any more information you can add that isn't already in another answer? This doesn't really need to be posted as a new answer otherwise
21

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" 

Comments

15

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.

Comments

3

A command that would copy in any case

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

Comments

1

If destination file is read only use /y/r

xcopy /y/r source.txt dest.txt 

Comments

1

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.

Comments

-2

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.