Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for checkvarsvbs.bat

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

  1. @ECHO OFF
  2. IF "%~1"=="" GOTO Syntax
  3. FOR %%A IN (%*) DO IF "%%~A"=="/?" GOTO Syntax
  4. IF NOT EXIST "%~f1" GOTO Syntax
  5. IF /I NOT "%~x1"==".hta" IF /I NOT "%~x1"==".vbs" GOTO Syntax
  6. IF NOT "%~2"=="" (
  7. 	FOR /F "tokens=1*" %%A IN ('ECHO.%*') DO (
  8. 		FOR %%C IN (%%B) DO (
  9. 			IF /I NOT "%%~C"=="/S" IF /I NOT "%%~C"=="/U" IF /I NOT "%%~C"=="/V" GOTO Syntax
  10. 		)
  11. 	)
  12.  
  13. )
  14. RXGREP /? 2>&1 | FIND /I "RXGREP" >NUL
  15. IF ERRORLEVEL 1 (
  16. 	ECHO.
  17. 	ECHO ERROR: This batch file requires RXGREP.EXE, which wasn't found
  18. 	ECHO        on this computer. RXGREP.EXE can be downloaded from:
  19. 	ECHO        http://www.robvanderwoude.com/csharpexamples.php#RxGrep
  20. 	GOTO Syntax
  21. )
  22.  
  23. SETLOCAL ENABLEDELAYEDEXPANSION
  24.  
  25. SET ShowSubs=1
  26. SET ShowVars=1
  27. SET UnusedOnly=0
  28. IF NOT "%~2"=="" (
  29. 	FOR /F "tokens=1*" %%A IN ('ECHO.%*') DO (
  30. 		FOR %%C IN (%%B) DO (
  31. 			IF /I "%%~C"=="/S" SET ShowVars=0
  32. 			IF /I "%%~C"=="/U" SET UnusedOnly=1
  33. 			IF /I "%%~C"=="/V" SET ShowSubs=0
  34. 		)
  35. 	)
  36. )
  37. :: /S and /V are mutually exclusive
  38. SET /A Test = %ShowSubs% + %ShowVars%
  39. IF %Test% EQU 0 GOTO Syntax
  40.  
  41. TITLE Initializing . . .
  42. IF DEFINED Subroutine. (
  43. 	FOR /F "delims==" %%A IN ('SET Subroutine.') DO (
  44. 		SET %%A=
  45. 	)
  46. )
  47. IF DEFINED Variable. (
  48. 	FOR /F "delims==" %%A IN ('SET Variable.') DO (
  49. 		SET %%A=
  50. 	)
  51. )
  52. SET RC=0
  53. SET RC1=0
  54. SET RC2=0
  55.  
  56. IF %ShowSubs% EQU 1 (
  57. 	TITLE Generating list of subroutines . . .
  58. 	FOR /F "tokens=1* delims=( " %%A IN ('RXGREP.EXE "%~f1" "[\r\n]\s*(Sub|Function) [A-Z][^\(\s]+" /I 2^>NUL') DO (
  59. 		FOR %%C IN (%%B) DO (
  60. 			SET Subroutine.%%C=0
  61. 		)
  62. 	)
  63. 	FOR /F "tokens=2 delims=.=" %%A IN ('SET Subroutine. 2^>NUL') DO (
  64. 		TITLE Checking usage of subroutine %%A . . .
  65. 		FOR /F %%B IN ('RXGREP.EXE "%~f1" "\b%%~A\b" /I 2^>NUL ^| FIND /I /C "%%~A"') DO (
  66. 			SET /A Subroutine.%%A = %%B - 1
  67. 		)
  68. 	)
  69. )
  70.  
  71. IF %ShowVars% EQU 1 (
  72. 	TITLE Generating list of defined variables . . .
  73. 	REM Parse all variables up to and including first array
  74. 	FOR /F "tokens=1*" %%A IN ('RXGREP.EXE "%~f1" "\s*Dim [^\n\r\(]+" 2^>NUL /I') DO (
  75. 		FOR %%C IN (%%B) DO (
  76. 			SET Variable.%%C=0
  77. 		)
  78. 	)
  79. 	REM parse variables after first array if applicable
  80. 	SET Line=
  81. 	FOR /F "tokens=1*" %%A IN ('RXGREP.EXE "%~f1" "\s*Dim [^\n\r]+\([^\n\r]+" /I 2^>NUL ^| RXGREP.EXE "[^\(\)]+" 2^>NUL') DO (
  82. 		SET Line=%%~B
  83. 	)
  84. 	:Loop
  85. 	FOR /F "tokens=1*" %%A IN ('ECHO "!Line!" ^| RXGREP.EXE "\s*Dim [^\n\r]+\([^\n\r]+" /I 2^>NUL ^| RXGREP.EXE "[^\(\)]+" 2^>NUL') DO (
  86. 		FOR %%C IN (%%A) DO (
  87. 			SET Variable.%%C=0
  88. 		)
  89. 		SET Line=%%B
  90. 	)
  91. 	IF NOT "!Line!"=="" GOTO Loop
  92. 	FOR /F "tokens=2 delims=.=" %%A IN ('SET Variable. 2^>NUL') DO (
  93. 		TITLE Checking usage of variable %%A . . .
  94. 		FOR /F %%B IN ('RXGREP.EXE "%~f1" "\b%%~A\b" /I 2^>NUL ^| FIND /I /C "%%~A"') DO (
  95. 			SET /A Variable.%%A = %%B - 1
  96. 		)
  97. 	)
  98. )
  99.  
  100. TITLE Done
  101.  
  102. IF %ShowSubs% EQU 1 (
  103. 	:: Count number of unused subroutines
  104. 	FOR /F %%A IN ('SET Subroutine.^| FIND.EXE "=0" ^| FIND.EXE /C "."') DO SET RC1=%%A
  105. 	:: Format output
  106. 	IF !RC1! EQU 1 (
  107. 		SET UnusedTitle=1 unused subroutine:
  108. 		SET UnusedUnderline=^====================
  109. 	) ELSE (
  110. 		SET UnusedTitle=!RC1! unused subroutines:
  111. 		SET UnusedUnderline=^=====================
  112. 	)
  113. 	IF !RC1! GTR 9 (
  114. 		SET UnusedUnderline=^=!UnusedUnderline!
  115. 	)
  116. 	IF !RC1! GTR 99 (
  117. 		SET UnusedUnderline=^=!UnusedUnderline!
  118. 	)
  119. 	IF !RC1! GTR 999 (
  120. 		SET UnusedUnderline=^=!UnusedUnderline!
  121. 	)
  122.  
  123. 	IF %UnusedOnly% EQU 1 (
  124. 		(SET Subroutine.) >NUL 2>&1
  125. 		IF ERRORLEVEL 1 GOTO CountUnusedVars
  126. 		ECHO.
  127. 		ECHO !UnusedTitle!
  128. 		ECHO !UnusedUnderline!
  129. 		FOR /F "tokens=2 delims=.=" %%A IN ('SET Subroutine.^| FIND.EXE "=0"') DO ECHO.%%A
  130. 	) ELSE (
  131. 		ECHO.
  132. 		(SET Subroutine.) >NUL 2>&1
  133. 		IF ERRORLEVEL 1 (
  134. 			ECHO No subroutines found
  135. 			GOTO CountUnusedVars
  136. 		)
  137. 		ECHO Defined subroutines:
  138. 		ECHO.====================
  139. 		SET Subroutine.
  140. 		IF !RC1! EQU 0 (
  141. 			ECHO.
  142. 			ECHO No unused subroutines
  143. 		) ELSE (
  144. 			ECHO.
  145. 			ECHO !UnusedTitle!
  146. 			ECHO !UnusedUnderline!
  147. 			SET Subroutine.| FIND.EXE "=0"
  148. 		)
  149. 	)
  150. )
  151.  
  152. :CountUnusedVars
  153. IF %ShowVars% EQU 1 (
  154. 	:: Count number of unused variables
  155. 	FOR /F %%A IN ('SET Variable.^| FIND.EXE "=0" ^| FIND.EXE /C "."') DO SET RC2=%%A
  156. 	:: Format output
  157. 	IF !RC2! EQU 1 (
  158. 		SET UnusedTitle=1 unused variable:
  159. 		SET UnusedUnderline=^==================
  160. 	) ELSE (
  161. 		SET UnusedTitle=!RC2! unused variables:
  162. 		SET UnusedUnderline=^===================
  163. 	)
  164. 	IF !RC2! GTR 9 (
  165. 		SET UnusedUnderline=^=!UnusedUnderline!
  166. 	)
  167. 	IF !RC2! GTR 99 (
  168. 		SET UnusedUnderline=^=!UnusedUnderline!
  169. 	)
  170. 	IF !RC2! GTR 999 (
  171. 		SET UnusedUnderline=^=!UnusedUnderline!
  172. 	)
  173.  
  174. 	IF %UnusedOnly% EQU 1 (
  175. 		(SET Variable.) >NUL 2>&1
  176. 		IF ERRORLEVEL 1 GOTO Done
  177. 		ECHO.
  178. 		ECHO !UnusedTitle!
  179. 		ECHO !UnusedUnderline!
  180. 		FOR /F "tokens=2 delims=.=" %%A IN ('SET Variable.^| FIND.EXE "=0"') DO ECHO.%%A
  181. 	) ELSE (
  182. 		(SET Variable.) >NUL 2>&1
  183. 		IF ERRORLEVEL 1 (
  184. 			ECHO No variables found
  185. 			GOTO Done
  186. 		)
  187. 		ECHO.
  188. 		ECHO Defined variables:
  189. 		ECHO.==================
  190. 		SET Variable.
  191. 		IF !RC2! EQU 0 (
  192. 			ECHO.
  193. 			ECHO No unused variables
  194. 		) ELSE (
  195. 			ECHO.
  196. 			ECHO !UnusedTitle!
  197. 			ECHO !UnusedUnderline!
  198. 			SET Variable. | FIND.EXE "=0"
  199. 		)
  200. 	)
  201. )
  202.  
  203. :Done
  204. SET /A RC = !RC1! + !RC2!
  205. ENDLOCAL & EXIT /B %RC%
  206. GOTO:EOF
  207.  
  208.  
  209. :Syntax
  210. ECHO.
  211. ECHO CheckVarsVBS.bat,  Version 2.00
  212. ECHO Check subroutines and variables usage in VBScript and HTA files
  213. ECHO.
  214. ECHO Usage:  CHECKVARSVBS  vbsfile  [ /S ^| /V ]  [ /U ]
  215. ECHO.
  216. ECHO Where:  "vbsfile"     is the VBScript ot HTA file to be examined
  217. ECHO         /S            tests Subroutines only
  218. ECHO                       ^(default: test subroutines as well as variables^)
  219. ECHO         /U            list Unused subroutines and variables only
  220. ECHO                       ^(default: list all subroutines and variables^)
  221. ECHO         /V            tests Variables only
  222. ECHO                       ^(default: test subroutines as well as variables^)
  223. ECHO.
  224. ECHO Notes:  Output format is "Subroutine.name=num" and "Variable.name=num" where
  225. ECHO         "name" is a subroutine name specified by a Sub or Function command,
  226. ECHO         or a variable name specified by a Dim command, and "num" is the
  227. ECHO         number of times the subroutine or variable is used in the script.
  228. ECHO         The return code ^("errorlevel"^) equals the number of unused subroutines
  229. ECHO         and/or variables, or will be -1 in case of ^(command line^) errors.
  230. ECHO.
  231. ECHO Written by Rob van der Woude
  232. ECHO http://www.robvanderwoude.com
  233. EXIT /B -1
  234.  

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