VBS Scripting: Basic Syntax Examples

VBS, short for Visual Basic Script, is a scripting language developed by Microsoft for managing and automating tasks in Windows systems. Here are some examples explaining the basic syntax of VBS.

  1. In VBS, comments can be added using single quotes (‘) or the Rem keyword. Comments are used to explain the code and will not be executed by the interpreter.
' 这是一个注释
Rem 这也是一个注释
  1. Variables:
    In VBS, you can use the Dim keyword to declare variables and assign them initial values.
Dim name
name = "John"
  1. Data types:
    VBS supports data types such as strings, integers, and floating point numbers. The TypeName function can be used to get the data type of a variable.
Dim age
age = 25
MsgBox TypeName(age) ' 输出 Integer
  1. Conditional statement:
    VBS uses the If…Then…Else structure to implement conditional judgments.
Dim score
score = 80

If score >= 60 Then
    MsgBox "及格"
Else
    MsgBox "不及格"
End If
  1. Loop statements:
    VBS supports loop structures such as Do While, Do Until, For…Next.
Dim i
For i = 1 To 5
    MsgBox i
Next
  1. Functions and subroutines: Functions and subroutines can be defined using the Function and Sub keywords.
Function add(a, b)
    add = a + b
End Function

MsgBox add(2, 3)

Here are some basic syntax examples of VBS, hoping it would be helpful for you. VBS is a very powerful scripting language that can be used for various automation tasks and system management operations.

bannerAds