VB 总结
语法要点回顾、常见陷阱、与 PowerShell 的取舍。
Key PointsVBScript 的语法很小,难的是记住它的几个「和别的语言不一样」的地方。
语法速览
VBSCRIPT
Option Explicit ' 强制声明
Dim x, arr(3) ' 变量与数组
Const MAX = 100 ' 常量
x = 10
arr(0) = "a"
If x > 5 Then ' 条件
WScript.Echo "大"
ElseIf x = 5 Then
WScript.Echo "等"
Else
WScript.Echo "小"
End If
Select Case x ' 多分支
Case 1 : WScript.Echo "一"
Case Else : WScript.Echo "其它"
End Select
For i = 1 To 3 ' 计数循环
Next
For Each v In arr ' 遍历
Next
Do While x > 0 ' 条件循环
x = x - 1
Loop
Sub DoIt(a) ' 过程
End Sub
Function Calc(a) ' 函数
Calc = a * 2
End Function和别的语言不一样的地方
| Point | VBScript | Most languages |
|---|---|---|
| Case sensitivity | 不区分 | 区分 |
| Statement end | 换行 | 分号 |
| Block end | End If / Next / Loop | } |
| Param passing | 默认 ByRef | 默认按值或引用 |
| Return | 给函数名赋值 | return |
| Boolean ops | 不短路 | 短路 |
| Break / continue | 只有 Exit | break / continue |
| String concat | & | + |
陷阱清单
- 变量拼错不报错 —— 不加
Option Explicit就新建变量 - **
And/Or不短路** ——If obj Is Nothing Or obj.X会崩 - **
"0"是真** —— 字符串非空即真 - **
Dim a(2)是 3 个** —— 括号里是最大下标 - **默认
ByRef** —— 函数里改了会影响外面 - 文件编码 —— 必须 ANSI 或 UTF-16,UTF-8 会乱码
和 PowerShell 的取舍
| Feature | VBScript | PowerShell |
|---|---|---|
| Availability | 所有 Windows 都有 | Win7+ 默认有 |
| Objects | 只有 COM | 完整 .NET |
| Pipeline | 没有 | 有,强 |
| Error handling | On Error Resume Next | try/catch |
| Debugging | Echo 打印 | 断点、ISE、VS Code |
| Future | 弃用中 | 微软主推 |
Write new scripts in PowerShell. Reading VBScript is still essential for maintenance: many enterprises still run .vbs logon scripts, scheduled tasks and deployment scripts.
迁移到 PowerShell
VBSCRIPT
' VBScript:列出目录下的 .log 文件
Dim fso, folder, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder("C:\logs")
For Each f In folder.Files
If LCase(Right(f.Name, 4)) = ".log" Then
WScript.Echo f.Name & " " & f.Size
End If
NextPOWERSHELL
# PowerShell:同样的事
Get-ChildItem C:\logs -Filter *.log |
ForEach-Object { "{0} {1}" -f $_.Name, $_.Length }The PowerShell version is shorter and emits objects, so Sort-Object, Where-Object and Export-Csv work directly. VBScript emits strings and you parse them yourself.
Migration is not line-by-line translation: a
For Each plus If in VBScript is often one Where-Object, and a Select Case is often a hashtable. Decide the outcome first, then write PowerShell — far cleaner than transliterating.继续学习
- PowerShell —— Windows 自动化的现在与未来
- COM 与 WMI —— 理解 VBScript 为什么能操作系统
- Python —— 跨平台的自动化脚本
- 批处理 —— 更老但仍在用的胶水
语法速览
VBSCRIPT
Option Explicit ' 强制声明
Dim x, arr(3) ' 变量与数组
Const MAX = 100 ' 常量
x = 10
arr(0) = "a"
If x > 5 Then ' 条件
WScript.Echo "大"
ElseIf x = 5 Then
WScript.Echo "等"
Else
WScript.Echo "小"
End If
Select Case x ' 多分支
Case 1 : WScript.Echo "一"
Case Else : WScript.Echo "其它"
End Select
For i = 1 To 3 ' 计数循环
Next
For Each v In arr ' 遍历
Next
Do While x > 0 ' 条件循环
x = x - 1
Loop
Sub DoIt(a) ' 过程
End Sub
Function Calc(a) ' 函数
Calc = a * 2
End Function和别的语言不一样的地方
| 要点 | VBScript | 多数语言 |
|---|---|---|
| 大小写 | 不区分 | 区分 |
| 语句结束 | 换行 | 分号 |
| 块结束 | End If / Next / Loop | } |
| 参数传递 | 默认 ByRef | 默认按值或引用 |
| 返回 | 给函数名赋值 | return |
| 逻辑运算 | 不短路 | 短路 |
| 跳出/跳过 | 只有 Exit | break / continue |
| 字符串拼接 | & | + |
陷阱清单
- 变量拼错不报错 —— 不加
Option Explicit就新建变量 - **
And/Or不短路** ——If obj Is Nothing Or obj.X会崩 - **
"0"是真** —— 字符串非空即真 - **
Dim a(2)是 3 个** —— 括号里是最大下标 - **默认
ByRef** —— 函数里改了会影响外面 - 文件编码 —— 必须 ANSI 或 UTF-16,UTF-8 会乱码
和 PowerShell 的取舍
| 对比项 | VBScript | PowerShell |
|---|---|---|
| 可用性 | 所有 Windows 都有 | Win7+ 默认有 |
| 对象 | 只有 COM | 完整 .NET |
| 管道 | 没有 | 有,强 |
| 错误处理 | On Error Resume Next | try/catch |
| 调试 | Echo 打印 | 断点、ISE、VS Code |
| 前景 | 弃用中 | 微软主推 |
新脚本用 PowerShell。但维护老脚本时,读懂 VBScript 是必备技能——很多企业的登录脚本、定时任务、部署脚本还是 .vbs。
迁移到 PowerShell
VBSCRIPT
' VBScript:列出目录下的 .log 文件
Dim fso, folder, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder("C:\logs")
For Each f In folder.Files
If LCase(Right(f.Name, 4)) = ".log" Then
WScript.Echo f.Name & " " & f.Size
End If
NextPOWERSHELL
# PowerShell:同样的事
Get-ChildItem C:\logs -Filter *.log |
ForEach-Object { "{0} {1}" -f $_.Name, $_.Length }PowerShell 版更短,而且输出是对象——可以直接 Sort-Object、Where-Object、Export-Csv。VBScript 只能输出字符串,后续处理全靠自己解析。
迁移不是逐行翻译。VBScript 的
For Each + If 组合,在 PowerShell 里往往是一句 Where-Object;Select Case 往往是一张哈希表。先想「要什么结果」,再写 PowerShell,比对照翻译干净得多。继续学习
- PowerShell —— Windows 自动化的现在与未来
- COM 与 WMI —— 理解 VBScript 为什么能操作系统
- Python —— 跨平台的自动化脚本
- 批处理 —— 更老但仍在用的胶水