[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文件操作] 分享自己做的一个备份的批处理。

本帖最后由 ptsdy 于 2012-10-12 12:40 编辑
  1. @echo off
  2. set e=%date%
  3. set f=%e:~,10%
  4. set f=%f:/=-%
  5. set g=%f:-=%
  6. set h=%g:~3,1%
  7. set t=%time:~,8%
  8. set s=%t::=.%
  9. set wb=%~n0info.txt
  10. if not exist "%wb%" (echo Dir------To---------Dir>"%wb%"
  11.    echo Source   ^|  Destination>>"%wb%")
  12. :backupinfo
  13. for %%k in ("%wb%") do ( cls & rem echo %%~zk
  14.     if %%~zk leq 50  (echo Please input backup information and save !!!
  15.        start /wait "" "%~dp0%wb%" & goto :backupinfo) )
  16. rem set Mapping source netpath=%wb:~,1%
  17. rem set Mapping source  localpath =%wb:~1,1%
  18. set pan1=%wb:~,1%:
  19. set pan2=%wb:~1,1%:
  20. :echo "%pan1%" "%pan2%" &pause
  21. set netuser=
  22. set netpwd=
  23. if defined netuser (
  24.    set netpwd="%netpwd%"
  25.    set netuser=/user:"%netuser%")
  26. subst %pan2% /d 2>nul >nul
  27. net use %pan1% /del /y 2>nul >nul
  28. echo Please choice:
  29. echo      1:(common backup,fast,a few file not updated)   
  30. echo      2:(detail backup,  slowly, all files checked)
  31. set /p choice=choice:
  32. del /f /s /q "%~dp0log %f% %s%.txt" >nul 2>nul
  33. echo Start Time: %date:~,10% %time:~,8% >>"%~dp0log %f% %s%.txt"
  34. echo ---------------------------------- >>"%~dp0log %f% %s%.txt"
  35. set num=0
  36. setlocal enabledelayedexpansion
  37. for /f  "eol=# skip=2 tokens=1,2 delims=|" %%i in ('type "%~dp0%wb%"') do (
  38.    if "%%j" equ "" echo Error:Not defined destination path>>"%~dp0log %f% %s%.txt" & exit /b
  39.    if "%h%" neq "2" goto :eof
  40.    for %%q in ("%%i") do (set m=%%~q
  41.         set n=!m:~,1!
  42.         if "!n!" neq "\" (cd /d %windir%\system32
  43.            subst %pan2% "%%i" 2>nul >nul || subst %pan2% "%%i\"  
  44.            if "!errorlevel!" neq "0" echo Error:Map localpath falure>>"%~dp0log %f% %s%.txt" & exit /b
  45.            if "%choice%" equ "1" (call :main10 %pan2% "%%i" "%%j") else call :main20 %pan2% "%%i" "%%j"
  46.            cd /d "%~dp0" & subst %pan2% /d 2>nul >nul
  47.            ) else (net use %pan1% "%%i" %netpwd% %netuser%
  48.            if "!errorlevel!" neq "0" echo Error:Map netpath falure>>"%~dp0log %f% %s%.txt" &exit /b
  49.            if "%choice%" equ "1" (call :main10 %pan1% "%%i" "%%j") else call :main20 %pan1% "%%i" "%%j"
  50.            cd /d "%~dp0" & net use %pan1% /del /y 2>nul >nul
  51.            )
  52.        )
  53.    )
  54. echo Total backup files number: %num%  >>"%~dp0log %f% %s%.txt"
  55. echo End   Time: %date:~,10% %time:~,8%   >>"%~dp0log %f% %s%.txt"
  56. start /wait "" "%~dp0log %f% %s%.txt"
  57. echo Save backup information and logs ??? n(not save) OR otherkey(save)
  58. set /p save=Whether save ? :
  59. if "%save%" equ "n" del /f /q "%~dp0log %f% *.txt" "%~dp0%wb%"
  60. endlocal & pause & exit /b
  61. ::.....................................................................
  62. :main10
  63. set "num1=0" & cd /d "%~dp0"
  64. echo [%time:~,8%] Common Backup:"%~2|%~3" >>"%~dp0log %f% %s%.txt"
  65. cd /d "%~1" && for /f  "delims=" %%m in ('dir /a-d /s /b /od') do (
  66.       if not exist "%%j%%~pm%%~nxm" (
  67.            xcopy /y /s /e /c /g /h "%%m" "%%j%%~pm" && set /a num+=1 && set /a num1+=1
  68.            )
  69.       )
  70. echo [%time:~,8%] Backup files number: %num1% >>"%~dp0log %f% %s%.txt"
  71. echo ***********************************************************************>>"%~dp0log %f% %s%.txt"
  72. goto :eof
  73. ::.....................................................................
  74. :main20
  75. set num2=0
  76. set "change=0" & cd /d "%~dp0"
  77. echo [%time:~,8%] Detail Backup:"%~2|%~3" >>"%~dp0log %f% %s%.txt"
  78. cd /d "%~1" && for /f  "delims=" %%m in ('dir /a-d /s /b /o-d') do (
  79.       if not exist "%%j%%~pm%%~nxm" (
  80.            xcopy /y /s /e /c /g /h "%%m" "%%j%%~pm" && set /a num+=1 && set /a num2+=1
  81.            ) else (for %%n in ("%%j%%~pm%%~nxm") do (
  82.                       if "%%~zn" neq "%%~zm" (set "tm=%%~tm" & set "tm=!tm::=.!" & set "tm=!tm: =-!" & set "tm=!tm:/=-!"
  83.                           echo          ●CHANGED #######%%i%%~pm%%~nxm>>"%~dp0log %f% %s%.txt"
  84.                           if not exist "%%j%%~pm%%~nm-C!tm!%%~xm" echo f | xcopy /y /s /e /c /g /h  "%%m" "%%j%%~pm%%~nm-C!tm!%%~xm" && set /a num+=1 &&set /a num2+=1 && set /a change+=1)                              
  85.                       )
  86.                   )
  87.       )
  88. echo            Copy changed number: %change% >>"%~dp0log %f% %s%.txt"
  89. echo [%time:~,8%] Backup files number: %num2% >>"%~dp0log %f% %s%.txt"
  90. echo ***********************************************************************>>"%~dp0log %f% %s%.txt"
  91. goto :eof
  92. :backupinfo.txt (this bat's name front two must be letters and not exist repeat letter disk!)
  93. :##### Left  |   source path ;  Right  |  destination path (Dont' have space)
  94. :##### [Dir--Dir] For example:  \\192.168.1.5\a\b\c|d:\m\n ; example: y:|d:  
复制代码

里面的“回声”该是 “echo”吧??

TOP

返回列表