25% developed

VBScript Programming

From Wikibooks, open books for an open world
Jump to navigation Jump to search

VBScript, short for Visual Basic Scripting Edition, is a Microsoft-supplied scripting language similar to Visual Basic and Visual Basic for Applications. It can be used via Windows Script Host (WSH) as a Windows scripting language. Furthermore, it can be used as a client-side scripting language in web pages instead of JavaScript, but such pages will only work with Internet Explorer. Moreover, it can be used for server-side web scripting as part of Active Server Pages (ASP), and in other environments. This book focuses on the language use via WSH.

Scripts to be used with WSH are usually plain text files with .vbs extension. By default, Windows associates the extension with an interpreter of the language, and therefore, entering the script name on the Windows batch command line leads to script execution, albeit without waiting for script completion. Furthermore, scripts can be run from the desktop or the Windows file explorer, usually by double clicking.

VBScript can access objects available via Microsoft COM interface, including those of Microsoft Office. Thus, Microsoft Office can be scripted not only from Visual Basic for Applications (VBA) embedded in office files such as spreadsheets but also from VBScript scripts, in a syntanctically similar yet somewhat different manner.

Two commands are available to let WSH execute a script: wscript and cscript. wscript is for interaction via simple popup windows whereas cscript is for command line interaction via text console. By default, .vbs extension is associated with wscript.

VBScript is mostly case-insensitive as for keywords and identifiers. Thus, "End If" is equivalent to "end if", and "FSO.CreateTextFile( ..." is equivalent to "fso.createtextfile( ...".

Data types[edit | edit source]

Unlike in VBA, variables cannot be dimensioned to be restricted to values of a particular data type, such as String. Rather, they are all of data type Variant.

However, there is still an underlying data type of each variable, depending on what you assign to the variable. You can find out about the current underlying data type of a variable content using TypeName function (returns a string) and VarType function (returns an integer).

Links:

Literals[edit | edit source]

Examples of literals of various data types:

a = 3                  'Integer: signed 16-bit integer
b = 2147483647         'Long: signed 32-bit integer
c = 3.5                'Double floating-point number
d = 1.7E-10            'Double floating-point number
e = &H7FFFFFFF         'Long; hexadecimal literal
f = &HFFFFFFFF         'Integer, whyever; value -1; arguably a bug or a trap
g = CLng("&HFFFFFFFF") 'Long -1
h = &H8000&            'Long via the final &; 32768
h = &HFFFFFFFF&        'Integer; the final & did not help make this Long; value -1; arguably a bug or a trap
i = &O666              'Integer; octal literal
i = &O177777           'Integer; -1
i = &O177777&          'Long via final &; 65535
i = &O37777777777      'Integer; -1; a bug or a trap
i = &O37777777777&     'Integer; -1; a bug or a trap
i = &666               'Integer; octal literal without O after &
j = "Hello"            'String
k = True               'Boolean; -1 when converted to an integer
l = #1-Jan-2020#       'Date
m = #Jan-2-2020#       'Date, month first
n = #1-2-2020#         'Date, month first: Jan 2, 2020
n = #2020-02-01#       'Date, ISO format
o = #Jan-2-2020 2:20:13 PM# 'Date, with time
p = #Jan-2-2020 14:20:13#   'Date, with time

Examples that are not of literals but serve the same purpose:

a = CCur(922337203685477) + CCur(5807) / 10000    'Currency: 64-bit fixed-point real number with 4 decimal places
b = CCur(-922337203685477) - CCur(5808) / 10000   'Currency
c = CByte(255)     'Byte; unsigned 8-bit integer
'c = CByte(-1)     'Error
d = CLng(5)        'Long
e = CSng(1.5)      'Single precision floating point
f = Array(1, 2.5, "s", Array(3, 4)) 'Array

Dictionary[edit | edit source]

