113

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?

FZs
  • 16,581
  • 13
  • 41
  • 50
  • 37
    use `/Y` option. – npocmaka Oct 31 '17 at 09: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 Oct 31 '17 at 09: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 Oct 31 '17 at 13:26
  • @Aacini, If a user isn't computer orientated, it would be foolhardy to even consider the command line, _(there are many GUI methods for backing up files and directories)_. Choosing the command line route a decision was made to use a depracated command even over `Copy` and include some of its options. This reasonably implies an ability to read and understand command help output. – Compo Oct 31 '17 at 15:03
  • 2
    Thank you! I tried `/Y`, its good! – FZs Nov 05 '17 at 16:35
  • 1
    Possible duplicate of [How to overwrite existing files in batch?](https://stackoverflow.com/questions/4051294/how-to-overwrite-existing-files-in-batch) – OhadR Jul 18 '19 at 06:35
  • /y help me, thanks – Venkat Ch Mar 10 '20 at 12:59

2 Answers2

195

The solution is the /Y switch:

xcopy "C:\Users\ADMIN\Desktop\*.*" "D:\Backup\" /K /D /H /Y
FZs
  • 16,581
  • 13
  • 41
  • 50
  • 2
    the /Y option only overwrites the pre-existing files: https://www.lifewire.com/xcopy-command-2618103 – ttfreeman Apr 07 '20 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 Apr 07 '20 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 Apr 07 '20 at 20:59
2

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