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. IF         "%~1"==""     GOTO Syntax
  4. IF    NOT  "%~2"==""     GOTO Syntax
  5. IF /I NOT "%~x1"==".vbs" GOTO Syntax
  6. IF    NOT EXIST   "%~f1" GOTO Syntax
  7. RXGREP /? 2>&1 | FIND /I "RXGREP" >NUL || GOTO Syntax
  8.  
  9. SETLOCAL
  10.  
  11. TITLE Initializing . . .
  12. IF DEFINED Variable. (
  13. 	FOR /F "delims==" %%A IN ('SET Variable.') DO (
  14. 		SET %%A=
  15. 	)
  16. )
  17. SET RC=0
  18.  
  19. TITLE Generating list of defined variables . . .
  20. FOR /F "tokens=1*" %%A IN ('TYPE "%~f1" ^| FINDSTR /R /B /I /C:" *Dim "') DO (
  21. 	FOR %%C IN (%%B) DO (
  22. 		SET Variable.%%C=0
  23. 	)
  24. )
  25.  
  26. FOR /F "tokens=2 delims=.=" %%A IN ('SET Variable.') DO (
  27. 	TITLE Checking usage of variable %%A . . .
  28. 	FOR /F %%B IN ('TYPE "%~f1" ^| RXGREP "[^\n\r]*\b%%A\b[^\n\r]*" /I ^| FIND /I /C "%%~A"') DO (
  29. 		SET /A Variable.%%A = %%B - 1
  30. 	)
  31. )
  32.  
  33. TITLE Done
  34.  
  35. :: Count number of unused variables
  36. FOR /F %%A IN ('SET Variable. ^| FIND "=0" ^| FIND /C "."') DO SET RC=%%A
  37. :: Format output
  38. IF %RC% EQU 1 (
  39. 	SET UnusedTitle=1 unused variable:
  40. 	SET UnusedUnderline=^==================
  41. ) ELSE (
  42. 	SET UnusedTitle=%RC% unused variables:
  43. 	SET UnusedUnderline=^===================
  44. )
  45. IF %RC% GTR 9 (
  46. 	SET UnusedTitle=%RC% unused variables:
  47. 	SET UnusedUnderline=^====================
  48. )
  49. IF %RC% GTR 99 (
  50. 	SET UnusedUnderline=^=====================
  51. )
  52. IF %RC% GTR 999 (
  53. 	SET UnusedUnderline=^======================
  54. )
  55.  
  56. ECHO.
  57. ECHO Defined variables:
  58. ECHO.==================
  59. SET Variable.
  60. IF %RC% EQU 0 (
  61. 	ECHO.
  62. 	ECHO No unused variables
  63. ) ELSE (
  64. 	ECHO.
  65. 	ECHO>&2 %UnusedTitle%
  66. 	ECHO>&2 %UnusedUnderline%
  67. 	SET Variable. | FIND "=0" 1>&2
  68. )
  69.  
  70. ENDLOCAL & EXIT /B %RC%
  71. GOTO:EOF
  72.  
  73.  
  74. :Syntax
  75. ECHO.
  76. ECHO CheckVarsVBS.bat,  Version 1.01
  77. ECHO Check variable usage in VBScript code
  78. ECHO.
  79. ECHO Usage:  CHECKVARSVBS  vbscriptcode.vbs
  80. ECHO.
  81. ECHO Notes:  Output format is "Variable.var=num" where "var" is a variable name
  82. ECHO         specified with a Dim command, and "num" is the number of times the
  83. ECHO         variable is used in the script.
  84. ECHO         The summary list of unused variables is sent to standard error instead
  85. ECHO         of standard output, so by redirection ^(of standard output^) to NUL only
  86. ECHO         the unused variables will be listed on screen.
  87. ECHO         The return code ^("errorlevel"^) equals the number of unused variables
  88. ECHO         or 1 in case of ^(command line^) errors.
  89. ECHO.
  90. ECHO Written by Rob van der Woude
  91. ECHO http://www.robvanderwoude.com
  92. EXIT /B 1
  93.  

page last uploaded: 2016-12-15, 11:20