echo ╭═════════════════════╮
echo ╭═══┤ 本脚本由 rain_zheng 制作 QQ 112983066 ├═══╮
echo ║ ╰═════════════════════╯ ║
echo ║ 欢迎使用 系统补丁自动安装程序 批处理版 ║
echo ║ ║
echo ╰═════════════════════════════╯
title 自动安装补丁程序
color 0a
echo ********** 现在开始安装升级补丁,请耐心等待,请不要关闭此窗口!!! **********
echo 正在加载文件,请稍候.........
for /f "tokens=1 delims= " %%j in ('dir office /od /b') do office\%%j /q
for /f "tokens=1 delims= " %%j in ('dir windows /od /b') do windows\%%j /passive /norestart /quiet
IF EXIST M: GOTO DRIVET_OK
update.bat
exit
color 0a
REM By rain_zheng
SET TITLE=Windows 补丁安装脚本
TITLE %TITLE%
SETLOCAL ENABLEDELAYEDEXPANSION
SET PATCHFLAG=KB
SET CAT=%PATCHFLAG%*.cat
SET PATCHLIST="%temp%\patcheslist.tmp"
SET INSTALLED=√ 已安装
SET NOTINSTALLED=× 未安装
SET DELIMS=-----------------------------------------
SET PATCH_TOTAL=0
SET PATCH_NOTINSTALLED=0
SET FLAG_INSTALLED=1*
SET FLAG_NOTINSTALLED=0*
TITLE 正在搜索... -- %TITLE%
ECHO 正在搜索当前目录"%cd%"及其子目录下的所有补丁...
ECHO 如果想在搜索完成后立即安装未安装的补丁,请按回车。
ECHO.
REM 搜索补丁,没有搜索到则退出,否则继续。
CALL :pfind || (call :error 1& goto :eof)
ECHO.
:confirm
TITLE 请选择要安装的补丁 -- %TITLE%
ECHO 搜索到%PATCH_TOTAL%个补丁,其中%PATCH_NOTINSTALLED%个未安装。安装尚未安装的补丁,请直接按『回车』。
ECHO 在安装office组件补丁时,会有错误提示,请将此提示忽略,点击『确定』即可继续安装。
SET confirm=
SET /p confirm=
IF /i "%confirm%" == "ALL" (SET confirm=
) ELSE IF not defined confirm (SET confirm=%FLAG_INSTALLED:~0,1%
) ELSE GOTO confirm
ECHO %DELIMS%
ECHO.
ECHO 正在安装,请稍候...
ECHO.
CALL :setup %confirm%
del %PATCHLIST% 2>nul >nul
TITLE 安装已结束 -- %TITLE%
ECHO %DELIMS%
ECHO.
ECHO 安装已结束。要使补丁生效,你可能需要手动重新启动计算机。
ECHO 按任意键退出。
SET TITLE=
ENDLOCAL
PAUSE >nul
TITLE %ComSpec%
goto :eof
:setup
if "%1" == "" (set patchsum=%PATCH_TOTAL%) ELSE set patchsum=%PATCH_NOTINSTALLED%
set patch_counter=0
FOR /f "eol=%1 usebackq tokens=2,*" %%i in (%PATCHLIST%) DO (
set /a patch_counter+=1
TITLE !patch_counter!/%patchsum%-%%i -- %TITLE%
set nobackup=nobackup
echo %%i | find /i "%PATCHFLAG%8" 2>nul >nul && set nobackup=n
%%j /quiet /passive /norestart /!nobackup! 2>nul >nul
ECHO !patch_counter!/%patchsum% %%i √)
goto :eof
:pfind
SET REG=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
SET listinreg="%temp%\listinreg.tmp"
reg query %reg%>%listinreg%
del %PATCHLIST% 2>nul >nul
REM 补丁是一个压缩包
FOR /r %%i in (*%PATCHFLAG%*.exe) DO CALL :pfind.sub "%%~ni" "%%~fi" "%%~sfi"
REM 补丁在压缩包展开后的目录内
FOR /r %%i in (%cat%) DO IF exist %%~dpiupdate.exe FOR /f %%j in ("%%~dpiupdate.exe") DO CALL :pfind.sub "%%~ni" "%%~fj" "%%~sfj"
IF not exist %PATCHLIST% EXIT /b 1
sort %PATCHLIST% /o %PATCHLIST%
rem 得到补丁个数
FOR /f "tokens=3 delims= " %%i in ('find /c /i "%FLAG_NOTINSTALLED%" %PATCHLIST%') DO SET PATCH_NOTINSTALLED=%%i
FOR /f "tokens=3 delims= " %%i in ('find /c /i "%FLAG_INSTALLED%" %PATCHLIST%') DO SET /a PATCH_TOTAL=%%i + %PATCH_NOTINSTALLED%
del %listinreg% 2>nul >nul
IF not defined patch_total EXIT /b 2
IF %patch_total% LSS 1 EXIT /b 3
EXIT /b 0
goto :eof
REM 看看补丁安装了没,然后写到标准输出和%PATCHLIST%内
:pfind.sub
IF "%~3" == "" GOTO :eof
CALL :getkbnum %1
SET id=!errorlevel!
IF not "!id!" == "-1" (
find /i "!id!" %listinreg% 2>nul >nul && (
set status=%FLAG_INSTALLED%& set isinstalled=%INSTALLED%
)||(set status=%FLAG_NOTINSTALLED%& set isinstalled=%NOTINSTALLED%)
ECHO !status! %PATCHFLAG%!id! %2>>%PATCHLIST%
ECHO !isinstalled! %PATCHFLAG%!id! %3
)
goto :eof
:getkbnum
SETLOCAL ENABLEDELAYEDEXPANSION
SET str=%~1
IF not defined str EXIT /b -1
IF not defined PATCHFLAG SET PATCHFLAG=KB
echo %PATCHFLAG%>getsize.tmp
for %%i in (getsize.tmp) do SET /a offset=%%~zi-2
del getsize.tmp 2>nul >nul
SET start=0
REM 仅有%PATCHFLAG%则返回-1
IF /i "%str%" == "%PATCHFLAG%" EXIT /b -1
REM 删除%PATCHFLAG%前的字符,删除失败则返回-1
:getkbnum.findkb
IF "%~1" == "!str!" (IF "!str:~%start%,%offset%!" == "" (EXIT /b -1
) ELSE IF /i "!str:~%start%,%offset%!" == "%PATCHFLAG%" (SET str=!str:~%start%!
) ELSE (SET /a start+=1 & goto getkbnum.findkb))
REM 仅有%PATCHFLAG%则返回-1
SET str=!str:~%offset%!
IF "%str%" == "" EXIT /b -1
SET start=0
REM 保证%PATCHFLAG%后的第一个字符不是数字时,仍能返回-1
IF "!str:~%start%,1!" GTR "9" EXIT /b -1
IF "!str:~%start%,1!" LSS "0" EXIT /b -1
REM 返回%PATCHFLAG%后的数字
:getkbnum.findnum
IF "!str:~%start%,1!" GTR "9" EXIT /b !str:~0,%start%!
IF "!str:~%start%,1!" LSS "0" EXIT /b !str:~0,%start%!
SET /a start+=1
goto getkbnum.findnum
ENDLOCAL
EXIT /b -1
TITLE ERROR -- %TITLE%
IF "%1" == "1" (ECHO 没有找到补丁。使用的搜索规则是:CAT:%cat%,PATCHFLAG:%PATCHFLAG%。程序已结束。)
SET TITLE=
PAUSE >nul
TITLE %ComSpec%
goto :eof