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

9

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.

Replace or Skip Files 2017 07 30 22 12 56 - Windows Tip: How To Copy Files without Overwriting Them in Command Line

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

copy to bypass - Windows Tip: How To Copy Files without Overwriting Them in Command Line

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 "topath\filename" COPY "frompath\file" "topath\file"

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

echo n | copy /-y "frompath\file" "topath\file"

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

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 ("frompath\file\*.*) DO IF NOT EXIST "topath\%~nxf" COPY "frompath\file" "topath\file"

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.

9 COMMENTS

  1. hi,
    i tried rocoby command not work
    robocopy C:\Lab VIEW Report\INVERTER UL TEST REPORT\ C:\Users\TESTING01\Desktop\log\ /s /xn

    • It may be because your command is not specifying any files. Try something like this: C:\Lab VIEW Report\INVERTER UL TEST REPORT\*.* C:\Users\TESTING01\Desktop\log\ /s /xn

LEAVE A REPLY

Please enter your comment!
Please enter your name here