Getting Started
4 snippetsBasic VBScript syntax and first scripts
Hello World
WScript.Echo "Hello, World!" Message Box
MsgBox "Hello, World!"
MsgBox "Alert", vbExclamation, "Warning" Comments
' Single line comment
REM Another comment style Run Script
' Save as script.vbs
cscript script.vbs ' Console
wscript script.vbs ' GUI Variables & Data Types
5 snippetsVariable declaration and types
Variable Declaration
Dim name
name = "John"
Dim age, city
age = 30
city = "NYC" Constants
Const PI = 3.14159
Const MAX_SIZE = 100 Data Types
Dim num ' Integer or Long
Dim price ' Double
Dim text ' String
Dim flag ' Boolean
Dim today ' Date Type Conversion
CInt("42") ' To Integer
CDbl("3.14") ' To Double
CStr(100) ' To String
CBool(1) ' To Boolean
CDate("1/1/2024") ' To Date Check Variable Type
If IsNumeric(value) Then
WScript.Echo "Number"
End If
If IsDate(value) Then
WScript.Echo "Date"
End If Operators
4 snippetsArithmetic and comparison operators
Arithmetic
result = 10 + 5 ' Addition
result = 10 - 5 ' Subtraction
result = 10 * 5 ' Multiplication
result = 10 / 5 ' Division
result = 10 Mod 3 ' Modulus
result = 2 ^ 3 ' Exponentiation Comparison
If a = b Then ' Equal
If a <> b Then ' Not equal
If a > b Then ' Greater than
If a < b Then ' Less than
If a >= b Then ' Greater or equal
If a <= b Then ' Less or equal Logical Operators
If a And b Then ' AND
If a Or b Then ' OR
If Not a Then ' NOT
If a Xor b Then ' XOR String Concatenation
fullname = firstname & " " & lastname
message = "Hello, " & name & "!" Tired of looking up syntax?
DocuWriter.ai generates documentation and explains code using AI.
Control Flow
5 snippetsConditional statements and branching
If Statement
If age >= 18 Then
WScript.Echo "Adult"
End If If-Else
If score >= 60 Then
WScript.Echo "Pass"
Else
WScript.Echo "Fail"
End If If-ElseIf-Else
If grade >= 90 Then
result = "A"
ElseIf grade >= 80 Then
result = "B"
ElseIf grade >= 70 Then
result = "C"
Else
result = "F"
End If Select Case
Select Case day
Case "Monday"
WScript.Echo "Start of week"
Case "Friday"
WScript.Echo "End of week"
Case Else
WScript.Echo "Midweek"
End Select Inline If (IIf)
result = IIf(age >= 18, "Adult", "Minor")
status = IIf(score >= 60, "Pass", "Fail") Loops
7 snippetsIteration constructs
For Loop
For i = 1 To 10
WScript.Echo i
Next For Loop with Step
For i = 0 To 100 Step 10
WScript.Echo i
Next
For i = 10 To 1 Step -1
WScript.Echo i ' Countdown
Next For Each Loop
Dim fruits
fruits = Array("apple", "banana", "cherry")
For Each fruit In fruits
WScript.Echo fruit
Next While Loop
Dim count
count = 0
While count < 5
WScript.Echo count
count = count + 1
Wend Do While Loop
Dim i
i = 0
Do While i < 5
WScript.Echo i
i = i + 1
Loop Do Until Loop
Dim i
i = 0
Do Until i >= 5
WScript.Echo i
i = i + 1
Loop Exit Loop
For i = 1 To 100
If i = 50 Then
Exit For
End If
WScript.Echo i
Next Functions & Procedures
4 snippetsReusable code blocks
Sub Procedure
Sub Greet(name)
WScript.Echo "Hello, " & name
End Sub
Greet "World" Function with Return
Function Add(a, b)
Add = a + b
End Function
result = Add(5, 3)
WScript.Echo result Function with Multiple Returns
Function Calculate(x, y)
If y = 0 Then
Calculate = -1
Else
Calculate = x / y
End If
End Function ByRef vs ByVal
Sub ModifyValue(ByRef x, ByVal y)
x = x * 2 ' Modifies original
y = y * 2 ' Doesn't affect original
End Sub Arrays
5 snippetsWorking with arrays
Array Declaration
Dim fruits(2)
fruits(0) = "apple"
fruits(1) = "banana"
fruits(2) = "cherry" Dynamic Array
Dim numbers()
ReDim numbers(5)
numbers(0) = 10
ReDim Preserve numbers(10) ' Keep data Array Function
Dim fruits
fruits = Array("apple", "banana", "cherry")
WScript.Echo fruits(0) ' apple Multi-Dimensional Array
Dim matrix(2, 2)
matrix(0, 0) = 1
matrix(0, 1) = 2
matrix(1, 0) = 3 Array Bounds
lower = LBound(fruits) ' 0
upper = UBound(fruits) ' 2
size = UBound(fruits) - LBound(fruits) + 1 File System Operations
6 snippetsReading and writing files
Create FileSystemObject
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject") Write to File
Dim fso, file
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.CreateTextFile("output.txt", True)
file.WriteLine "Hello, World!"
file.Close Read from File
Dim fso, file, content
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("input.txt", 1)
content = file.ReadAll
file.Close
WScript.Echo content Append to File
Dim fso, file
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("log.txt", 8, True)
file.WriteLine "Log entry"
file.Close Check File Exists
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists("test.txt") Then
WScript.Echo "File exists"
End If Copy/Delete Files
fso.CopyFile "source.txt", "dest.txt"
fso.DeleteFile "temp.txt"
fso.MoveFile "old.txt", "new.txt"