MSDN Documentation

Microsoft Developer Network

VBScript Basics

This document provides an introduction to the fundamental concepts of VBScript (Visual Basic Scripting Edition). VBScript is a scripting language developed by Microsoft that is embedded in many of their products, including Internet Explorer, Windows Script Host (WSH), and Active Server Pages (ASP).

What is VBScript?

VBScript is a lightweight, interpreted scripting language that allows you to automate tasks, manipulate data, and create dynamic content. It shares many similarities with Visual Basic but is designed for a scripting environment.

Getting Started

To run VBScript, you typically use the Windows Script Host (WSH). You can create VBScript files with a .vbs extension and execute them by double-clicking them or by using the cscript.exe or wscript.exe command-line utilities.

Core Concepts

Variables

Variables are used to store data. You declare variables using the Dim statement. VBScript is loosely typed, meaning you don't have to specify the data type explicitly.

Dim userName
userName = "Alice"

Dim userAge
userAge = 30

Dim isActive
isActive = True
            

Data Types

VBScript supports several basic data types:

Operators

VBScript supports various operators for performing operations:

Control Flow Statements

These statements allow you to control the execution path of your script.

If...Then...Else

Used for conditional execution.

If userAge >= 18 Then
    MsgBox "Adult"
Else
    MsgBox "Minor"
End If
            
Select Case

Used for multi-way branching.

Dim grade
grade = "B"

Select Case grade
    Case "A"
        MsgBox "Excellent!"
    Case "B"
        MsgBox "Good job!"
    Case Else
        MsgBox "Needs Improvement"
End Select
            
For Loop

Executes a block of code a specified number of times.

For i = 1 To 5
    MsgBox "Iteration: " & i
Next
            
Do While Loop

Executes a block of code as long as a condition is true.

Dim count
count = 0
Do While count < 3
    MsgBox "Count is: " & count
    count = count + 1
Loop
            

Procedures (Subroutines and Functions)

Procedures allow you to group code into reusable blocks.

Subroutines

Perform an action but do not return a value.

Sub GreetUser(name)
    MsgBox "Hello, " & name & "!"
End Sub

GreetUser("Bob")
            
Functions

Perform an action and return a value.

Function AddNumbers(num1, num2)
    AddNumbers = num1 + num2
End Function

Dim sum
sum = AddNumbers(10, 20)
MsgBox "The sum is: " & sum
            

Built-in Functions

VBScript provides a rich set of built-in functions for string manipulation, mathematical operations, date/time handling, and more.

For more advanced VBScript topics such as working with objects, error handling, and interaction with the Windows environment, please refer to the related documentation.

Conclusion

This overview covers the fundamental building blocks of VBScript. By understanding these concepts, you can begin to write your own scripts to automate tasks and enhance your Windows experience.