VBScript

Implementation of programming language Basic

VBScript (Visual Basic Scripting Edition) is a lightweight scripting language developed by Microsoft based on Visual Basic.

VBScript development started in 1996 as part of Microsoft Windows Script Technologies, originally aimed at web development. The new language was to replace an outdated one used by command.com. During the next couple of years the language developed fast and drew attention and later recognition from system administrators who needed a powerful automation tool better than that of batch files. Starting with Windows 98, VBScript was included in standard shipment of all Windows copies.

The language prospered and gained new capabilities till .NET framework appeared. At this stage VBScript was incorporated in ASP.NET and ceased to evolve as a standalone engine. No new versions will appear after VBScript 5.8, and its support is delegated to Microsoft’s Sustaining Engineering Team. However, the existing scripting engine will be included in the following Windows releases.

Usage

VBScript scripts are interpreted by a specialized environment which exists in several versions:

  • Windows Script Host allows to create standalone applications by creating a .vbs scripts and running them using cscript.exe (in command-line environment) or wscript.exe (with GUI).
  • With Internet Explorer VBScript usage is similar to that of JavaScript: creation of executable functions embedded in HTML web-pages to extend their capabilities. However, other web-browsers don’t support VBScript, so JavaScript is preferred for this purpose.
  • The language can also be used for server-side processing of web-pages, typically with Active Server Pages.
  • VBScript can be embedded in other types of scripting files: Windows Script Files (.wsf files which provide modularity and code reuse) and HTML Application (.hta files which combine HTML capabilities for user interface description with a scripting language for logic implementation).
  • Microsoft Script Control technology allows to embed VBScript environment into other applications.

Language Features

The syntax of the language is a simplified version of Visual Basic. In particular, typing system was simplified: all variables have Variant data type, though any value can be cast to the necessary type explicitly using conversion functions CStr, CInt etc. Most of the important capabilities are preserved: date/time manipulation, string processing, math calculations, exceptions handling, regular expressions etc. The language defines a lot of additional constants to improve code readability.

Examples:

Hello, World!:

Example for versions VBScript 5.7, VBScript 5.8

The program outputs the message into console, and thus should be executed using cscript.exe.

WScript.Echo("Hello, World!")

Factorial:

Example for versions VBScript 5.7, VBScript 5.8

This example shows iterative factorial calculation. Note that no overflow happens, though the type of factorial variable is inferred from its usage and never set explicitly.

f = 1
For i = 0 To 16
   WScript.Echo(i & "! = " & f)
   f = f * (i + 1)
Next

Fibonacci numbers:

Example for versions VBScript 5.7, VBScript 5.8

This program calculates Fibonacci numbers recursively. Note the absence of a lot of elements typical for other Basic dialects: variable declarations and function return type, explicit number-to-string conversion before concatenation etc.

Function Fibonacci(N)
  If N < 2 Then
    Fibonacci = N
  Else
    Fibonacci = Fibonacci(N - 1) + Fibonacci(N - 2)
  End If
End Function

For i = 1 To 16
  res = res & Fibonacci(i) & ", "
Next
WScript.Echo (res & "...")

CamelCase:

Example for versions VBScript 5.7, VBScript 5.8

Unlike many other Visual Basic versions, VBScript has no StrConv function. In the end character-by-character processing of the string turns out to be easier.

Text = LCase(WScript.StdIn.ReadLine)
CamelCase = ""
WasSpace = True
For i = 1 To Len(Text)
  Ch = Mid(Text, i, 1)
  If InStr("abcdefghijklmnopqrstuvwxyz", Ch) = 0 Then
    WasSpace = True
  Else
    If WasSpace Then
      CamelCase = CamelCase & UCase(Ch)
    Else
      CamelCase = CamelCase & Ch
    End If
    WasSpace = False
  End If
Next
WScript.Echo CamelCase

Quadratic equation:

Example for versions VBScript 5.7, VBScript 5.8
Function GetInt()
  Input = WScript.StdIn.ReadLine
  If not IsNumeric(Input) Then
    WScript.Echo "Coefficient is not a number."
    WScript.Quit
  End If
  GetInt = CInt(Input)
End Function
  

A = GetInt()
If A = 0 Then
  WScript.Echo "Not a quadratic equation."
  WScript.Quit
End If
B = GetInt()
C = GetInt()
D = B * B - 4 * A * C
p1 = -B / 2.0 / A
p2 = Sqr(Abs(D)) / 2.0 / A
If D = 0 Then
  WScript.Echo "x = " & p1
Else
  If D > 0 Then
    WScript.Echo "x1 = " & (p1 + p2)
    WScript.Echo "x2 = " & (p1 - p2)
  Else
    WScript.Echo "x1 = (" & p1 & ", " & p2 & ")"
    WScript.Echo "x2 = (" & p1 & ", " & -p2 & ")"
  End If
End If