VBScript supports dictionaries AKA associative arrays via Scripting.Dictionary object. The key can be of any type except for arrays; thus, strings, integers, floating points, dates, etc. are supported.

Examples:

Set Dict = CreateObject("Scripting.Dictionary")
Dict.Add "Key", "Value"
Value = Dict.Item("Key")
If Dict.Exists("Key") Then
  WScript.Echo "Key exists"
End If
Dict.Remove "Key"

Dict.Add "DE", "Germany"
Dict.Add "USA", "United States"
Dict.Item("USA") = "The United States" 'Member rewrite
Keys = Dict.Keys 'An array, not a collection
For Idx = 0 To Dict.Count - 1 'For each key
  WScript.Echo "Key " & Keys(Idx)
  WScript.Echo "Value " & Dict.Item(Keys(Idx))
Next

Links:

Input and output[edit | edit source]

For input and output for user interaction, VBScript in Windows Scripting Host offers a simple popup message box and a simple popup input box, which work for both script invocations via wscript (the default) and cscript. Text console input and output is not supported in scripts run via wscript; running via cscript is required.

Examples of input via WScript object, primarily via text console:

WScript.Echo "Hello"  'Works both via wscript (makes a popup window)
                      'and cscript (outputs to a console).
WScript.Echo "Multiple items and types:", 4, 3.5, #1-Jan-2020#
' ^ The items separated by commas are output space-separated.
If InStr(1, WScript.FullName, "cscript", vbTextCompare) Then
  ' ^ Tests whether we are running via cscript, not wscript.
  WScript.StdOut.Write "Enter an integer:" 'Only works via cscript, not wscript.
  ' ^ Outputs to text console. Writes no newline.
  str = WScript.StdIn.ReadLine() 'User input from console
  WScript.StdOut.WriteLine "Entered " & str 'Writes a newline.
  WScript.StdErr.WriteLine "Test error message" 'Standard error stream is available.
Else
  WScript.Echo "Not running via cscript. Some examples are skipped."
End If

Examples of input and output via popup windows, both for wscript and cscript:

MsgBox "Hello"
MsgBox "Hello 2", 0, "Window Title"
str = InputBox("Enter an integer")
int1 = CInt(str) 'Converts string to int
MsgBox int1 + 1

For reading from a text file and writing to a text file, see #File operations.

Example of getting command-line arguments:

i1 = WScript.Arguments.Item(0)
i2 = WScript.Arguments.Item(1)
WScript.Echo CInt(i1) + CInt(i2)

Example of getting an environment variable:

Set Shell = CreateObject("WScript.Shell")
HomeDrive = Shell.ExpandEnvironmentStrings("%HOMEDRIVE%")
HomePath = Shell.ExpandEnvironmentStrings("%HOMEPATH%")
WScript.Echo HomeDrive & HomePath

Links:

File operations[edit | edit source]

Unlike in VBA, file operations are not part of the language built-in core but rather are accessible from FileSystemObject.

Reading from a text file:

FileName = "MyFile.txt"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set File = FSO.OpenTextFile(FileName)
While Not File.AtEndOfStream
  Line = file.Readline() 'Line has no newline.
  'Process Line
  WScript.StdOut.WriteLine Line 'Works only via cscript
Wend
File.Close

Writing to a text file:

FileName = "MyFile.txt"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set File = FSO.CreateTextFile(FileName, True)
File.Write "Hello, there." & vbCrLf
File.Close

MsgBox "File " & FileName & " written."

Getting current directory:

Set FSO = CreateObject("Scripting.FileSystemObject")
CurrentDirectory = FSO.GetAbsolutePathName(".") 
MsgBox CurrentDirectory

Reading bytes from a binary file using ADODB.Stream:

Bytes = ReadBytes("MyFile.bin")
For Each Byte_ in Bytes
  WScript.Echo Byte_
Next

