147

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
Vertexwahn
  • 7,709
  • 6
  • 64
  • 90
Mal
  • 1,471
  • 2
  • 9
  • 3
  • According to here https://ss64.com/nt/xcopy.html#:~:text=XCOPY%20is%20similar%20to%20the,source%20and%20destination%20in%20detail.&text=%2FD%3Amm%2Ddd%2D,newer%20than%20the%20destination%20time.XCOPY `xcopy` has been deprecated. Although it's still ships - `robocopy` is the successor. – JGFMK Feb 28 '22 at 12:07

10 Answers10

173

Add /Y to the command line

Eugene Mayevski 'Callback
  • 45,135
  • 8
  • 71
  • 121
46

You can use :

copy /b/v/y

See SS64 on COPY.

James Bradbury
  • 1,708
  • 1
  • 19
  • 31
Benoit
  • 76,634
  • 23
  • 210
  • 236
  • 3
    im new to batch whats b/v/y stand for? – Mal Oct 29 '10 at 11:27
  • 14
    use `copy /?` to have help! `/b` means binary file, `/v` means check, `/y` is force. http://www.ss64.com is a very good reference otherwise. – Benoit Oct 29 '10 at 11:30
  • sorry i just realised i have spaces in my foldernames i ussually use underscores, How is this handld properly? – Mal Oct 29 '10 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 Oct 29 '10 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: https://superuser.com/questions/179449/windows-path-without-spaces-with-1 – JGFMK Feb 28 '22 at 12:10
34

Add /y to the command line of xcopy:

Example:

xcopy /y c:\mmyinbox\test.doc C:\myoutbox
Sam Denty
  • 3,693
  • 3
  • 30
  • 43
  • 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 Aug 20 '13 at 19:52
  • 1
    use a trailing slash for the target path, otherwise it will give error if target folder doesnt exist – Code Name Jack Nov 26 '18 at 08:07
  • @Michelle the [most upvoted answer](https://stackoverflow.com/a/4051313/2076973) 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 Dec 13 '22 at 12:13
23

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
Alok
  • 361
  • 3
  • 4
  • 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 May 12 '17 at 06:12
17

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"
Ruwen
  • 3,008
  • 1
  • 19
  • 16
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.

Kim Mason
  • 149
  • 1
  • 2
3

A command that would copy in any case

xcopy "path\source" "path\destination" /s/h/e/k/f/c/y
araknoid
  • 3,065
  • 5
  • 33
  • 35
Raj Nandan Sharma
  • 3,694
  • 3
  • 32
  • 42
1

If destination file is read only use /y/r

xcopy /y/r source.txt dest.txt
Proggear
  • 662
  • 6
  • 10
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.

GTodorov
  • 1,993
  • 21
  • 24
-2

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

abanmitra
  • 1,344
  • 1
  • 10
  • 10