目前这个脚本相当不错,但它的速度很慢,通过几千个文件需要将近 3 个小时。有人有任何提示可以使脚本更高效或更好地工作吗?
该脚本非常严格,找到补丁、kb 编号、架构等,然后将它们放入正确的文件中。任何找不到匹配项的内容都会移动到“未分类”文件中,我可以在其中调整脚本以包含该文件。
@ECHO OFF
SETLOCAL enableDelayedExpansion
SET "Titlebar=Text File Generator"
TITLE %Titlebar%
SET "SystemType=Standard"
FOR /F %%A IN ('DIR /B /A:-D ^|findstr "csv"') DO DEL /F %%A
ECHO/ Generating Text Files. This may take up to 10 minutes...
ECHO Architecture,KB Number, File Path, Switch, ,Windows 2000,Windows XP,Server 2003,Windows Vista,Server 2008,Windows 7,Office 2000,Office 2003,Office 2007,Office 2010, ,KB Info URL>>Masterlist.csv
FOR /F "Tokens=*" %%I IN ('DIR /S /B /A:-D') DO SET /A MaxCnt+=1
FOR /F "Tokens=*" %%I IN ('DIR /S /B /A:-D') DO CALL:Generate "%%I" 2>Nul>Nul
EXIT /B
:Progress
SET /A Percent=100*%1/%2
SET Percent=!Percent!%%
TITLE %titlebar% %CurentCnt% Of %MaxCnt% (%ProgressFormat:!Percent!% Complete)
EXIT /B
:Generate
SET /A CurentCnt+=1
SETLOCAL
CALL:Progress %CurentCnt% %MaxCnt%
IF /I "%~x1" NEQ ".exe" (
IF /I "%~x1" NEQ ".msu" (
IF /I "%~x1" NEQ ".msi" (
IF /I "%~x1" NEQ ".msp" (
ECHO "%~1">>SkippedFiles.csv
EXIT /B
)
)
)
)
CALL:FindKB "%~n1"
FINDSTR /I "%KB%" "%~dp0\Script Dependencies\Superseded-patches.txt" >Nul && EXIT /B
CALL:PathFix "%~1"
CALL:FindArchitecture "%~n1"
CALL:FindMS
CALL:FindOS "%~n1"
CALL:CustomRules "%~x1"
CALL:SwitchFix "%~n1"
CALL:FinalCheck
CALL:Export
ENDLOCAL
GOTO:EOF
:Export
IF NOT DEFINED KB ECHO %Architecture%,%KB%,%FilePath%,%Switch%, ,%Windows2000%,%WindowsXP%,%Server2003%,%Vista%,%Server2008%,%Windows7%,%Office2000%,%Office2003%,%Office2007%,%Office2010%>>MasterList.csv
IF DEFINED KB ECHO %Architecture%,%KB%,%FilePath%,%Switch%, ,%Windows2000%,%WindowsXP%,%Server2003%,%Vista%,%Server2008%,%Windows7%,%Office2000%,%Office2003%,%Office2007%,%Office2010%, ,http://support.microsoft.com/kb/%KB:~2%>>MasterList.csv
IF DEFINED Windows2000 SET "Exported=1" & ECHO %Architecture%,%KB%,%FilePath%,%Switch%>>%SystemType%-Windows2000.csv
IF DEFINED WindowsXP SET "Exported=1" & ECHO %Architecture%,%KB%,%FilePath%,%Switch%>>%SystemType%-WindowsXP.csv
IF DEFINED Server2003 SET "Exported=1" & ECHO %Architecture%,%KB%,%FilePath%,%Switch%>>%SystemType%-Server2003.csv
IF DEFINED Vista SET "Exported=1" & ECHO %Architecture%,%KB%,%FilePath%,%Switch%>>%SystemType%-Vista.csv
IF DEFINED Windows7 SET "Exported=1" & ECHO %Architecture%,%KB%,%FilePath%,%Switch%>>%SystemType%-Windows7.csv
IF DEFINED Server2008 SET "Exported=1" & ECHO %Architecture%,%KB%,%FilePath%,%Switch%>>%SystemType%-Server2008.csv
IF DEFINED Office2000 SET "Exported=1" & ECHO %Architecture%,%KB%,%FilePath%,%Switch%>>%SystemType%-Office2000.csv
IF DEFINED Office2003 SET "Exported=1" & ECHO %Architecture%,%KB%,%FilePath%,%Switch%>>%SystemType%-Office2003.csv
IF DEFINED Office2007 SET "Exported=1" & ECHO %Architecture%,%KB%,%FilePath%,%Switch%>>%SystemType%-Office2007.csv
IF DEFINED Office2010 SET "Exported=1" & ECHO %Architecture%,%KB%,%FilePath%,%Switch%>>%SystemType%-Office2010.csv
IF DEFINED WinOS SET "Exported=1"
IF DEFINED WinOS ECHO %Architecture%,%KB%,%FilePath%,%Switch%>>%SystemType%-Windows2000.csv
IF DEFINED WinOS ECHO %Architecture%,%KB%,%FilePath%,%Switch%>>%SystemType%-WindowsXP.csv
IF DEFINED WinOS ECHO %Architecture%,%KB%,%FilePath%,%Switch%>>%SystemType%-Server2003.csv
IF DEFINED WinOS ECHO %Architecture%,%KB%,%FilePath%,%Switch%>>%SystemType%-Vista.csv
IF DEFINED WinOS ECHO %Architecture%,%KB%,%FilePath%,%Switch%>>%SystemType%-Windows7.csv
IF DEFINED WinOS ECHO %Architecture%,%KB%,%FilePath%,%Switch%>>%SystemType%-Server2008.csv
IF NOT DEFINED Exported ECHO %Architecture%,%KB%,%FilePath%,%Switch%>>Uncategorized.csv
EXIT /B
:FindArchitecture
ECHO "%~1" | FINDSTR /I "x64" && ( SET "Architecture=x64" & SET /A "Count+=1" )
ECHO "%~1" | FINDSTR /I "x86" && ( SET "Architecture=x86" & SET /A "Count+=1" )
ECHO "%~1" | FINDSTR /I "IA64" && ( SET "Architecture=ia64" & SET /A "Count+=1" )
ECHO "%~1" | FINDSTR /I "AMD64" && ( SET "Architecture=amd64" & SET /A "Count+=1" )
ECHO "%~1" | FINDSTR /I "x86x64" && ( SET "Architecture=x86x64" & SET /A "Count-=1" )
IF "%Count%" GTR "1" ( SET "Architecture=Error" )
SET Count=0
EXIT /B
:FindOS
ECHO "%~1" | FINDSTR /I "Windows2000" && ( SET "Windows2000=1" & SET /A "Count+=1" )
ECHO "%~1" | FINDSTR /I "WindowsXP" && ( SET "WindowsXP=1" & SET /A "Count+=1" )
ECHO "%~1" | FINDSTR /I "WindowsServer2003" && ( SET "Server2003=1" & SET /A "Count+=1" )
ECHO "%~1" | FINDSTR /I "Windows5.1" && ( SET "WindowsXP=1" & SET /A "Count+=1" )
ECHO "%~1" | FINDSTR /I "Windows5.2" && ( SET "Server2003=1" & SET /A "Count+=1" )
ECHO "%~1" | FINDSTR /I "Windows6.0" && ( SET "Vista=1" & SET /A "Count+=1" )
ECHO "%~1" | FINDSTR /I "Windows6.1" && ( SET "Windows7=1" & SET "Server2008=1" & SET /A "Count+=1" )
ECHO "%~1" | FINDSTR /I "Windows7" && ( SET "Windows7=1" & SET /A "Count+=1" )
ECHO "%~1" | FINDSTR /I "Vista" && ( SET "Vista=1" & SET /A "Count+=1" )
ECHO "%~1" | FINDSTR /I "Server2008" && ( SET "Server2008=1" & SET /A "Count+=1" )
IF "%Count%" GTR "1" ( SET "OS=Error" )
SET Count=0
EXIT /B
:FindKB
set val="%~1"
ECHO !val!|findstr /irc:"KB[0-9][0-9][0-9][0-9][0-9][0-9][0-9]" >nul && (
set "val=!val:*KB=!"
Set "KB=KB!val:~0,7!"
EXIT /B
)
ECHO !val!|findstr /irc:"KB[0-9][0-9][0-9][0-9][0-9][0-9]" >nul && (
set "val=!val:*KB=!"
Set "KB=KB!val:~0,6!"
EXIT /B
)
ECHO !val!|findstr /irc:"KB[0-9][0-9][0-9][0-9][0-9]" >nul && (
set "val=!val:*KB=!"
Set "KB=KB!val:~0,5!"
EXIT /B
)
EXIT /B
:FindMS
FOR /F "Tokens=2 Delims=/" %%A IN ("%Path%") DO SET "MS=%%A"
EXIT /B
:PathFix
SET "Path_to_convert=%~1"
SET "Reference_path=%~dp0"
SET "FilePath=!Path_to_convert:*%Reference_path%=!"
GOTO:EOF
:SwitchFix
SET "Switch=/quite /norestart"
IF "%Office2003%"=="1" SET "Switch=/Q"
IF "%~1"==".msu" SET "Switch=/quiet /norestart"
EXIT /B
:CustomRules
ECHO "%~1" | FINDSTR /I "972581" && ( SET "KB=KB972581" )
ECHO "%~1" | FINDSTR /I "MS09-035" && ( SET "WinOS=All" & EXIT /B )
ECHO "%~1" | FINDSTR /I "msxml4" && ( SET "WinOS=All" & EXIT /B )
ECHO "%~1" | FINDSTR /I "NDP40" && ( SET "WinOS=All" & EXIT /B )
ECHO "%~1" | FINDSTR /I "KB890830" && ( SET "WinOS=All" & EXIT /B )
ECHO "%~1" | FINDSTR /I "NetFramework" && ( SET "WinOS=ALL" & EXIT /B )
ECHO "%~1" | FINDSTR /I "msxml6" && ( SET "Server2003=1" & EXIT /B )
ECHO "%~1" | FINDSTR /I "NDP20" && ( SET "WindowsXP=1" & SET "Server2003=1" & EXIT /B )
ECHO "%~1" | FINDSTR /I "KB969878" && ( SET "WindowsXP=1" & SET "Server2003=1" & EXIT /B )
ECHO "%~1" | FINDSTR /I "WYukon2005Setup" && ( SET "Server2003=1" & SET "Server2008=1" & EXIT /B )
ECHO "%~1" | FINDSTR /I "KB952068" && ( SET "Server2008=1" & SET "Server2003=1" & EXIT /B )
ECHO "%~1" | FINDSTR /I "KB954600" && ( SET "Server2008=1" & SET "Server2003=1" & EXIT /B )
ECHO "%~1" | FINDSTR /I "KB924430" && ( SET "Server2000=1" & SET "Server2003=1" & SET "Architecture=x86" & EXIT /B )
ECHO "%~1" | FINDSTR /I "messenger" && ( SET "Server2000=1" & SET "Server2003=1" & SET "WindowsXP=1" & SET "Architecture=x86" & EXIT /B )
ECHO "%~1" | FINDSTR /I "msxml4" && ( SET "WinOS=All" & SET "Architecture=x86x64" & EXIT /B )
ECHO "%~1" | FINDSTR /I "KB974882" && ( SET "WinOS=All" & SET "Architecture=x86x64" & EXIT /B )
ECHO "%~1" | FINDSTR /I "vcredist" && ( SET "WinOS=All" & EXIT /B )
ECHO "%~1" | FINDSTR /I "WindowsServer2003" && ( SET "Server2003=1" & EXIT /B )
ECHO "%~1" | FINDSTR /I "KB974945" && ( SET "WinOS=All" & EXIT /B )
ECHO "%~1" | FINDSTR /I "WindowsMedia" && ( SET "WinOS=All" & EXIT /B )
ECHO "%~1" | FINDSTR /I "Windows-2000-XP" && ( SET "WindowsXP=1" & SET "Server2000=1" & EXIT /B )
ECHO "%~1" | FINDSTR /I "KB960089" && ( SET "Server2008=1" & SET "Server2003=1" & EXIT /B )
ECHO "%~1" | FINDSTR /I "KB960083" && ( SET "WinOS=All" & EXIT /B )
ECHO "%~1" | FINDSTR /I "KB2657424" && ( SET "Server2008=1" & SET "Vista=1" & SET "Server2003=1" & SET "WindowsXP=1" & EXIT /B )
ECHO "%~1" | FINDSTR /I "KB2656370" && ( SET "WindowsXP=1" & EXIT /B )
ECHO "%~1" | FINDSTR /I "KB2541015" && ( SET "Office2003=1" & SET "Architecture=x86" & EXIT /B )
ECHO "%~1" | FINDSTR /I "NDP" && ( SET "WinOS=All" & EXIT /B )
ECHO "%~1" | FINDSTR /I "vcredist" && ( SET "WinOS=All" & SET "Switch=/q" & EXIT /B )
::------------------------------------------------------------------
::-- Office --------------------------------------------------------
::------------------------------------------------------------------
ECHO "%~1" | FINDSTR /I "Officexp" && ( SET "WindowsXP=1" & SET "Architecture=x86" & EXIT /B )
ECHO "%~1" | FINDSTR /I "office2000" && ( SET "Office2000=1" & SET "Architecture=x86" & EXIT /B )
ECHO "%~1" | FINDSTR /I "Office2003" && ( SET "Office2003=1" & SET "Architecture=x86" & EXIT /B )
ECHO "%~1" | FINDSTR /I "outlook2003" && ( SET "Office2003=1" & SET "Architecture=x86" & EXIT /B )
ECHO "%~1" | FINDSTR /I "Project2003" && ( SET "Office2003=1" & SET "Architecture=x86" & EXIT /B )
::------------------------------------------------------------------
::-- Office 2007 ---------------------------------------------------
::------------------------------------------------------------------
ECHO "%~1" | FINDSTR /I "Office2007" && ( SET "Office2007=1" & EXIT /B )
ECHO "%~1" | FINDSTR /I "publisher2007" && ( SET "Office2007=1" & EXIT /B )
ECHO "%~1" | FINDSTR /I "access2007" && ( SET "Office2007=1" & EXIT /B )
ECHO "%~1" | FINDSTR /I "outlook2007" && ( SET "Office2007=1" & EXIT /B )
ECHO "%~1" | FINDSTR /I "excelviewer2007" && ( SET "Office2007=1" & EXIT /B )
ECHO "%~1" | FINDSTR /I "excel2007" && ( SET "Office2007=1" & EXIT /B )
ECHO "%~1" | FINDSTR /I "word2007" && ( SET "Office2007=1" & EXIT /B )
ECHO "%~1" | FINDSTR /I "infopath2007" && ( SET "Office2007=1" & EXIT /B )
ECHO "%~1" | FINDSTR /I "powerpoint2007" && ( SET "Office2007=1" & EXIT /B )
ECHO "%~1" | FINDSTR /I "vbe62007" && ( SET "Office2007=1" & EXIT /B )
ECHO "%~1" | FINDSTR /I "graph2007" && ( SET "Office2007=1" & EXIT /B )
ECHO "%~1" | FINDSTR /I "pptconv2007" && ( SET "Office2007=1" & EXIT /B )
ECHO "%~1" | FINDSTR /I "xlview2007" && ( SET "Office2007=1" & EXIT /B )
ECHO "%~1" | FINDSTR /I "xlconv2007" && ( SET "Office2007=1" & EXIT /B )
ECHO "%~1" | FINDSTR /I "wordconv2007" && ( SET "Office2007=1" & EXIT /B )
::------------------------------------------------------------------
::-- Office 2010 ---------------------------------------------------
::------------------------------------------------------------------
ECHO "%~1" | FINDSTR /I "Office2010" && ( SET "Office2010=1" & EXIT /B )
ECHO "%~1" | FINDSTR /I "Excel2010" && ( SET "Office2010=1" & EXIT /B )
ECHO "%~1" | FINDSTR /I "infopath2010" && ( SET "Office2010=1" & EXIT /B )
ECHO "%~1" | FINDSTR /I "vbe72010" && ( SET "Office2010=1" & EXIT /B )
ECHO "%~1" | FINDSTR /I "ace2010" && ( SET "Office2010=1" & EXIT /B )
ECHO "%~1" | FINDSTR /I "graph2010" && ( SET "Office2010=1" & EXIT /B )
ECHO "%~1" | FINDSTR /I "powerpoint2010" && ( SET "Office2010=1" & EXIT /B )
::------------------------------------------------------------------
::-- Windows Media Player ------------------------------------------
::------------------------------------------------------------------
ECHO "%~1" | FINDSTR /I "visio2001" && ( SET "Office2001=1" & SET "Architecture=x86" & EXIT /B )
ECHO "%~1" | FINDSTR /I "Visio2002" && ( SET "Office2002=1" & SET "Architecture=x86" & EXIT /B )
ECHO "%~1" | FINDSTR /I "visio2003" && ( SET "Office2003=1" & SET "Architecture=x86" & EXIT /B )
ECHO "%~1" | FINDSTR /I "visio2007" && ( SET "Office2007=1" & EXIT /B )
ECHO "%~1" | FINDSTR /I "vviewer2010" && ( SET "Office2010=1" & EXIT /B )
EXIT /B
:FinalCheck
IF NOT DEFINED Architecture SET "Architecture=x86x64"
EXIT /B
下面是我正在处理的脚本
@ECHO OFF
SETLOCAL enableDelayedExpansion
SET "Titlebar=Text File Generator"
TITLE %Titlebar%
SET "SystemType=Standard"
del /f *.CSV
ECHO/ Generating Text Files. This may take up to 10 minutes...
ECHO Architecture,KB Number, File Path, Switch, ,Windows 2000,Windows XP,Server 2003,Windows Vista,Server 2008,Windows 7,Office 2000,Office 2003,Office 2007,Office 2010, ,KB Info URL>>Masterlist.csv
FOR /F %%I IN ('DIR /S /B /A:-D ^| find /c /v ""') DO SET MaxCnt=%%I
FOR /F "Tokens=*" %%I IN ('DIR /S /B /A:-D') DO CALL:Generate "%%I"
EXIT /B
:Generate
SET /A CurentCnt+=1
SETLOCAL
SET /A Percent=100*%CurentCnt%/%MaxCnt%
SET Percent=!Percent!%%
TITLE %titlebar% %CurentCnt% Of %MaxCnt% (%ProgressFormat:!Percent!% Complete)
IF /I "%~x1" NEQ ".exe" (
IF /I "%~x1" NEQ ".msu" (
IF /I "%~x1" NEQ ".msi" (
IF /I "%~x1" NEQ ".msp" (
ECHO "%~1">>SkippedFiles.csv
EXIT /B
)
)
)
)
SET "Switch=/quiet /norestart"
SET "FileName=%~n1"
:: Find KB Number
set "val=!FileName:*KB=!"
ECHO !val!|findstr /brc:"[0-9][0-9][0-9][0-9][0-9][0-9][0-9]" >nul && (
Set "KB=KB!val:~0,7!"
) || ECHO !val!|findstr /brc:"[0-9][0-9][0-9][0-9][0-9][0-9]" >nul && (
Set "KB=KB!val:~0,6!"
) || ECHO !val!|findstr /brc:"[0-9][0-9][0-9][0-9][0-9]" >nul && (
Set "KB=KB!val:~0,5!"
)
::Check list of superseded patches, if superseded skip this function
FINDSTR /I "%KB%" "%~dp0\Script Dependencies\Superseded-patches.txt" 2>Nul>Nul && EXIT /B
::Echo Architecture
for %%A in ( x64 x86 ia64 amd64 x86x64 ) do (
if "!FileName:%%A=!" neq "!FileName!" (
if not defined Architecture (set "Architecture=%%A") else set "Architecture=Error"
)
)
::Final Check
IF "%Office2003%"=="1" SET "Switch=/Q"
IF NOT DEFINED Architecture SET "Architecture=x86x64"
::Export
IF defined KB set "KB_EXTENSION=, ,http://support.microsoft.com/kb/%KB:~2%"
IF DEFINED WinOS ( set "Exported=1" & FOR %%A in (Windows2000 WindowsXP Server2003 Vista Windows7 Server2008 Office2000 Office2003 Office2007 Office2010) DO SET "%%A=1" )
FOR %%A in (Windows2000,WindowsXP,Server2003,Vista,Windows7,Server2008,Office2000,Office2003,Office2007,Office2010) do if defined %%A ( set "Exported=1" & ECHO %Architecture%,%KB%,%FilePath%,%Switch%>>%SystemType%-%%A.csv )
ECHO %Architecture%,%KB%,%FilePath%,%Switch%, ,%Windows2000%,%WindowsXP%,%Server2003%,%Vista%,%Server2008%,%Windows7%,%Office2000%,%Office2003%,%Office2007%,%Office2010%%KB_EXTENSION%>>MasterList.csv
IF NOT DEFINED Exported ECHO %Architecture%,%KB%,%FilePath%,%Switch%>>Uncategorized.csv