VB 教程
VBScript 是什么、能跑在哪、今天还有哪些用途。
Key PointsVBScript 是微软的轻量脚本语言,跑在 WSH、经典 ASP 和(历史上的)IE 里,语法是 Visual Basic 的简化版。
VBScript 是什么
VBScript is a subset of Visual Basic with types, windows and controls stripped out, keeping only the core language. It produces no executable on its own — it needs a host: Windows Script Host, classic ASP on IIS, or historically Internet Explorer.
三种宿主
| Host | Entry | Use |
|---|---|---|
| WSH (cscript) | cscript //nologo x.vbs | 运维脚本、批量处理 |
| WSH (wscript) | 双击 .vbs | 弹窗提示、静默运行 |
| ASP | <% %> 里 | 老式 Web 页面 |
| IE | <script language="VBScript"> | 已废弃,仅 IE 支持 |
第一个脚本
VBSCRIPT
' hello.vbs
Dim name
name = "World"
WScript.Echo "Hello, " & name
WScript.Echo "当前时间:" & Now()Bash
cscript //nologo hello.vbsOutput
Hello, World
当前时间:2026/11/3 14:25:09WScript.Echo prints to the console under cscript and shows a message box under wscript. The same script behaves differently depending on how it is launched — the most common surprise when debugging WSH scripts.
今天还值得学吗
| Situation | Verdict |
|---|---|
| Maintaining old ASP sites | 要 —— 服务端逻辑全是 VBScript |
| Windows admin scripts | 可以,但 PowerShell 更强 |
| New automation | 用 PowerShell 或 Python |
| Client-side scripting | 不要 —— 只有 IE 支持 |
VBScript still works on Windows 11 but Microsoft lists it as a deprecated feature that may be removed. No new project should depend on it; it matters only for maintaining existing systems.
学习路线
- 语法 —— 变量、运算符、条件、循环
- 过程 ——
Sub与Function、参数传递 - 内置函数 —— 字符串、日期、类型转换
- 宿主对象 ——
WScript、FileSystemObject - 实战 —— 批量改名、日志分析、定时任务
VBScript 是什么
VBScript 是 Visual Basic 的一个子集,去掉了类型声明、窗口、控件,只保留语言核心。它本身不生成可执行文件,需要宿主来运行:Windows 脚本宿主(WSH)、经典 ASP 的 IIS、或者历史上的 IE。
三种宿主
| 宿主 | 入口 | 用途 |
|---|---|---|
| 命令行 | cscript //nologo x.vbs | 运维脚本、批量处理 |
| 窗口 | 双击 .vbs | 弹窗提示、静默运行 |
| IIS | <% %> 里 | 老式 Web 页面 |
| 浏览器 | <script language="VBScript"> | 已废弃,仅 IE 支持 |
第一个脚本
VBSCRIPT
' hello.vbs
Dim name
name = "World"
WScript.Echo "Hello, " & name
WScript.Echo "当前时间:" & Now()Bash
cscript //nologo hello.vbsOutput
Hello, World
当前时间:2026/11/3 14:25:09WScript.Echo 在 cscript 下输出到控制台,在 wscript 下弹一个消息框。同一个脚本两种运行方式,输出形式不同——这是 WSH 脚本调试时最常遇到的差异。
今天还值得学吗
| 场景 | 结论 |
|---|---|
| 维护老 ASP 站点 | 要 —— 服务端逻辑全是 VBScript |
| Windows 运维脚本 | 可以,但 PowerShell 更强 |
| 新的自动化需求 | 用 PowerShell 或 Python |
| 浏览器端 | 不要 —— 只有 IE 支持 |
VBScript 在 Windows 11 上默认仍可用,但微软已把它列为「弃用功能」,新系统可能逐步移除。任何新项目都不应依赖它,只有维护既有系统时才需要。
学习路线
- 语法 —— 变量、运算符、条件、循环
- 过程 ——
Sub与Function、参数传递 - 内置函数 —— 字符串、日期、类型转换
- 宿主对象 ——
WScript、FileSystemObject - 实战 —— 批量改名、日志分析、定时任务