1. Computing & Technology

Discuss in my forum

VBScript - Running VBScript in WSH

Part 3 of a "From the Ground Up" Tutorial

By , About.com Guide

Updated November 14, 2010

VBScript is a version of the BASIC programming language designed for the Command Prompt. It's used by administrators and programmers to do things that don't need a graphical user interface to get the work done. This is Part 3 of the tutorial. If you would like to start at the beginning, Click Here For Part 1.

The other standard hosting environments, IE and IIS, aren't used for VBScript nearly as much as Windows itself. To run VBScript in Windows, you use a part of Windows called the Windows Scripting Host, usually known by its acronym: WSH.

You can use our friend Notepad again to write the code for a WSH based VBScript program. Save the file with the file extension .vbs. You will normally run the script using one of two programs:

  • wscript.exe
  • cscript.exe

The difference is that Wscript displays the output and receives input through dialog and input boxes. Cscript.exe is used with the Command Prompt (which used to be DOS years ago).

To illustrate VBScript in WSH, we can't use the Two Button example. The reason is that there is no convenient "button". For that, you need VB.NET and the ToolBox controls. Scripting is not a visual environment and, in general, you can't do things that require visual controls. So we'll do the next best thing and use one of the two primary ways that you can use a visual control:

  • InputBox(Prompt[, [Title], [Default], [Xpos], [Ypos], [HelpFile][, Context]]
  • MsgBox(Prompt[, [Buttons], [Title | Helpfile[, Context]]

Using Notepad again, the code below is entered and saved in a file. I called mine StatisticsExample.vbs.


Dim Ans
Ans = 0
Dim inputNumber
Dim Sum
Sum = 0.0
Dim Count
Count = 0
Do While Ans <> vbYes
   inputNumber = _
	InputBox( _
		"Enter a number", _
		"Statistics Example", _
		0)
   Sum = _
	Sum + inputNumber
   Count = Count +1
   Ans = MsgBox("Finished?", vbYesNo)
Loop
Average = Sum / Count
WScript.Echo "The Sum is: ", _
	Sum, vbCrLf, _
	"The Average is: ", _
	Average

(I used a lot of line continuations - space followed by underscore - because I'm very limited on space in this article and this keeps the lines shorter.)

To run this program, first start the Command Prompt environment. (It's usually in the Accessories group under All Programs.) Then you have to use a few of your old DOS skills. (Everyone started programming with DOS batch files, right? I did!) Change to the folder where you stored your VBScript program. You can do this by using the CD command (change directory) along with the Dir /AD command (display the directory names) to see what to enter next. The illustration below shows these commands in use. (Yes, it's slow! See what we had to do back in the stone age!)

--------
Click Here to display the illustration
--------

After finally arriving in the directory where your script is saved, you can enter the command ...


wscript StatisticsExample.vbs

... or ...


cscript StatisticsExample.vbs

The program is shown running in the illustration below:

--------
Click Here to display the illustration
--------

Note that variables are not declared as any particular type. In fact, VBScript is like the old VB6 and VBA. All the variables are of type Variant (and they really don't have to be declared at all) which means they can be anything. A variable actually takes on a type when a value is assigned. In other words, when the value of Ans is assigned ...


Dim Ans
Ans = 0

... the subtype of Ans becomes Integer. If the VBScript compiler doesn't have to change the subtypes as the program runs, it will run slightly faster. And besides, you can avoid program crashes caused by illegal type conversions if you keep the variable types consistent.

I've also used the WScript.Echo command. If this was run using wscript, this would be displayed in a dialog box instead.

©2012 About.com. All rights reserved.

A part of The New York Times Company.