0

我需要一个批处理命令,它将在复制到目标后保留文件的备份。

假设我在源目录中有 2 个文件:

"1.txt" which has content (abc)  
"2.txt" which has content (abc)  

我在目标目录中有 4 个文件:

"1.txt" which has content (xyz)  
"2.txt" which has content (xyz)  
"5.txt" which has content (xyz)  
"6.txt" which has content (xyz)    

现在我必须将所有文本文件从源目录复制到目标目录,但在这种情况下,由于目标目录已经存在两个文本文件(1.txt 和 2.txt),我们需要在从源复制之前对其进行备份文件夹(可能类似于 1.txt.bkup 2.txt.bkup)。

从源复制到目标后,我的目标的内容应该是:

"1.txt.bkup" which has content (xyz)  
"2.txt.bkup" which has content (xyz)  
"5.txt" which has content (xyz)  
"6.txt" which has content (xyz)  
"1.txt" which has content (abc)  
"2.txt" which has content (abc)    

如何才能做到这一点?希亚姆

4

1 回答 1

3

尝试这个:

cd /d sourcefolder
for %%a in (*.txt) do (
    if exist "destinationfolder\%%~a" (
        move /y "destinationfolder\%%~a" "destinationfolder\%%~a.bkup"
    )
    copy "%%~a" "destinationfolder\%%~a"
)
于 2013-07-26T08:51:45.920 回答