Function ReadBytes(FileName)
  'Read bytes into variant array of bytes
  Dim OutBytes()
  'SetLocale "ja" 'Uncomment to test locale independence
  Set Stream = CreateObject("ADODB.Stream")
  Stream.Open
  Stream.Type = 1 'Binary mode
  Stream.LoadFromFile FileName
  ReDim OutBytes(Stream.Size - 1)
  'Stream.Position = 10 'Skip first 10 bytes if you wish
  J = 0
  While Not Stream.EOS
    Bytes = Stream.Read(1000)  'Read a block of up to 1000 bytes
    'Bytes = Stream.Read()     'Or read them all: we would not need the while loop
    'VarType(Bytes) = 8209 'Byte array, not variant array
    For I = 0 To UBound(Bytes)
      Byte_ = AscB(MidB(Bytes, I + 1, 1)) 'vartype(Byte_) = 17, which is Byte
      OutBytes(J) = Byte_
      J = J + 1
    Next
  Wend
  Stream.Close
  ReadBytes = OutBytes
End Function

Writing bytes to a binary file using ADODB.Stream:

Set Stream = WScript.CreateObject("ADODB.Stream")
Stream.Type = 2 'Text mode
Stream.Charset = "iso-8859-1"
Stream.Open
'Write test bytes from 0 to 255
For I = 0 to 255
  Stream.WriteText ChrW(I)
Next
Stream.SaveToFile "MyFile.bin", 2 '2=Overwrite file
Stream.Close

Above, the trick is to use iso-8859-1 together with ChrW. A caveat is that iso-8859-1 does not define control codes so their mapping from Unicode to iso-8859-1 is formally undefined, but an implementation-defined mapping was chosen in ADODB.Stream and so it works anyway. The above cannot be reliably achieved using FileSystemObject by writing Chr(num) to a text file since that is only going to work in some locales but not others.

Links:

Byte array[edit | edit source]

Byte arrays are to be contrasted with variant arrays of bytes. In VBScript, all declared arrays are variant arrays and it has no direct and simple method of creating a byte array. Still, a byte array can be created, even if in a more wordy manner and in dependence on ADODB.Stream.

Populating a byte array and then read-iterating it:

'Populate byte array
Set Stream = CreateObject("ADODB.Stream")
Stream.Type = 2 'Text mode
Stream.Charset = "iso-8859-1"
Stream.Open
'Write test bytes from 0 to 255
For I = 0 to 255
  Stream.WriteText ChrW(I)
Next
Stream.Position = 0
Set Stream2 = CreateObject("ADODB.Stream")
Stream2.Type = 1 'Binary mode
Stream2.Open
Stream.CopyTo Stream2
Stream2.Position = 0
ByteArray = Stream2.Read() 'VarType(ByteArray) = 8209 'Byte array, not variant array
Stream2.Close
Stream.Close

'Read-iterate byte array
For i = 0 To UBound(ByteArray)
  Byte_ = AscB(MidB(ByteArray, i+1, 1))
  WScript.Echo Byte_
Next

Above, populating a byte array uses writing Unicode code points in the range of 0-255 in iso-8859-1 encoding into a text stream, copying the text stream into a binary stream, and then reading the bytes from the binary stream. Read-iterating the byte array is brief and uses the trick of combined MidB and AscB rather than array index access.

Operators and math[edit | edit source]

Arithmetic operators include +, -, *, / (true division), \ (integer division), ^ (exponentiation) and mod (modulo). Integer division truncates toward zero, like in the C language: -3 \ 2 = -1. Integer division converts the operands to integral types (rounding them) before performing the division: 3.6 \ 2 = 2 (Long). Modulo is a remainder after the truncate-toward-zero integer division, and therefore, -3 mod 2 = -1 like C's -3 % 2 rather than like Python's -3 % 2 == 1.

Comparison operators include =, <>, <, >, <=, >= and Is.

