Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for listprogs.bat

(view source code of listprogs.bat as plain text)

  1. @ECHO OFF
  2. IF NOT "%~3"=="" GOTO Syntax
  3. FOR %%A IN (%*) DO (
  4. 	IF "%%~A"=="/?" GOTO Syntax
  5. )
  6.  
  7. SETLOCAL ENABLEDELAYEDEXPANSION
  8.  
  9. :: Defaults
  10. SET Pattern=".*"
  11. SET Format=List
  12. SET SwitchCount=0
  13.  
  14. :: Parse command line
  15. FOR %%A IN (%*) DO (
  16. 	SET Argument=%%A
  17. 	IF "!Argument:~0,1!"=="/" (
  18. 		IF !SwitchCount! GTR 0 GOTO Syntax
  19. 		IF /I "%%A"=="/A" (
  20. 			SET Format=Aligned
  21. 			SET UseSeparator=0
  22. 		)
  23. 		IF /I "%%A"=="/S" (
  24. 			SET Format=Aligned
  25. 			SET UseSeparator=1
  26. 		)
  27. 		IF /I "%%A"=="/T" (
  28. 			SET Format=TabDelimited
  29. 		)
  30. 		IF "!Format!"=="List" GOTO Syntax
  31. 		SET /A SwitchCount += 1
  32. 	) ELSE (
  33. 		IF NOT "!Pattern!"==".*" GOTO Syntax
  34. 		SET Pattern="%%~A"
  35. 	)
  36. )
  37.  
  38. :: Prepare Aligned output
  39. IF "%Format%"=="Aligned" (
  40. 	SET Width=0
  41. 	FOR /F "skip=1 tokens=2" %%A IN ('MODE CON ^| MORE /E +2 ^| FIND /V "-"') DO (
  42. 		IF "!Width!"=="0" SET Width=%%A
  43. 	)
  44. 	SET Col2Width=25
  45. 	SET /A Col1Width = !Width! - !Col2Width! - 2
  46. 	SET WhiteLine=
  47. 	FOR /L %%W IN (0,1,!Width!) DO (SET WhiteLine= !WhiteLine!)
  48. 	IF "%UseSeparator%"=="1" (
  49. 		REM Create a line of hyphens almost as wide as the console
  50. 		SET Separator=
  51. 		FOR /L %%W IN (3,1,!Width!) DO (SET Separator=-!Separator!)
  52. 	)
  53. )
  54.  
  55. :: Iterate through the registry and add entries to the list
  56. FOR %%A IN (SOFTWARE SOFTWARE\Wow6432Node) DO (
  57. 	FOR /F "tokens=*" %%B IN ('REG Query HKEY_LOCAL_MACHINE\%%A\Microsoft\Windows\CurrentVersion\Uninstall 2^>NUL ^| FIND /I "HKEY_LOCAL_MACHINE"') DO (
  58. 		REM Check and display each valid result
  59. 		CALL :AddToList "%%~B"
  60. 	)
  61. )
  62.  
  63. :: Iterate through the (sorted) list and display entries in the requested format
  64. FOR /F "tokens=1* delims==" %%A IN ('SET _DisplayName.') DO (
  65. 	FOR /F "tokens=1* delims=." %%C IN ("%%~A") DO (
  66. 		CALL :Display "%%~D" "%%~B"
  67. 	)
  68. )
  69.  
  70. :: In case of Aligned output add a closing separaror line
  71. IF "%Format%"=="Aligned" IF "%UseSeparator%"=="1" ECHO.%Separator%
  72.  
  73. ENDLOCAL
  74. GOTO:EOF
  75.  
  76.  
  77. :AddToList
  78. :: Read DisplayName and DisplayVersion values
  79. FOR %%C IN (DisplayName DisplayVersion) DO (
  80. 	SET %%C=
  81. 	FOR /F "tokens=1,2*" %%D IN ('REG Query "%~1" /v "%%~C" 2^>NUL ^| FIND /I "%%~C"') DO SET %%D=%%~F
  82. )
  83. :: Abort if either value is empty
  84. IF "%DisplayName%"==""    GOTO:EOF
  85. IF "%DisplayVersion%"=="" GOTO:EOF
  86. :: Abort if the DisplayName does not match the regex pattern
  87. ECHO.%DisplayName%| FINDSTR /R /I /C:%Pattern% >NUL || GOTO:EOF
  88. SET "_DisplayName.%DisplayName%=%DisplayVersion%"
  89. IF /I "%Format%"=="List" SET "_Regkey.%DisplayName%=%~1"
  90. GOTO:EOF
  91.  
  92.  
  93. :Aligned
  94. :: Align the columns
  95. SET Displayname=%Displayname%%WhiteLine%
  96. SET DisplayVersion=%WhiteLine%%DisplayVersion%
  97. SET Displayname=!Displayname:~0,%Col1Width%!
  98. SET DisplayVersion=!DisplayVersion:~-%Col2Width%!
  99. :: Insert a separator line if requested by /S switch
  100. IF "%UseSeparator%"=="1" ECHO.%Separator%
  101. :: Display aligned values
  102. ECHO.%DisplayName%%DisplayVersion%
  103. GOTO:EOF
  104.  
  105.  
  106. :Display
  107. SET DisplayName=%~1
  108. SET DisplayVersion=%~2
  109. IF /I NOT "%Format%"=="List" (
  110. 	CALL :%Format%
  111. 	GOTO:EOF
  112. )
  113. FOR /F "tokens=1* delims==" %%E IN ('SET _RegKey.!DisplayName!') DO SET RegKey="%%~F"
  114. CALL :%Format% !RegKey!
  115. GOTO:EOF
  116.  
  117.  
  118. :List
  119. :: Display values as list
  120. ECHO Registry Key    = %~1
  121. ECHO Display Name    = %DisplayName%
  122. ECHO Display Version = %DisplayVersion%
  123. ECHO.
  124. GOTO:EOF
  125.  
  126.  
  127. :TabDelimited
  128. :: Display tab delimited values
  129. ECHO.%DisplayName%	%DisplayVersion%
  130. GOTO:EOF
  131.  
  132.  
  133. :Syntax
  134. ECHO.
  135. ECHO ListProgs.bat,  Version 1.11
  136. ECHO List "all" installed program names and versions found in the registry
  137. ECHO.
  138. ECHO Usage:  LISTPROGS  [ "pattern" ]  [ /A ^| /S ^| /T ]
  139. ECHO.
  140. ECHO Where:  "pattern"  limits output to DisplayName values matching the FINDSTR
  141. ECHO                    type regular expression "pattern"     (case insensitive)
  142. ECHO         /A         shows results in Aligned table           (default: List)
  143. ECHO         /S         aligned table with Separator lines       (default: List)
  144. ECHO         /T         shows results Tab delimited              (default: List)
  145. ECHO.
  146. ECHO Notes:  Searches HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall and
  147. ECHO         HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall
  148. ECHO         for subkeys that have both DisplayName and DisplayVersion set.
  149. ECHO         Registry keys are displayed in the default List output, but not in
  150. ECHO         Aligned output nor in Tab delimited output.
  151. ECHO         With Aligned output (/A) a window width of 110 columns or wider
  152. ECHO         is recommended - may be set with the command:  MODE CON COLS=110
  153. ECHO.
  154. ECHO Written by Rob van der Woude
  155. ECHO http://www.robvanderwoude.com
  156.  
  157. EXIT /B 1
  158.  

page last uploaded: 2017-04-06, 13:33