122

I'm writing a batch program for copying all files newer than the destination from "C:\Users\ADMIN\Desktop" to "D:\Backup".

This code is works:

xcopy "C:\Users\ADMIN\Desktop\*.*" "D:\Backup\" /K /D /H

However, it asks for each existing destination file: Overwrite file [Yes / No / All]?

I want to overwrite all existing destination files without user intervention.

How can I solve this?

7
  • 39
    use /Y option.
    – npocmaka
    Commented Oct 31, 2017 at 9:32
  • 2
    Type XCOPY/? at the Command prompt, hit enter and read the help information on the command. Or better yet, try it with ROBOCOPY/? which superseded XCOPY way back when Windows Vista came out!
    – Compo
    Commented Oct 31, 2017 at 9:43
  • 2
    @Compo: Just as a personal comment: it is not easy to start to use ROBOCOPY if the users are not computer oriented (i.e. if they can't find the /Y switch from xcopy help screen). There are several complex details there...
    – Aacini
    Commented Oct 31, 2017 at 13:26
  • 2
    Thank you! I tried /Y, its good!
    – FZs
    Commented Nov 5, 2017 at 16:35
  • 1
    Possible duplicate of How to overwrite existing files in batch?
    – OhadR
    Commented Jul 18, 2019 at 6:35

2 Answers 2

206

The solution is the /Y switch:

xcopy "C:\Users\ADMIN\Desktop\*.*" "D:\Backup\" /K /D /H /Y
3
  • 2
    the /Y option only overwrites the pre-existing files: lifewire.com/xcopy-command-2618103
    – ttfreeman
    Commented Apr 7, 2020 at 20:50
  • 1
    In my case, I am looking to overwrite EVERYTHING. the contents of one folder to replace another folder's content
    – ttfreeman
    Commented Apr 7, 2020 at 20:50
  • 1
    I know, stupid windows. each one of them has a different problem. rmdir says folder is in use by another applicaion, Poweshell Remove-Item says dies not have permission and so on.
    – ttfreeman
    Commented Apr 7, 2020 at 20:59
4

I wanted to copy some backups from a share location (or server) to a local one by replace option, so I use this code and it worked well:

Xcopy "\\bi-srv\SQL\Backup" "E:\backup\sql\Backup" /c /i /e /h /y

just save it in a bat format and schedule it by windows task scheduler

1
  • It is enough to append that parameter => /y Commented May 13 at 11:33

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