Windows Tip: How To Copy Files without Overwriting Them in Command Line

If you are copying files using drag-drop or copy/paste, you may simply choose “Skip this file” or “Skip these files” option to not overwrite the files that are already existed at the destination folder.

Or, if you are using command line copy, you can answer N to bypass these files that are already existed.

But how can I do the same through a batch file using any copy command line?

There are two ways of doing it.

You can either use the IF statement to copy the file only when the file does not exist in the destination.

IF NOT EXIST "topathfilename" COPY "frompathfile" "topathfile"

A friend of NoW, Glenn Reimche, shared a much better way using copy command line without the IF statement.

echo n | copy /-y "frompathfile" "topathfile"

If you need to copy a folder to another location but do not overwrite the files already existed in the new location, here is what you can do.

FOR %f in ("frompathfile*.*) DO IF NOT EXIST "topath%~nxf" COPY "frompathfile" "topathfile"

The %~nx in the command is to strip off the folder info from the variable %f so you only get the filename as the result.

Or, use a much powerful copy command line alternative instead, i.e. robocopy.

You can use the following switches to skip certain files.

/xc - exclude changed files
/xn - exclude newer files
/xo - exclude older files

For example, to copy all files from folder A to folder B excluding all files that have the newer timestamp.

robocopy frompath topath /s /xn

Hope it helps and please share if you have a better solution.

Source