Anyone who has used Linux will be aware of the shells (command line interpreters such as Bash, C and Korn) and that's what some languages such as VBScript lack - it's not possible, for example, to open up a DOS window and simply type in a VBScript command.
However, what the programmer can do is to create a VBScript program that will act as wrapper and produce a very simple shell - this will allow the user enter VBScript commands and it will then interpret them.
The VBScript shell will need to:
- accept user inputs from the command line
- process the input using VBScript
- wait for the next user input
Of course the first thing that the script must do is to ensure that the command-based script host (cscript.exe) is running correctly.
Setting up the Command-Based Script Host
VBScript is always run via cscript.exe, and so the first thing that the script must do is to start this running; and to do that the script uses the Windows script host (wscript.exe):
With the script hosts in place the script needs to start taking inputs from the user.
Setting up the VBScript Input and Output
The input for the program will be the DOS prompt and the output will be DOS screen:
Next the user inputs must be handled correctly.
Accepting User Inputs
The process for dealing with the user inputs is quite simple:
- display a prompt
- read the user input line by line
- if the user has asked to exit then do so, otherwise process the VBScript command
- go back to displaying the prompt
And the code to do this is:
Here the code loops, processing the input, until the word "exit" is entered. In this example another subroutine (called run_vbscript) is required to carry out the VBScript command.
Processing the VBScript Commands
The subroutine for processing the VBScript commands:
- accepts the command as text
- changes any instances of the word "write" to "output.writeline" (allowing the user to write an output to the screen)
The subroutine uses the executeglobal method to run the VBScript command:
With this small amount of code the programmer is ready to start using the VBScript command line interpreter.
Using the VBScript Command Line Interpreter
The VBScript code should be saved into a file with a .vbs suffix (for example shell.vbs) and then the easiest way to start the shell is to double-click on it in Windows Explorer - this will run the script in a DOS window so that VBScript commands can be written directly at the prompt (remembering to use the write statement to produce an output on the screen). For example:
The programmer can now test out any VBScript functionality (again remembering to use write to produce an output to the screen).
Using Compound Statements
Compound statements (such as if...then...else and for...next) can be used, but not in the normal way - instead they must be typed in on the same line with a colon separating the statements; for example:
And so, the programmer can create a fully functional command line interpreter for VBScript with just a single vbs file.
Summary
In order to use VBScript as a command line interpreter the user must:
- call cscript and wscript correctly
- accept inputs typed into the DOS window
- use the VBScipt executeglobal method to process any commands
- use colons to build compound statements
This shell may not be the most advanced in the world, but it will allow the user to easily experiment with VBScript commands.