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 styleRun Script
' Save as script.vbs
cscript script.vbs ' Console
wscript script.vbs ' GUIVariables & 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 = 100Data Types
Dim num ' Integer or Long
Dim price ' Double
Dim text ' String
Dim flag ' Boolean
Dim today ' DateType Conversion
CInt("42") ' To Integer
CDbl("3.14") ' To Double
CStr(100) ' To String
CBool(1) ' To Boolean
CDate("1/1/2024") ' To DateCheck Variable Type
If IsNumeric(value) Then
WScript.Echo "Number"
End If
If IsDate(value) Then
WScript.Echo "Date"
End IfOperators
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 ' ExponentiationComparison
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 equalLogical Operators
If a And b Then ' AND
If a Or b Then ' OR
If Not a Then ' NOT
If a Xor b Then ' XORString 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 IfIf-Else
If score >= 60 Then
WScript.Echo "Pass"
Else
WScript.Echo "Fail"
End IfIf-ElseIf-Else
If grade >= 90 Then
result = "A"
ElseIf grade >= 80 Then
result = "B"
ElseIf grade >= 70 Then
result = "C"
Else
result = "F"
End IfSelect Case
Select Case day
Case "Monday"
WScript.Echo "Start of week"
Case "Friday"
WScript.Echo "End of week"
Case Else
WScript.Echo "Midweek"
End SelectInline 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
NextFor 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
NextFor Each Loop
Dim fruits
fruits = Array("apple", "banana", "cherry")
For Each fruit In fruits
WScript.Echo fruit
NextWhile Loop
Dim count
count = 0
While count < 5
WScript.Echo count
count = count + 1
WendDo While Loop
Dim i
i = 0
Do While i < 5
WScript.Echo i
i = i + 1
LoopDo Until Loop
Dim i
i = 0
Do Until i >= 5
WScript.Echo i
i = i + 1
LoopExit Loop
For i = 1 To 100
If i = 50 Then
Exit For
End If
WScript.Echo i
NextFunctions & 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 resultFunction with Multiple Returns
Function Calculate(x, y)
If y = 0 Then
Calculate = -1
Else
Calculate = x / y
End If
End FunctionByRef vs ByVal
Sub ModifyValue(ByRef x, ByVal y)
x = x * 2 ' Modifies original
y = y * 2 ' Doesn't affect original
End SubArrays
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 dataArray Function
Dim fruits
fruits = Array("apple", "banana", "cherry")
WScript.Echo fruits(0) ' appleMulti-Dimensional Array
Dim matrix(2, 2)
matrix(0, 0) = 1
matrix(0, 1) = 2
matrix(1, 0) = 3Array Bounds
lower = LBound(fruits) ' 0
upper = UBound(fruits) ' 2
size = UBound(fruits) - LBound(fruits) + 1File 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.CloseRead 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 contentAppend to File
Dim fso, file
Set fso = CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("log.txt", 8, True)
file.WriteLine "Log entry"
file.CloseCheck File Exists
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists("test.txt") Then
WScript.Echo "File exists"
End IfCopy/Delete Files
fso.CopyFile "source.txt", "dest.txt"
fso.DeleteFile "temp.txt"
fso.MoveFile "old.txt", "new.txt"