Answers:
rsync --backup
会做你想做的。您可以使用旧文件指定后缀--suffix
,或使用旧文件移动到其他文件夹--backup-dir
。
此脚本将所有重复文件%DestinationFolder%
放入以变量$Backup_%date%_%time%
为%DestinationFolder%
前缀的根目录下的备份目录中,默认情况下为%prefix%
变量$Backup_
。您可以自定义此值。
@echo off
set Folder=
set DestinationFolder=
set prefix=$Backup_
set suffix=%date%_%time%
set suffix=%suffix:/=-%
set suffix=%suffix::=-%
cd /d "%Folder%"
for /R %%a in (.) do (
set PTH=%%a
call :DUP
)
xcopy /herkvy "%Folder%" "%DestinationFolder%" 1>nul 2>&1
goto :eof
:DUP
set PTH=%PTH:\.=%
call set PTH=%%PTH:%Folder%=%%
type nul>"%tmp%\FileList"
for /f "tokens=4,* delims= " %%b in ('dir /a "%Folder%%PTH%" ^| find "/" ^| find /i /v "<DIR>"') do (
echo %%c>>"%tmp%\FileList"
)
for /f "tokens=4,* delims= " %%d in ('dir /a "%DestinationFolder%%PTH%" ^| find "/" ^| find /i /v "<DIR>"') do (
find /i "%%e" "%tmp%\FileList" 1>nul 2>&1 && (
if not exist "%DestinationFolder%\%prefix%%suffix%%PTH%" md "%DestinationFolder%\%prefix%%suffix%%PTH%"
move "%DestinationFolder%%PTH%\%%e" "%DestinationFolder%\%prefix%%suffix%%PTH%\%%e" 1>nul 2>&1
)
)
exit /b
注意:请设置Folder
为要备份的文件夹的路径,并DestinationFolder
设置为目标路径(备份)文件夹没有Baclslash At The End!例:
正确:
set Folder=C:\project\files
set DestinationFolder=D:\Data\backup
错误:
set Folder=C:\project\files\
set DestinationFolder=D:\Data\backup\