posfan12
posfan12

Reputation: 2651

Batch copy and overwrite files if they exist in the destination

I found the following batch script here on SE:

Batch Script - IF EXIST copy to %localappdata% error

IF EXIST "%localappdata%\foldername\filename" (COPY /Y "filename" "location")

How do I modify the script to operate on a directory full of files? I tried this but it didn't work:

IF EXIST "temp\*.*" (COPY /Y "artwork\*.*" "temp")

Upvotes: 1

Views: 7619

Answers (2)

Hackoo
Hackoo

Reputation: 18827

You can try something like that :

@echo off
set "FolderSource=%localappdata%\FolderTest"
Set "Target=%~dp0Location"
IF Not EXIST "%FolderSource%" echo "%FolderSource%" does not exist & pause>nul & exit
If Not Exist "%Target%" MD "%Target%"
For /f "delims=" %%a in ('Dir /b /s "%FolderSource%\*.*"') Do (
    IF EXIST "%Target%\%%a" ( COPY /Y "%%a" "%Target%"
        ) ELSE ( COPY "%%a" "%Target%"
    )
)
pause>nul

Upvotes: 2

MC ND
MC ND

Reputation: 70923

xcopy command includes a update switch to only copy files that are already present in the target folder.

xcopy /y /u "artwork\*" "temp" 

Upvotes: 4

Related Questions