更有效的运行查找功能的方法

时间:2012-09-22 01:18:50

标签: batch-file

目前这个脚本相当不错,但它的速度很慢,经过几千个文件需要将近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

1 个答案:

答案 0 :(得分:2)

  • 你有太多不必要的CALL。 CALL是一个相对昂贵的命令,你的代码每个文件最多有11个CALL!我已经将它降低到每个文件1个CALL,它可能会被切割为零。

  • 您正在执行每个文件的大量FINDSTR命令(好吧,可能是轻微的夸大)。搜索非常简单。使用字符串搜索和替换来查找字符串而不是FINDSTR应该更快。

  • 我大大简化并优化了代码来统计文件并删除CSV文件。

  • :FindKB代码似乎比它应该的速度慢,但我一个人留下它。

  • 我完全重构了大部分代码,以便于维护。

有些事情对我来说不合适,但我一个人离开了:

  • 我没有看到{TIDLE部分%ProgressFormat:!Percent!%如何运作

  • 您定义MS但不使用它。

  • 您有条件地定义了操作系统,但我看不到您在哪里使用它。

  • 您假设许多变量开始未定义。它们可能应该显式初始化为undefined。

  • 你的:SwitchFix代码似乎有缺陷

    • 您似乎正在检查扩展程序,但是您传递了名称
    • 看起来像拼写错误 / quite - > / quiet

这是一些经过严格修改的代码。如果没有几个错误我会感到震惊 - 这是完全未经测试的。但如果能让它发挥作用,那里有一些好主意。

@ECHO OFF
SETLOCAL enableDelayedExpansion
SET "Titlebar=Text File Generator"
TITLE %Titlebar%
SET "SystemType=Standard"

::FindMS - This is constant, only needs to be done once
::Actually I can't see where MS is used at all, so I don't think it is needed.
FOR /F "Tokens=2 Delims=/" %%A IN ("%Path%") DO SET "MS=%%A"

::FOR /F %%A IN ('DIR /B /A:-D ^|findstr "csv"') DO DEL /F %%A
::I would think DEL /F *.CSV would be better, but *CSV* matches original logic
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 /A MaxCnt=%%I
FOR /F "eol=: delims=" %%I IN ('DIR /S /B /A:-D') DO CALL :Generate "%%I" 2>Nul >Nul
EXIT /B


:Generate
SET /A CurentCnt+=1
SETLOCAL

::CALL:Progress %CurentCnt% %MaxCnt%
SET /A Percent=100*CurrentCnt/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
            )
        )
    )
)

::CALL:FindKB "%~n1"
::FindKB
set "val=%~n1"
set "val=!val:*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!"
)

FINDSTR /I "%KB%" "%~dp0\Script Dependencies\Superseded-patches.txt" >Nul && EXIT /B

::CALL:PathFix "%~1"
::PathFix
SET "FilePath=%~1"
SET "FilePath=!FilePath:*%~dp0=!"

::CALL:FindArchitecture "%~n1"
::FindArchitecture
set "Architecture="
set "val=%~n1"
for %%A in ( x64 x86 ia64 amd64 x86x64 ) do (
  if "!val:%%A=!" neq "!val!" (
    if not defined Architecture (set "Architecture=%%A") else set "Architecture=Error"
  )
)

::CALL:FindMS - This is a constant value so moved to front and done only once. I don't think it is needed at all

::CALL:FindOS "%~n1"
::FindOS
set "val=%~n1"
set "cmd="
for %%A in (
  "Windows2000=SET Windows2000=1"
  "WindowsXP=SET WindowsXP=1"
  "WindowsServer2003=SET Server2003=1"
  "Windows5.1=SET WindowsXP=1"
  "Windows5.2=SET Server2003=1"
  "Windows6.0=SET Vista=1"
  "Windows6.1=SET Windows7=1&SET Server2008=1"
  "Windows7=SET Windows7=1"
  "Vista=SET Vista=1"
  "Server2008=SET Server2008=1"
) do for /f "tokens=1* delims==" %%B in (%%A) do if "!val:%%B=!" neq "!val!" ( 
  if not defined cmd (set "cmd=%%C") else (
    set "cmd=!cmd!&%%C"
    set "OS=Error"
  )
)
(%cmd%)

