Suite101

Windows Scripting: Creating a VBScript Shell

How to Create Command Line Interpreter with VBScript

© Mark Alexander Bain

Dec 6, 2008
A VBScript Program, Mark Alexander Bain
VBScript does not have a command line interpreter of its own; however a programmer can quickly write a wrapper script that will act as a simple VBScript shell.

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):

wfullname = wscript.fullname
exe = lcase(mid(wfullname, instrrev(wfullname, "\")+1))
if not exe = "cscript.exe" then
set wshell = createobject("wscript.shell")
wshell.run "cscript.exe """ & wscript.scriptfullname & """"
wscript.quit
end if

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:

set input = wscript.stdin
set output = wscript.stdout

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:

vbprompt = "$ "
output.write vbprompt
do until input.atendofstream
user_ip = input.readline
if lcase(user_ip) = "exit" then
exit do
else
run_vbscript user_ip
output.write vbprompt
end if
loop

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:

sub run_vbscript (vbcommand)
on error resume next
vbcommand = replace (vbcommand, "write", "output.writeline")
executeglobal vbcommand
end sub

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:

Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
$ x = 1
$ write x
1
$ x = x + 9
$ write x
10
$ y = array (1,2,3,4)
$ write ubound(y)
3

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:

$ for x=1 to 3:write x:next
1
2
3
$ if x<10 then:write "x is less than 10":end if
x is less than 10

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.


The copyright of the article Windows Scripting: Creating a VBScript Shell in Windows Programming is owned by Mark Alexander Bain. Permission to republish Windows Scripting: Creating a VBScript Shell in print or online must be granted by the author in writing.


A VBScript Program, Mark Alexander Bain
The VBScript Command Line Interpreter, Mark Alexander Bain
     


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo

Comments
Apr 22, 2009 8:36 AM
Guest :
awesome article dude! love it!

thank you
1 Comment: