VBScript 程序详解
VBScript程序通常指独立的.vbs脚本文件(通过 Windows Script Host 运行),也可以嵌入 HTML 或旧版 ASP 页面。下面重点介绍最常见的独立 .vbs 程序的结构、编写、运行和实用示例。
1. 基本程序结构
一个完整的 VBScript 程序通常包含以下部分:
Option Explicit ' 强制声明变量(强烈推荐放在第一行) ' 声明变量 Dim variable1, variable2 ' 主程序代码(从这里开始执行) variable1 = "Hello" MsgBox variable1 & " World!" ' 可以调用子程序或函数 Call MyProcedure() ' 子程序(Sub) Sub MyProcedure() MsgBox "这是一个子程序" End Sub ' 函数(Function,有返回值) Function Add(a, b) Add = a + b End Function2. 如何创建和运行 .vbs 程序
- 打开记事本(Notepad)
- 复制粘贴代码
- 保存文件时:
- 文件名:例如
hello.vbs - 保存类型:所有文件
- 编码:ANSI 或 Unicode(推荐 ANSI)
- 文件名:例如
- 双击
.vbs文件即可运行(会弹出消息框) - 或者在命令提示符下运行:
wscript hello.vbs→ 图形模式(显示 MsgBox)cscript hello.vbs→ 控制台模式(输出到命令行)
3. 实用程序示例
示例1:简单问候程序
Option Explicit Dim userName userName = InputBox("请输入你的名字:", "问候程序") If userName <> "" Then MsgBox "你好," & userName & "!" & vbCrLf & "今天是 " & Date(), vbInformation, "欢迎" Else MsgBox "你没有输入名字,再见!" End If示例2:批量重命名当前文件夹下的文件(添加前缀)
Option Explicit Dim fso, folder, file, newName Dim prefix prefix = InputBox("请输入要添加的前缀:", "批量重命名") If prefix = "" Then MsgBox "前缀为空,程序退出。" WScript.Quit End If Set fso = CreateObject("Scripting.FileSystemObject") Set folder = fso.GetFolder(".") For Each file In folder.Files newName = prefix & file.Name file.Name = newName Next MsgBox "所有文件已添加前缀 '" & prefix & "' 并重命名完成!"使用方法:把这个脚本保存为rename.vbs,放到需要重命名的文件夹中,双击运行。
示例3:定时关机程序
Option Explicit Dim minutes, seconds minutes = InputBox("请输入多少分钟后关机:", "定时关机", "60") If IsNumeric(minutes) Then seconds = CLng(minutes) * 60 CreateObject("WScript.Shell").Run "shutdown /s /t " & seconds, 0, False MsgBox "电脑将在 " & minutes & " 分钟后关机!" & vbCrLf & "取消关机请运行:shutdown /a" Else MsgBox "输入无效!" End If示例4:读取并显示文本文件内容
Option Explicit Dim fso, ts, content, filePath filePath = InputBox("请输入文本文件路径:", "读取文件", "C:\test.txt") Set fso = CreateObject("Scripting.FileSystemObject") If fso.FileExists(filePath) Then Set ts = fso.OpenTextFile(filePath, 1) ' 1 = ForReading content = ts.ReadAll ts.Close MsgBox content, vbInformation, "文件内容" Else MsgBox "文件不存在!" End If示例5:遍历文件夹并列出所有 .txt 文件
Option Explicit Dim fso, folder, file, list Dim folderPath folderPath = InputBox("请输入文件夹路径:", "列出TXT文件", "C:\") Set fso = CreateObject("Scripting.FileSystemObject") If fso.FolderExists(folderPath) Then Set folder = fso.GetFolder(folderPath) list = "找到的 .txt 文件:" & vbCrLf & vbCrLf For Each file In folder.Files If LCase(fso.GetExtensionName(file.Name)) = "txt" Then list = list & file.Name & " (" & file.Size & " 字节)" & vbCrLf End If Next MsgBox list, vbInformation, "TXT 文件列表" Else MsgBox "文件夹不存在!" End If示例6:备份指定文件到另一个文件夹
Option Explicit Dim fso, sourceFile, destFolder sourceFile = "C:\重要文档\报告.docx" destFolder = "D:\备份\" Set fso = CreateObject("Scripting.FileSystemObject") If fso.FileExists(sourceFile) Then If Not fso.FolderExists(destFolder) Then fso.CreateFolder(destFolder) End If fso.CopyFile sourceFile, destFolder & fso.GetFileName(sourceFile), True MsgBox "备份成功!" Else MsgBox "源文件不存在!" End If小结
- VBScript 程序最强大的地方在于快速自动化 Windows 操作:文件管理、注册表、进程控制、消息提醒等。
- 编写时永远加
Option Explicit - 多用
CreateObject("Scripting.FileSystemObject")和CreateObject("WScript.Shell") - 双击运行最方便,适合做小工具
如果你有具体需求,比如:
- 自动清理临时文件
- 监控文件夹新文件
- 操作 Excel/Word
- 发送邮件
- 获取系统信息(如CPU、内存)
告诉我,我可以为你写出完整可直接运行的 .vbs 程序!