Search code examples
filebatch-filecopyxcopy

Batch command to not copy a file if exists and then to delete the file


I am trying to create a batch file that will Copy files from say d:\temp to c:\temp and if the file already exists on c:\temp to actually delete it from c:\temp, do not attempt to copy that file and continue copying remaining files. Repeat if exists etc.


Solution

  • Here you go

    for /r D:\temp %%a in (*) do (
        if exist "C:\temp\%%~nxa" (
            del "C:\temp\%%~nxa" /f /s /q
        ) else (
            copy "%%a" C:\temp
        )
    )