本帖最后由 zhangop9 于 2011-8-21 21:45 编辑
http://bbs.bathome.net/thread-2498-1-1.html
BAT+RAR实现备份的批处理代码
这个能用,可是一个是我用的是完整路径,还有就想有个还原的功能,能不能改一下,还原到原位置- @ECHO OFF
-
-
-
- Title 备份@BAT by Bo QQ:104630166
-
- ::Ver 2008-11-16
-
-
-
- COLOR 1F
-
-
-
- ::SETLOCAL ENABLEDELAYEDEXPANSION
-
- ::SETLOCAL DISABLEDELAYEDEXPANSION
-
-
-
-
-
-
-
- Rem =========================参数整理===================
-
- ::帮助
-
- IF "%*" EQU "/?" CALL :BRHelp && SHIFT /1
-
- ::传递进来的参数进行整理
-
- IF /I "%*" EQU "SET" CALL :BRSet %预留给计划或定时备份时使用,未完成%
-
- :Parameter
-
- Rem ECHO -%1-
-
- SET "Parameter=%1"
-
- Rem ECHO -%Parameter%-
-
- IF NOT DEFINED Parameter GOTO EndParameter
-
- %BMRem% IF /I "%Parameter:~0,3%" EQU "BM-" ( SET "BackupMode=%Parameter:~3%" && SET "BMRem=Rem" ) && GOTO IFParameter
-
- %BLRem% IF /I "%Parameter:~0,3%" EQU "BL-" ( SET "BackupList=%Parameter:~3%" && SET "BLRem=Rem" ) && GOTO IFParameter
-
- %BORem% IF /I "%Parameter:~0,3%" EQU "BO-" ( SET "BackupOutput=%Parameter:~3%" && SET "BORem=Rem" ) && GOTO IFParameter
-
- %RarVRem% IF /I "%Parameter:~0,5%" EQU "RarV-" ( SET "RarV=%Parameter:~5%" && SET "RarVRem=Rem" ) && GOTO IFParameter
-
- %RarKRem% IF /I "%Parameter:~0,5%" EQU "RarK-" ( SET "RarK=%Parameter:~5%" && SET "RarKRem=Rem" ) && GOTO IFParameter
-
- %RarRIRem% IF /I "%Parameter:~0,6%" EQU "RarRI-" ( SET "RarRI=%Parameter:~6%" && SET "RarRIRem=Rem" ) && GOTO IFParameter
-
- %RarRVRem% IF /I "%Parameter:~0,6%" EQU "RarRV-" ( SET "RarRV=%Parameter:~6%" && SET "RarRVRem=Rem" ) && GOTO IFParameter
-
- %PRRem% IF /I "%Parameter:~0,3%" EQU "PR-" ( SET "PathRar=%Parameter:~3%" && SET "PRRem=Rem" ) && GOTO IFParameter
-
- ECHO. && ECHO. && ECHO %Parameter% && CALL :ERROR && EXIT /B
-
- :IFParameter
-
- SHIFT /1
-
- SET "Parameter="
-
- GOTO Parameter
-
- :EndParameter
-
- ::测试时显显示各参数的值
-
- IF 1 LSS 0 (
-
- ECHO 1.备份方式“%BackupMode%”
-
- ECHO 2.备份清单路径“%BackupList%”
-
- ECHO 3.输出“%BackupOutput%”
-
- ECHO 4.分卷“%RarV%”
-
- ECHO 5.锁定“%RarK%”
-
- ECHO 6.优先级“%RarRI%”
-
- ECHO 7.恢复卷“%RarRV%”
-
- ECHO 8.RAR.exe路径“PathRar”
-
- PAUSE
-
- )
-
-
-
-
-
- Rem =========================准备工作===================
-
- ::获取桌面的路径
-
- IF NOT DEFINED Desktop (
-
- ECHO WSCRIPT.ECHO CREATEOBJECT^("WSCRIPT.SHELL"^).REGREAD^("HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Desktop"^)>"%TEMP%\Tmp.vbs"
-
- FOR /F "DELIMS=" %%I IN ('CSCRIPT //NOLOGO "%TEMP%\Tmp.vbs"') DO ( CALL SET "Desktop=%%~I" )
-
- ERASE /F /Q /S "%TEMP%\Tmp~.vbs" 2>NUL 1>NUL
-
- )
-
- SET "DateTime=%DATE:~0,4%%DATE:~5,2%%DATE:~8,2%%Time:~0,2%%Time:~3,2%%Time:~6,2%"
-
-
-
-
-
- Rem =========================WinRar,变量PR===================
-
- ECHO.
-
- IF DEFINED PathRar GOTO ChPathRar
-
- :PathRar
-
- ::获取RAR.exe的路径
-
- ::REG QUERY "HKCR\WinRAR\DefaultIcon" /VE /F DATA /D 2>NUL 1>NUL || GOTO ChPathRar
-
- ::REG QUERY "HKCR\WinRAR\DefaultIcon" /VE 2>NUL 1>NUL || GOTO ChPathRar
-
- ECHO WSCRIPT.ECHO CREATEOBJECT("WSCRIPT.SHELL").REGREAD("HKCR\WinRAR\DefaultIcon\")>"%TEMP%\Tmp.vbs"
-
- FOR /F "DELIMS=" %%I IN ('CSCRIPT //NOLOGO "%TEMP%\Tmp.vbs"') DO ( CALL SET "PathRar=%%~dpI" )
-
- ERASE /F /Q /S "%TEMP%\Tmp~.vbs" 2>NUL 1>NUL
-
- Rem ECHO [%PathRar%] && PAUSE
-
- :ChPathRar
-
- IF %PathRar:~-1% EQU ^\ (
-
- SET "PathRar=%PathRar:~0,-1%"
-
- )
-
- SETLOCAL ENABLEDELAYEDEXPANSION
-
- IF NOT EXIST "%PathRar%\RAR.EXE" (
-
- IF EXIST "%ProgramFiles%\WinRAR\RAR.exe" ( CALL SET "PathRar=%ProgramFiles%\WinRAR\RAR.EXE" ) && GOTO EndPathRar
-
- IF /I "%TempPathRar%" EQU "Error" (
-
- ECHO. && ECHO. && ECHO ErrorLelel-PR && CALL :ERROR
-
- )
-
- ECHO.
-
- ECHO 系统未安装 WinRAR 软件,
-
- ECHO 或找不到 RAR.exe 文件,
-
- ECHO 请先安装 WinRAR 软件或
-
- ECHO 输入 RAR.exe 所在路径,
-
- ECHO [回车] 退出
-
- ECHO.
-
- SET "PathRar="
-
- SET /P PathRar=
-
- Rem SET "PathRar=!PathRar:^%=^!!"
-
- IF "!PathRar!" EQU "" ( EXIT /B )
-
- Rem ECHO TEST && ECHO !PATHRAR! && pause
-
- SET "TempPathRar=Error"
-
- ) && GOTO ChPathRar
-
- SETLOCAL DISABLEDELAYEDEXPANSION
-
- SET "PathRar=%PathRar%\RAR.EXE"
-
- :EndPathRar
-
- FOR /F "Skip=1 TOKENS=2" %%I IN ('"%PathRar%"/?') DO (
-
- SET "RarVer=%%I"
-
- GOTO CVRar
-
- )
-
- :CVRar
-
- IF "%RarVer%" LSS "3" (
-
- ECHO.
-
- ECHO RAR 版本不支持,程序即将退出!
-
- ECHO.
-
- PAUSE
-
- EXIT /B
-
- )
-
-
-
-
-
- Rem =========================备份方式,变量BM===================
-
- ECHO.
-
- IF DEFINED BM GOTO CHBackupMode
-
- ::备份方式的选择
-
- :BackupMode
-
- ECHO.
-
- ECHO 请选择备份方式:
-
- ECHO 完整备份
-
- ECHO [D] 差异备份
-
- ECHO 增量备份
-
- ECHO [H] 帮助
-
- ECHO.
-
- SET "BackupMode="
-
- SET /P BackupMode=请选择:
-
- :ChBackupMode
-
- IF /I "%BackupMode%" EQU "B" ( SET "DirA=A" ) && ( SET "RarAc=-AC" ) && GOTO EndBackupMode
-
- IF /I "%BackupMode%" EQU "D" ( SET "DirA=A:A" ) && ( SET "RarAc=" ) && GOTO EndBackupMode
-
- IF /I "%BackupMode%" EQU "I" ( SET "DirA=A:A" ) && ( SET "RarAc=-AC" ) && GOTO EndBackupMode
-
- IF /I "%BackupMode%" EQU "H" (
-
- ECHO.
-
- ECHO.
-
- ECHO.
-
- ECHO 完整备份:备份用户指定的全部文档,并标记
-
- ECHO 为“已备份”;
-
- ECHO 差异备份:备份用户指定文档中的“已更改”
-
- ECHO 或新建文档,不改变备件标记;
-
- ECHO 增量备份:备份用户指定文档中的“已更改”
-
- ECHO 或新建文档,并标记为“已备份”
-
- ) && GOTO BackupMode
-
- ECHO. && ECHO. && ECHO ErrorLelel-BM && CALL :ERROR && GOTO BackupMode
-
- :EndBackupMode
-
-
-
-
-
- Rem =========================备份清单,变量BL===================
-
- ECHO.
-
- ::Vista下不支持CMD窗口获取拖进来的文件路径
-
- ::备份清单
-
- :BackupList
-
- IF DEFINED BL GOTO ChBackupList
-
- IF NOT EXIST "%Desktop%\BackupList.txt" (
-
- ECHO ;备件清单:>"%Desktop%\BackupList.txt"
-
- ECHO ;请在下面填入将要备份的文件(夹)的路径>>"%Desktop%\BackupList.txt"
-
- ECHO ;多行输入。>>"%Desktop%\BackupList.txt"
-
- ECHO ;保存并退出,程序即可继续执行。>>"%Desktop%\BackupList.txt"
-
- ECHO ;↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓>>"%Desktop%\BackupList.txt"
-
- ECHO.>>"%Desktop%\BackupList.txt"
-
- ECHO.
-
- ECHO 请在桌面的 BackupList.txt 文件中
-
- ECHO 填入将要备份的文件(夹)的路径,
-
- ECHO 多行输入。
-
- NOTEPAD "%Desktop%\BackupList.txt"
-
- ) ELSE (
-
- ECHO.
-
- ECHO 请确认桌面的 BackupList.txt 文件中是备份文件的路径。
-
- ECHO 如果不是,请删除此文件。
-
- )
-
- SET "BackupList=%Desktop%\BackupList.txt"
-
- :ChBackupList
-
- ECHO //备份日期:%DATE%>>"%Temp%\RarFiles%DateTime%.lst
-
- ECHO //备份时间:%TIME%>>"%Temp%\RarFiles%DateTime%.lst
-
- ECHO.>>"%Temp%\RarFiles%DateTime%.lst
-
- FOR /F "EOL=;" %%I IN ( %BackupList% ) DO (
-
- Rem ECHO %%I && PAUSE
-
- DIR "%%~I" /%DirA% /B /S 2>NUL >>"%Temp%\RarFiles%DateTime%.lst"
-
- )
-
- :EndBackupList
-
-
-
-
-
- Rem =========================输出路径,变量BO===================
-
- ECHO.
-
- ::未涉及网络路径
-
- ::输出的路径
-
- IF DEFINED BO GOTO ChBackupOutput
-
- :BackupOutput
-
- ECHO.
-
- ECHO 备份文件将以此时的的日期和时间命名
-
- Rem ECHO %DateTime%
-
- ECHO.
-
- ECHO 请输入备份文件“Backup%DateTime%.RAR”的保存路径:
-
- ECHO.
-
- SET "BackupOutput="
-
- SET /P BackupOutput=
-
- :ChBackupOutput
-
- IF "%BackupOutput:~-1%" EQU "^\" SET "BackupOutput=%BackupOutput:~0,-1%"
-
- ::PUSHD %~DP0
-
- CD /D "%BackupOutput%" 2>NUL || MD %BackupOutput% 2>NUL || ( ECHO. && ECHO. && ECHO ErrorLelel-BO && CALL :ERROR && GOTO BackupOutput )
-
- ::POPD
-
- SET "BackupOutput=%BackupOutput%\Backup%DateTime%.RAR"
-
- :EndBackupOutput
-
-
-
-
-
- Rem =========================压缩===================
-
- ECHO.
-
- IF NOT DEFINED RarV SET "RarV=102400^*1024" %设置分卷压缩时分卷的大小,102400×1024等于100M%
-
- IF NOT DEFINED RarK SET "RarK=-K" %设置压缩文件是否被锁定%
-
- IF NOT DEFINED RarRI SET "RarRI=RI1:1" %设置压缩时的优先级和休眠时间%
-
- IF NOT DEFINED RarRV SET "RarRV=5" %设置恢复卷比例%
-
- "%PathRar%" A %RarAc% -AV- -EP3 -ISND %RarK% -M5 -%RarRI% -RR5P -RV%RarRV%P -T -V%RarV% -W%Temp% -z"%Temp%\RarFiles%DateTime%.lst" "%BackupOutput%" @"%Temp%\RarFiles%DateTime%.lst"
-
- :: RAR A -AC -AV- -EP3 -ISND -K -M5 -RI5:5 -RR5P -RV10P -T -V1048576 -W%Temp% -z"%Temp%\RarFiles%DateTime%.lst" "%Desktop%\Backup%DateTime%.rar" @"%Temp%\RarFiles%DateTime%.lst"
-
- IF ERRORLEVEL 2 ( SET "MSGBOXVBS=未知错误") && GOTO MSGBOXVBS
-
- IF ERRORLEVEL 1 ( SET "MSGBOXVBS=没有需要备份的文件" ) && GOTO MSGBOXVBS
-
- SET "MSGBOXVBS=备份数据已经完成"
-
- :MSGBOXVBS
-
- @ECHO MSGBOX " %MSGBOXVBS% ", ,"by Bo QQ:104630166" >%Temp%\MSG.VBS
-
- @"%Temp%\MSG.VBS"
-
- ERASE /F /S /Q /A "%Temp%\RarFiles%DateTime%.lst" 1>NUL
-
- ERASE /F /Q /S %Temp%\MSG.VBS 2>NUL 1>NUL
-
- EXIT /B
-
-
-
-
-
- Rem =========================报错===================
-
- :ERROR
-
- ECHO. && ECHO.
-
- COLOR 0C
-
- ECHO 错误,请重新输入:
-
- ECHO.
-
- PAUSE
-
- ECHO.
-
- COLOR 1F
-
- EXIT /B
-
-
-
-
-
- Rem =========================帮助===================
-
- :BRHelp
-
- ECHO.
-
- ECHO BM-[B^|D^|I] 备份方式(完整、差异、增量),例:BM-B
-
- ECHO.
-
- ECHO BL-[drive:[path[filename]]]
-
- ECHO 写有备份清单文件的完全路径及文件名,例:BL-"D:\backuprar.txt"
-
- ECHO.
-
- ECHO BO-[drive:[path]]
-
- ECHO 输出完整路径,不含文件名,例:BO-"D:\backup"
-
- ECHO.
-
- ECHO RarV-V^<大小^> 设置分卷压缩时分卷的大小,单位:M,例:RarV-V1048576
-
- ECHO.
-
- ECHO RarK-[K^|U] 设置压缩文件是否被锁定,例:RarK-U
-
- ECHO.
-
- ECHO RarRI-RI^<P^>[:^<S^>]
-
- ECHO 设置压缩时的优先级和休眠时间,例:RarRI-RI5:5
-
- ECHO 优先级0默认,1-最小,15-最大
-
- ECHO 休眠时间以毫秒为单位
-
- ECHO.
-
- ECHO RarRV-RV[N]P 设置恢复卷比例,百分比,例:RarRV-RV10P
-
- ECHO.
-
- ECHO PR-[drive:[path]] 文件 rxr.exe 路径
-
- ECHO.
-
- PAUSE
-
- EXIT /B
-
-
-
- Rem =========================计划任务===================
-
- :BRSet
复制代码
|