131

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
  • 6,759
  • 6
  • 50
  • 78
Mal
  • 1,311
  • 2
  • 8
  • 3

10 Answers10

159

Add /Y to the command line

Eugene Mayevski 'Callback
  • 43,492
  • 7
  • 62
  • 119
44

You can use :

copy /b/v/y

See SS64 on COPY.

James Bradbury
  • 1,523
  • 1
  • 18
  • 29
Benoit
  • 70,220
  • 21
  • 189
  • 223
  • 1
    im new to batch whats b/v/y stand for? – Mal Oct 29 '10 at 11:27
  • 10
    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
  • 1
    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
32

Add /y to the command line of xcopy:

Example:

xcopy /y c:\mmyinbox\test.doc C:\myoutbox
Sam Denty
  • 3,024
  • 3
  • 19
  • 33
  • 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
20

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
  • 321
  • 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
12

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
  • 129
  • 1
  • 2
3

A command that would copy in any case

xcopy "path\source" "path\destination" /s/h/e/k/f/c/y
araknoid
  • 2,805
  • 4
  • 30
  • 32
Raj Sharma
  • 3,196
  • 3
  • 27
  • 38
2

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
  • 2,140
  • 14
  • 13
1

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

abanmitra
  • 861
  • 8
  • 6
1

If destination file is read only use /y/r

xcopy /y/r source.txt dest.txt
Proggear
  • 518
  • 4
  • 8
0

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,487
  • 16
  • 19