You can conditionally execute commands using if...then...else statements. For example:
If 1 = 2 Then
MsgBox "It's true"
End If
More generally, you can evaluate multiple conditions in one go:
If [condition1] Then
[commands]
Elseif [condition2] Then
[more commands]
Else
[if all else fails, some default commands]
End If
A condition is a statement that logically evaluates as true or false. Here are some examples:
a = b |
True only if a and b have the same value |
a > b |
True if a is greater than b |
a <= b |
True if a is less than or equal to b |
a <> b |
True if a and b do not have the same value |
(a = b) And (c < d) |
True if a and b are equal and at the same time, c is less than d |
((a = b) And (a < c)) Or (a < b) |
True if some complex relationship holds |
Note in the last two examples that brackets must be used to surround individual conditions.
Here's another example that uses a function built-in to VBScript, Len(), that gets the number of characters in a text string.
str = InputBox "Please enter your name."
If Len(str) = 0 Then
MsgBox "You didn't enter anything!"
Else
MsgBox "The name you entered is: " & str
End If