如果目标中存在文件,则批量复制和覆盖文件

时间:2016-08-29 01:06:39

标签: windows batch-file

我在SE上找到了以下批处理脚本:

Batch Script - IF EXIST copy to %localappdata% error

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

如何修改脚本以对充满文件的目录进行操作?我试过了,但它没有工作:

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

2 个答案:

答案 0 :(得分:2)

XHR命令包含更新开关,仅复制目标文件夹中已存在的文件。

xcopy

答案 1 :(得分:1)

你可以尝试这样的事情:

@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