Bitwise operators include And, Or, Xor, Not, Imp and Eqv. These operate on integer types (Byte, Integer, Long) as bitwise operators. When operating on negative integers, they act in keeping with the two's complement internal representation of signed integers. There are no bitwise shift and bitwise rotation operators, but they can be emulated.

Boolean logical operators are the same as bitwise operators. When operating on booleans including boolean expressions, the bitwise operators double as boolean logical operators with no short-circuit evaluation by True being -1. Thus, True and 14 yields 14 since -1 as Integer (16-bit) is 0xFFFF.

Math functions include Abs, Atn, Cos, Exp, Fix, Int, Log, Randomize, Rnd, Round, Sgn, Sin, Sqr and Tan.

String concatenation operators are & (recommended) and + (not recommended).

In another wikibook, see Active Server Pages/Appendix A: Language Reference.

Links:

Excel[edit | edit source]

Excel can be scripted using VBScript by accessing Excel.Application object. Unlike VBA, VBScript does not support named argument passing to methods, only positional argument passing. Furthermore, functions that appear global from VBA in Excel need to be called as methods of the Excel object in VBScript. Moreover, Excel's specific constants such as xlAnd are not available; you need to define the constant yourself or use a number instead.

An example:

Set FSO = CreateObject("Scripting.FileSystemObject")
CurrentDirectory = FSO.GetAbsolutePathName(".") 

