(view source code of checkvarsvbs.bat as plain text)
@ECHO OFF
IF "%~1"=="" GOTO Syntax
FOR %%A IN (%*) DO IF "%%~A"=="/?" GOTO Syntax
IF NOT EXIST "%~f1" GOTO Syntax
IF /I NOT "%~x1"==".hta" IF /I NOT "%~x1"==".vbs" GOTO Syntax
IF NOT "%~2"=="" (
FOR /F "tokens=1*" %%A IN ('ECHO.%*') DO (
FOR %%C IN (%%B) DO (
IF /I NOT "%%~C"=="/S" IF /I NOT "%%~C"=="/U" IF /I NOT "%%~C"=="/V" GOTO Syntax
)
)
)
RXGREP /? 2>&1 | FIND /I "RXGREP" >NUL
IF ERRORLEVEL 1 (
ECHO.
ECHO ERROR: This batch file requires RXGREP.EXE, which wasn't found
ECHO on this computer. RXGREP.EXE can be downloaded from:
ECHO http://www.robvanderwoude.com/csharpexamples.php#RxGrep
GOTO Syntax
)
SETLOCAL ENABLEDELAYEDEXPANSION
SET ShowSubs=1
SET ShowVars=1
SET UnusedOnly=0
IF NOT "%~2"=="" (
FOR /F "tokens=1*" %%A IN ('ECHO.%*') DO (
FOR %%C IN (%%B) DO (
IF /I "%%~C"=="/S" SET ShowVars=0
IF /I "%%~C"=="/U" SET UnusedOnly=1
IF /I "%%~C"=="/V" SET ShowSubs=0
)
)
)
:: /S and /V are mutually exclusive
SET /A Test = %ShowSubs% + %ShowVars%
IF %Test% EQU 0 GOTO Syntax
TITLE Initializing . . .
IF DEFINED Subroutine. (
FOR /F "delims==" %%A IN ('SET Subroutine.') DO (
SET %%A=
)
)
IF DEFINED Variable. (
FOR /F "delims==" %%A IN ('SET Variable.') DO (
SET %%A=
)
)
SET RC=0
SET RC1=0
SET RC2=0
IF %ShowSubs% EQU 1 (
TITLE Generating list of subroutines . . .
FOR /F "tokens=1* delims=( " %%A IN ('RXGREP.EXE "%~f1" "[\r\n]\s*(Sub|Function) [A-Z][^\(\s]+" /I 2^>NUL') DO (
FOR %%C IN (%%B) DO (
SET Subroutine.%%C=0
)
)
FOR /F "tokens=2 delims=.=" %%A IN ('SET Subroutine. 2^>NUL') DO (
TITLE Checking usage of subroutine %%A . . .
FOR /F %%B IN ('RXGREP.EXE "%~f1" "\b%%~A\b" /I 2^>NUL ^| FIND /I /C "%%~A"') DO (
SET /A Subroutine.%%A = %%B - 1
)
)
)
IF %ShowVars% EQU 1 (
TITLE Generating list of defined variables . . .
REM Parse all variables up to and including first array
FOR /F "tokens=1*" %%A IN ('RXGREP.EXE "%~f1" "\s*Dim [^\n\r\(]+" 2^>NUL /I') DO (
FOR %%C IN (%%B) DO (
SET Variable.%%C=0
)
)
REM parse variables after first array if applicable
SET Line=
FOR /F "tokens=1*" %%A IN ('RXGREP.EXE "%~f1" "\s*Dim [^\n\r]+\([^\n\r]+" /I 2^>NUL ^| RXGREP.EXE "[^\(\)]+" 2^>NUL') DO (
SET Line=%%~B
)
:Loop
FOR /F "tokens=1*" %%A IN ('ECHO "!Line!" ^| RXGREP.EXE "\s*Dim [^\n\r]+\([^\n\r]+" /I 2^>NUL ^| RXGREP.EXE "[^\(\)]+" 2^>NUL') DO (
FOR %%C IN (%%A) DO (
SET Variable.%%C=0
)
SET Line=%%B
)
IF NOT "!Line!"=="" GOTO Loop
FOR /F "tokens=2 delims=.=" %%A IN ('SET Variable. 2^>NUL') DO (
TITLE Checking usage of variable %%A . . .
FOR /F %%B IN ('RXGREP.EXE "%~f1" "\b%%~A\b" /I 2^>NUL ^| FIND /I /C "%%~A"') DO (
SET /A Variable.%%A = %%B - 1
)
)
)
TITLE Done
IF %ShowSubs% EQU 1 (
:: Count number of unused subroutines
FOR /F %%A IN ('SET Subroutine.^| FIND.EXE "=0" ^| FIND.EXE /C "."') DO SET RC1=%%A
:: Format output
IF !RC1! EQU 1 (
SET UnusedTitle=1 unused subroutine:
SET UnusedUnderline=^====================
) ELSE (
SET UnusedTitle=!RC1! unused subroutines:
SET UnusedUnderline=^=====================
)
IF !RC1! GTR 9 (
SET UnusedUnderline=^=!UnusedUnderline!
)
IF !RC1! GTR 99 (
SET UnusedUnderline=^=!UnusedUnderline!
)
IF !RC1! GTR 999 (
SET UnusedUnderline=^=!UnusedUnderline!
)
IF %UnusedOnly% EQU 1 (
(SET Subroutine.) >NUL 2>&1
IF ERRORLEVEL 1 GOTO CountUnusedVars
ECHO.
ECHO !UnusedTitle!
ECHO !UnusedUnderline!
FOR /F "tokens=2 delims=.=" %%A IN ('SET Subroutine.^| FIND.EXE "=0"') DO ECHO.%%A
) ELSE (
ECHO.
(SET Subroutine.) >NUL 2>&1
IF ERRORLEVEL 1 (
ECHO No subroutines found
GOTO CountUnusedVars
)
ECHO Defined subroutines:
ECHO.====================
SET Subroutine.
IF !RC1! EQU 0 (
ECHO.
ECHO No unused subroutines
) ELSE (
ECHO.
ECHO !UnusedTitle!
ECHO !UnusedUnderline!
SET Subroutine.| FIND.EXE "=0"
)
)
)
:CountUnusedVars
IF %ShowVars% EQU 1 (
:: Count number of unused variables
FOR /F %%A IN ('SET Variable.^| FIND.EXE "=0" ^| FIND.EXE /C "."') DO SET RC2=%%A
:: Format output
IF !RC2! EQU 1 (
SET UnusedTitle=1 unused variable:
SET UnusedUnderline=^==================
) ELSE (
SET UnusedTitle=!RC2! unused variables:
SET UnusedUnderline=^===================
)
IF !RC2! GTR 9 (
SET UnusedUnderline=^=!UnusedUnderline!
)
IF !RC2! GTR 99 (
SET UnusedUnderline=^=!UnusedUnderline!
)
IF !RC2! GTR 999 (
SET UnusedUnderline=^=!UnusedUnderline!
)
IF %UnusedOnly% EQU 1 (
(SET Variable.) >NUL 2>&1
IF ERRORLEVEL 1 GOTO Done
ECHO.
ECHO !UnusedTitle!
ECHO !UnusedUnderline!
FOR /F "tokens=2 delims=.=" %%A IN ('SET Variable.^| FIND.EXE "=0"') DO ECHO.%%A
) ELSE (
(SET Variable.) >NUL 2>&1
IF ERRORLEVEL 1 (
ECHO No variables found
GOTO Done
)
ECHO.
ECHO Defined variables:
ECHO.==================
SET Variable.
IF !RC2! EQU 0 (
ECHO.
ECHO No unused variables
) ELSE (
ECHO.
ECHO !UnusedTitle!
ECHO !UnusedUnderline!
SET Variable. | FIND.EXE "=0"
)
)
)
:Done
SET /A RC = !RC1! + !RC2!
ENDLOCAL & EXIT /B %RC%
GOTO:EOF
:Syntax
ECHO.
ECHO CheckVarsVBS.bat, Version 2.00
ECHO Check subroutines and variables usage in VBScript and HTA files
ECHO.
ECHO Usage: CHECKVARSVBS vbsfile [ /S ^| /V ] [ /U ]
ECHO.
ECHO Where: "vbsfile" is the VBScript ot HTA file to be examined
ECHO /S tests Subroutines only
ECHO ^(default: test subroutines as well as variables^)
ECHO /U list Unused subroutines and variables only
ECHO ^(default: list all subroutines and variables^)
ECHO /V tests Variables only
ECHO ^(default: test subroutines as well as variables^)
ECHO.
ECHO Notes: Output format is "Subroutine.name=num" and "Variable.name=num" where
ECHO "name" is a subroutine name specified by a Sub or Function command,
ECHO or a variable name specified by a Dim command, and "num" is the
ECHO number of times the subroutine or variable is used in the script.
ECHO The return code ^("errorlevel"^) equals the number of unused subroutines
ECHO and/or variables, or will be -1 in case of ^(command line^) errors.
ECHO.
ECHO Written by Rob van der Woude
ECHO http://www.robvanderwoude.com
EXIT /B -1
page last modified: 2024-04-16; loaded in 0.0099 seconds