|
|
|
Windows Scripting: Creating a VBScript ShellHow to Create Command Line Interpreter with VBScript
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:
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 HostVBScript 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 OutputThe 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 InputsThe process for dealing with the user inputs is quite simple:
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 CommandsThe subroutine for processing the VBScript commands:
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 InterpreterThe 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 StatementsCompound 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. SummaryIn order to use VBScript as a command line interpreter the user must:
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.
Comments
Apr 22, 2009 8:36 AM
Guest
:
1 Comment:
|
|
|
|
|
|
|
|