Set Excel = CreateObject("Excel.Application")
Set Workbook = Excel.Workbooks.Open(CurrentDirectory & "\" & "MyFile.xlsx")
Workbook.Sheets(1).Cells(1, 1).Value = "Hey"
Workbook.Save
Workbook.Close

Links:

Constants[edit | edit source]

There are multiple built-in constants, starting with vb. For instance, vbOKCancel is used in conjunction with MsgBox.

Applications-specific constants such as Excel's xlAnd are not available.

Links:

Clipboard[edit | edit source]

VBScript does not support VBA's MSForms.DataObject to access clipboard.

There are workarounds:

  • For writing to the clipboard, you can run clip.exe available in Windows 7.
  • For reading from the clipboard, you can access Internet Explorer via COM and let it read from the clipboard.

Links:

External processes[edit | edit source]

You can run external processes using Run method of WScript.Shell:

Set Shell = WScript.CreateObject ("WScript.Shell")
Shell.Run "tasklist /v", 0, True

You can also do so using Exec method of WScript.Shell:

Set MyShell = CreateObject("WScript.Shell")
Set ExecObject = MyShell.Exec("tasklist /v")
' AllText = ExecObject.StdOut.ReadAll
Do While Not ExecObject.StdOut.AtEndOfStream
  Line = ExecObject.StdOut.ReadLine()
  If InStr(Line, "AcroRd32.exe") > 0 Then
    'Do something
  End If
Loop

Keywords: external commands, running programs.

In another Wikibook: Excel VBA#Command Output.

Links:

Regular expressions[edit | edit source]

You can use regular expressions using RexExp object:

Set RegExp = New RegExp
RegExp.Pattern = "[0-9][0-9]*"
If RegExp.Test("354647") Then
  MsgBox "Test passed."
End If

Alternatively, you could create the regex object via Set RegExp = CreateObject("VBScript.RegExp"), but, in VBScript, that is unnecessary.

Links:

Unicode[edit | edit source]

There is the following support for 16-bit Unicode in VBScript with WSH on platforms that support Unicode:

  • Script source code can be stored in UTF-16 LE (litte endian), and then, string literals can be in Unicode. Variable names and procedure names cannot be in Unicode. UTF-16 BE (big endian) and UTF-8 are not supported. A script in UTF-8 with BOM will not run. A script in UTF-8 without BOM may run but string literals with characters beyond 127 will be garbled when output via MsgBox and other interfaces.
  • UCase and LCase work with Unicode, so does MsgBox.
  • Unicode characters can be entered via ChrW function by passing it a code point number. ChrW(127) = Chr(127) while ChrW(128) <> Chr(128).
  • Unicode code point number of a character can be obtained via AscW.
  • FileSystemObject methods OpenTextFile and CreateTextFile support reading and writing UTF-16 LE Unicode when instructed to do so via optional parameters.
  • ADODB.Stream object supports writing Unicode to a file and reading from a file in UTF-8 by setting Stream.Charset to "utf-8", and in other encodings such as "ascii" or "iso-8859-1". You can obtain a list of encodings from cmd.exe command line via "reg query HKEY_CLASSES_ROOT\MIME\Database\Charset" or from PowerShell via "dir Registry::HKEY_CLASSES_ROOT\MIME\Database\Charset".

Links:

Reserved keywords[edit | edit source]

VBScript has the excepted reserved keywords such as Dim or While; these cannot be used as variable names. However, it has also some reserved keywords that come from VBA and are not used by VBScript syntax, such as Byte and Long.

Versions[edit | edit source]

VBScript versions include 5.1 (Win 2000), 5.6 (XP), 5.7 (Vista) and 5.8 (Win 7, Win 10).

The table of added features per version in the linked Microsoft documentation shows no added features after year 2000.

Script host version is to be distinguished from the script engine version.

To find out about the engine version and the Windows Script Host version:

VBScriptVersion = ScriptEngineMajorVersion & "." & ScriptEngineMinorVersion
WSHVersion = WScript.Version
WSHBuildVersion = WScript.BuildVersion

Running cscript without //nologo switch outputs script host version.

Links:

Comparison to VBA[edit | edit source]

Features missing from VBScript while present in VBA:

  • Named argument passing to functions and methods
  • Application-specific named constants such as Excel's xlAnd are not available; you have to set them on your own or pass numbers instead
  • Built-in file I/O; VBScript can use FileSystemObject
  • Creating custom collections via new Collection
  • Dimensioning variables with particular data types
  • Etc.

Links:

Limitations[edit | edit source]

Limitations:

  • Limitations described in #Comparison to VBA apply. In particular, there is no way to create custom Collections and thus no support for trivially expandable lists known from many programming languages; workarounds include redimensioning dynamic arrays as required or storing indices are numerical keys in a dictionary.
  • In general, compared e.g. to .NET-based PowerShell or to Python, very few facilities available as libraries; by contrast, the two mentioned scripting technologies sport very many libraries to support a variety of tasks.
  • No interactive shell to execute VBS commands one at a time, unlike e.g. .NET-based PowerShell or Python.
  • No reading from and writing to a console for scripts run via wscript, only for scripts run via cscript.
  • Limited support for array operations, e.g. no sorting functions. Users resort to writing their own.
  • No direct way of including other .vbs scripts as libraries; an indirect way is via WSF XML files.
  • No bitwise shift left and shift right operators, but can be easily implemented.
  • No general GUI programming from .vbs scripts; an alternative is to embed VBS in a HTML as HTML Application (HTA). Simple message boxes and input boxes are supported directly without HTA.
  • No arbitrary precision integer arithmetic.
  • No set type (mathematical set).
  • And more.

Popularity and adoption[edit | edit source]

VBScript in Windows Scripting Host used to be popular with Windows administrators. As of 2022, its use has since long dwindled and has been vastly surpassed by that of PowerShell. VBScript engine saw no major update since 2001 and Microsoft has positioned PowerShell as the preferred scripting technology for Windows administration. VBScript in Classic ASP has since long been surpassed by VB.NET and C# in ASP.NET. Unlike VBScript, VBA continues to see widespread use.

Links:

COM components[edit | edit source]

COM components often used with VBScript in WSH:

  • WScript.Shell
  • WScript.Network
  • Scripting.FileSystemObject
  • Scripting.Dictionary
  • Shell.Application
  • Excel.Application
  • Word.Application
  • Outlook.Application
  • InternetExplorer.Application

Links:

Related Wikibooks[edit | edit source]

External links[edit | edit source]