Program to parse a VBScript program and determine how many times each variable and user defined constant is used in the program. This is used to determine if any variables are declared but no longer used. The program assumes the use of "Option Explicit". All variables should be declared in either Dim statements, ReDim statements, Const statements, or as procedure arguments.

I always use "Option Explicit" in my programs, but over time I may modify the code several times. I often write a new program by copying a similar program and modifying it for the new task. As a result, there is a good chance there will be variables declared but no longer used. While this is not detrimental to the operation of the program, it seems untidy. Manually finding these cases is very time consuming. This program is designed to make the task easy. It also demonstrates some ideas for parsing code.

The program parses the VBScript file to find all variables, user defined constants, and procedure arguments. Arrays are treated similar to other variables. All quoted strings are ignored. The program keeps track of the scope of each variable. The program reads the file a second time and counts how many times each variable and constant is referenced. The program outputs a line for each variable and constant in the form:

<scope>\<variable>: <number of references>

%%Global is used to indicate the global scope. All variables and constants in the main program have global scope. Variables and constants in procedures have local scope. The program uses the name of the procedure to identify each local scope. This allows you to use the same variable name in different scopes, and the program treats them as independent.

The program seems to recognize most VBScript features and handles them correctly. The program was recently revised to handle variables names in square brackets, which allows you to have variable names with spaces or reserved keywords.

If any variables are not declared, the program will ignore them and treat them as if they are VBScript keywords.

ParseVars.txt <<-- Click here to view or download the program