[windows] How do I write a batch script that copies one directory to another, replaces old files?

I'd like a batch script in Windows with which I can copy one directory to another. If this directory already exists, and then for each file that already exists in both with the same name and location, it should be overwritten, if it does not exists, it should just be added.

In the end it should be a batch script to which I can pass 2 arguments, source & destination.

This question is related to windows batch-file

The answer is


Have you considered using the "xcopy" command?

The xcopy command will do all that for you.


Just use xcopy /y source destination


Try this:

xcopy %1 %2 /y /e

The %1 and %2 are the source and destination arguments you pass to the batch file. i.e. C:\MyBatchFile.bat C:\CopyMe D:\ToHere


It seems that the latest function for this in windows 7 is robocopy.

Usage example:

robocopy <source> <destination> /e /xf <file to exclude> <another file>

/e copies subdirectories including empty ones, /xf excludes certain files from being copied.

More options here: http://technet.microsoft.com/en-us/library/cc733145(v=ws.10).aspx


Examples related to windows

"Permission Denied" trying to run Python on Windows 10 A fatal error occurred while creating a TLS client credential. The internal error state is 10013 How to install OpenJDK 11 on Windows? I can't install pyaudio on Windows? How to solve "error: Microsoft Visual C++ 14.0 is required."? git clone: Authentication failed for <URL> How to avoid the "Windows Defender SmartScreen prevented an unrecognized app from starting warning" XCOPY: Overwrite all without prompt in BATCH Laravel 5 show ErrorException file_put_contents failed to open stream: No such file or directory how to open Jupyter notebook in chrome on windows Tensorflow import error: No module named 'tensorflow'

Examples related to batch-file

'ls' is not recognized as an internal or external command, operable program or batch file '' is not recognized as an internal or external command, operable program or batch file XCOPY: Overwrite all without prompt in BATCH CanĀ“t run .bat file under windows 10 Execute a batch file on a remote PC using a batch file on local PC Windows batch - concatenate multiple text files into one How do I create a shortcut via command-line in Windows? Getting Error:JRE_HOME variable is not defined correctly when trying to run startup.bat of Apache-Tomcat Curl not recognized as an internal or external command, operable program or batch file Best way to script remote SSH commands in Batch (Windows)