::CALL:CustomRules "%~x1"
set "val=%~x1"
if "!val:972581=!" neq "!val!" SET "KB=KB972581"
set "cmd="
for %%A in (
  "MS09-035=set WinOS=All"
  "msxml4=set WinOS=All"
  "NDP40=set WinOS=All"
  "KB890830=set WinOS=All"
  "NetFramework=set WinOS=ALL"
  "msxml6=set Server2003=1"
  "NDP20=set WindowsXP=1&set Server2003=1"
  "KB969878=set WindowsXP=1&set Server2003=1"
  "WYukon2005Setup=set Server2003=1&set Server2008=1"
  "KB952068=set Server2008=1&set Server2003=1"
  "KB954600=set Server2008=1&set Server2003=1"
  "KB924430=set Server2000=1&set Server2003=1&set Architecture=x86"
  "messenger=set Server2000=1&set Server2003=1&set WindowsXP=1&set Architecture=x86"
  "msxml4=set WinOS=All&set Architecture=x86x64"
  "KB974882=set WinOS=All&set Architecture=x86x64"
  "vcredist=set WinOS=All"
  "WindowsServer2003=set Server2003=1"
  "KB974945=set WinOS=All"
  "WindowsMedia=set WinOS=All"
  "Windows-2000-XP=set WindowsXP=1&set Server2000=1"
  "KB960089=set Server2008=1&set Server2003=1"
  "KB960083=set WinOS=All"
  "KB2657424=set Server2008=1&set Vista=1&set Server2003=1&set WindowsXP=1"
  "KB2656370=set WindowsXP=1"
  "KB2541015=set Office2003=1&set Architecture=x86"
  "NDP=set WinOS=All"
  "vcredist=set WinOS=All&set Switch=/q"
  ";----------------------------------------------------------------"
  ";--Office--------------------------------------------------------"
  ";----------------------------------------------------------------"
  "Officexp=set WindowsXP=1&set Architecture=x86"
  "office2000=set Office2000=1&set Architecture=x86"
  "Office2003=set Office2003=1&set Architecture=x86"
  "outlook2003=set Office2003=1&set Architecture=x86"
  "Project2003=set Office2003=1&set Architecture=x86"
  ";----------------------------------------------------------------"
  ";--Office2007----------------------------------------------------"
  ";----------------------------------------------------------------"
  "Office2007=set Office2007=1"
  "publisher2007=set Office2007=1"
  "access2007=set Office2007=1"
  "outlook2007=set Office2007=1"
  "excelviewer2007=set Office2007=1"
  "excel2007=set Office2007=1"
  "word2007=set Office2007=1"
  "infopath2007=set Office2007=1"
  "powerpoint2007=set Office2007=1"
  "vbe62007=set Office2007=1"
  "graph2007=set Office2007=1"
  "pptconv2007=set Office2007=1"
  "xlview2007=set Office2007=1"
  "xlconv2007=set Office2007=1"
  "wordconv2007=set Office2007=1"
  ";----------------------------------------------------------------"
  ";--Office2010----------------------------------------------------"
  ";----------------------------------------------------------------"
  "Office2010=set Office2010=1"
  "Excel2010=set Office2010=1"
  "infopath2010=set Office2010=1"
  "vbe72010=set Office2010=1"
  "ace2010=set Office2010=1"
  "graph2010=set Office2010=1"
  "powerpoint2010=set Office2010=1"
  ";----------------------------------------------------------------"
  ";--WindowsMediaPlayer--------------------------------------------"
  ";----------------------------------------------------------------"
  "visio2001=set Office2001=1&set Architecture=x86"
  "Visio2002=set Office2002=1&set Architecture=x86"
  "visio2003=set Office2003=1&set Architecture=x86"
  "visio2007=set Office2007=1"
  "vviewer2010=set Office2010=1"
) do for /f "tokens=1* delims==" %%B in (%%A) do if "!val:%%B=!" neq "!val! (
  set "cmd=%%C"
  goto :CustomBreak
)
:CustomBreak
(%cmd%)

::CALL:SwitchFix "%~n1"
::SwitchFix
SET "Switch=/quite /norestart"
IF "%Office2003%"=="1" SET "Switch=/Q"
IF "%~n1"==".msu" SET "Switch=/quiet /norestart"

::CALL:FinalCheck
IF NOT DEFINED Architecture SET "Architecture=x86x64"

::CALL:Export
::Export
set "KB_EXTENSION="
if defined KB set "KB_EXTENSION=, ,http://support.microsoft.com/kb/%KB:~2%"
ECHO %Architecture%,%KB%,%FilePath%,%Switch%, ,%Windows2000%,%WindowsXP%,%Server2003%,%Vista%,%Server2008%,%Windows7%,%Office2000%,%Office2003%,%Office2007%,%Office2010%%KB_EXTENSION%>>MasterList.csv
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
)
if defined WinOS (
  set "Exported=1"
  for %%A in (
    Windows2000
    WindowsXP
    Server2003
    Vista
    Windows7
    Server2008
  ) do ECHO %Architecture%,%KB%,%FilePath%,%Switch%>>%SystemType%-%%A.csv
)
IF NOT DEFINED Exported ECHO %Architecture%,%KB%,%FilePath%,%Switch%>>Uncategorized.csv
EXIT /B