152

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.
    – JGFMK
    Commented Feb 28, 2022 at 12:07

10 Answers 10

178

Add /Y to the command line

3
  • How to submit F = File , D = directory ?
    – user285594
    Commented Sep 14, 2016 at 14:03
  • @YumYumYum stackoverflow.com/questions/3018289/… Commented Oct 17, 2016 at 21:51
  • 5
    xcopy /s/Y c:\mmyinbox\test.doc C:\myoutbox (Y is a CAPITAL.)
    – Halfacht
    Commented Jan 16, 2018 at 13:08
47

You can use :

copy /b/v/y

See SS64 on COPY.

5
  • 3
    im new to batch whats b/v/y stand for?
    – Mal
    Commented Oct 29, 2010 at 11:27
  • 14
    use copy /? to have help! /b means binary file, /v means check, /y is force. ss64.com is a very good reference otherwise.
    – Benoit
    Commented Oct 29, 2010 at 11:30
  • sorry i just realised i have spaces in my foldernames i ussually use underscores, How is this handld properly?
    – Mal
    Commented Oct 29, 2010 at 11:31
  • 2
    enclose your arguments inside "'s. If you have a " inside an argument which is enclosed (which is never the case for filenames) double it.
    – Benoit
    Commented Oct 29, 2010 at 11:33
  • @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/…
    – JGFMK
    Commented Feb 28, 2022 at 12:10
39

Add /y to the command line of xcopy:

Example:

xcopy /y c:\mmyinbox\test.doc C:\myoutbox
3
  • 2
    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.
    – Michelle
    Commented Aug 20, 2013 at 19:52
  • 1
    use a trailing slash for the target path, otherwise it will give error if target folder doesnt exist Commented Nov 26, 2018 at 8:07
  • @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
    – Bersan
    Commented Dec 13, 2022 at 12:13
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
  • 1
    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
    – jeb
    Commented May 12, 2017 at 6:12
19

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"
14

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.

3

A command that would copy in any case

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

If destination file is read only use /y/r

xcopy /y/r source.txt dest.txt
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.

-2

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.