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:
- String: Text data (e.g., "Hello World").
- Number: Numeric data, including integers and floating-point numbers.
- Boolean:
TrueorFalsevalues. - Date: Date and time values.
- Object: References to objects.
- Null: Indicates that a variable contains no valid data.
- Empty: Indicates that a variable has not yet been initialized.
Operators
VBScript supports various operators for performing operations:
- Arithmetic Operators:
+,-,*,/,\(integer division),Mod(modulo). - Comparison Operators:
=,<>,<,>,<=,>=. - Logical Operators:
And,Or,Not,Xor,Eqv,Imp. - String Concatenation Operator:
&.
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.
MsgBox(): Displays a message and waits for user input.InputBox(): Prompts the user for input.Len(): Returns the length of a string.Left(),Right(),Mid(): Extracts substrings.UCase(),LCase(): Converts strings to uppercase or lowercase.InStr(): Finds the position of one string within another.Array(): Creates an array.Split(): Splits a string into an array.Join(): Joins array elements into a string.
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.