写了一个批处理,可以实现文件备份,自动对比删除冗余文件。

替换的原则是按照文件修改时间,替换掉修改时间教早的。

set desdir=D:\
set filetypes=pdf uvproj bat c h s lib cpp txt rar



del /s /q s:\backtempsrc.t>nul
del /s /q s:\backtempdes.t>nul
del /s /q s:\dellist.t>nul
cls
@echo off&setlocal enabledelayedexpansion


set /p sels="是否扫描数据冗余?(Y/N) sel="
if "%sels%"=="Y" goto section1
goto section2

:section1
echo 删除冗余文件-----------------------------------------
echo -文件扫描
for /f "delims=" %%i in ('cd') do set srcdir=%%i
for /f "delims=" %%i in ('dir /s /b') do (
set thistemp=%%i
echo !thistemp:%srcdir%=!>>s:\backtempsrc.t
)
for /f "delims=" %%i in ('dir /s /b "%desdir%"') do (
set thistemp=%%i
echo !thistemp:%desdir%=!>>s:\backtempdes.t
)
echo -文件对比
for /f "delims=" %%i in (s:\backtempdes.t) do (
set flags=1
for /f "delims=" %%j in (s:\backtempsrc.t) do (
if "%%i"=="%%j" set flags=0
)
if !flags!==1 (
echo %desdir%%%i
echo %%i>>s:\dellist.t
)
)
if exist s:\dellist.t (
for /f "delims=" %%i in (s:\dellist.t) do (
rd /s /q "%desdir%%%i">>nul
del /s /q "%desdir%%%i">>nul
)
)
echo 冗余文件删除完毕
echo *****************************************************
echo.
echo.


:section2
echo 开始备份:-------------------------------------------
for %%i in (%filetypes%) do (
xcopy *.%%i "%desdir%\" /S/I/d/h/r/y
)
echo _____________________________________________________
echo.
echo 备份完成 按任意键退出!
pause>nul

del /s /q s:\backtempsrc.t>nul
del /s /q s:\backtempdes.t>nul
del /s /q s:\dellist.t>nul

注意,我的电脑里面有个S盘,是个虚拟内存盘,所以讲临时生成的文件放在那里,如果没有的需要改下批处理里的s:\盘符。

(by:cntsw 转载请注明出处)

posted @ 2014-12-07 20:05  cntsw  阅读(856)  评论(0编辑  收藏  举报