Catalog / VBScript Cheat Sheet
VBScript Cheat Sheet
A quick reference guide to VBScript, covering essential syntax, objects, and functions for automating tasks in Windows environments.
VBScript Basics
Data Types
Boolean |
True or False |
Byte |
Integer between 0 and 255 |
Integer |
Integer between -32,768 and 32,767 |
Long |
Integer between -2,147,483,648 and 2,147,483,647 |
Single |
Single-precision floating-point number |
Double |
Double-precision floating-point number |
String |
Sequence of characters |
Date |
Represents dates and times |
Object |
An OLE Automation object |
Variables
Variables are declared using the Example:
|
To explicitly declare the type of a variable, use Example:
|
Constants are declared using the Example:
|
Operators
Arithmetic |
+, -, *, /, \ (integer division), Mod (modulus), ^ (exponentiation) |
Comparison |
=, <>, <, >, <=, >= |
Logical |
And, Or, Not, Xor |
String Concatenation |
& |
Assignment |
= |
Control Structures
Conditional Statements
If…Then…Else
|
Select Case
|
Looping Structures
For…Next
|
For Each…Next
|
Do While…Loop
|
Do…Loop While
|
While…Wend (Legacy, avoid using)
|
Error Handling
On Error Resume Next Example:
|
Err Object Properties: Methods: |
Functions and Procedures
Functions
Functions are blocks of code that perform a specific task and return a value. Syntax:
Example:
|
Functions are called by using their name and passing arguments, if any. Example:
|
Subroutines (Procedures)
Subroutines are blocks of code that perform a specific task but do not return a value. Syntax:
Example:
|
Subroutines are called using the Example:
|
Built-in Functions
|
Displays a message box. |
|
Displays a prompt for user input. |
|
Returns the length of a string. |
|
Converts a string to uppercase. |
|
Converts a string to lowercase. |
|
Removes leading and trailing spaces from a string. |
|
Returns a specified number of characters from the left side of a string. |
|
Returns a specified number of characters from the right side of a string. |
|
Returns a specified number of characters from a string. |
Working with Objects
Creating Objects
Objects can be created using the Syntax:
Example:
|
Late Binding vs Early Binding:
|
FileSystemObject (FSO)
|
Creates a new text file. |
|
Opens an existing text file. |
|
Checks if a folder exists. |
|
Checks if a file exists. |
|
Creates a new folder. |
|
Deletes a file. |
|
Deletes a folder. |
WScript Object
|
Displays a message. |
|
Terminates the script. |
|
Collection of command-line arguments. |
|
The full path of the current script. |