VBS脚本常用经典代码收集
在网上查找资料的时候发现好多经典的vbs代码,收集起来也为了以后学习。
VBS脚本用途很多:
1. 计算
2. 处理文件和文件夹
3. 管理Windows
4. 处理Word, Excel, PowerPoint等Office文档
5. 嵌入网页,驱动dHTML
6. 编写HTTP通信
7. 调用系统功能(COM组件),比如说语音说话
8. 分析HTML, XML
9. 调用命令行并分析返回结果
10. 处理图片
11. 自动化按键
12. 调用Windows Media Player并管理
13. 调用Windows Live Messenger并管理
14. 服务端技术:Active Server Page (ASP)
15. 脚本病毒
16. 处理数据库
下面是我收集的VBS代码,大部分转自http://www.doczj.com/doc/f778ed2e7375a417866f8f54.html,以后还会更新。
VBS获取系统安装路径
先定义这个变量是获取系统安装路径的,然后我们用”&strWinDir&”调用这个变量。
set WshShell = WScript.CreateObject("WScript.Shell")
strWinDir = WshShell.ExpandEnvironmentStrings("%WinDir%")
VBS获取C:\Program Files路径
msgbox CreateObject("WScript.Shell").ExpandEnvironmentStrings("%ProgramFiles%")
VBS获取C:\Program Files\Common Files路径
msgbox CreateObject("WScript.Shell").ExpandEnvironmentStrings("%CommonProgramFiles%")
给桌面添加网址快捷方式
set gangzi = WScript.CreateObject("WScript.Shell")
strDesktop = gangzi.SpecialFolders("Desktop")
set oShellLink = gangzi.CreateShortcut(strDesktop & "\Internet Explorer.lnk")
oShellLink.TargetPath = "http://www.doczj.com/doc/f778ed2e7375a417866f8f54.html"
oShellLink.Description = "Internet Explorer"
oShellLink.IconLocation = "%ProgramFiles%\Internet Explorer\iexplore.exe, 0"
oShellLink.Save
给收藏夹添加网址
Const ADMINISTRATIVE_TOOLS = 6
Set objShell = CreateObject("Shell.Application")
Set objFolder = http://www.doczj.com/doc/f778ed2e7375a417866f8f54.htmlspace(ADMINISTRATIVE_TOOLS)
Set objFolderItem = objFolder.Self
Set objShell = WScript.CreateObject("WScript.Shell")
strDesktopFld = objFolderItem.Path
Set objURLShortcut = objShell.CreateShortcut(strDesktopFld & "\奋斗Blog.url")
objURLShortcut.TargetPath = "http://www.doczj.com/doc/f778ed2e7375a417866f8f54.html/"
objURLShortcut.Save
删除指定目录指定后缀文件
On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFile "C:\*.vbs", True
Set fso = Nothing
VBS改主页
Set oShell = CreateObject("WScript.Shell")
oShell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\Start Page","http://www.doczj.com/doc/f778ed2e7375a417866f8f54.html"
VBS加启动项
Set oShell=CreateObject("Wscript.Shell")
oShell.RegWrite "HKLM\Software\Microsoft\Windows\CurrentVersion\Run\cmd","cmd.exe"
VBS复制自己
set copy1=createobject("scripting.filesystemobject")
copy1.getfile(wscript.scriptfullname).copy("c:\huan.vbs")
复制自己到C盘的huan.vbs(复制本vbs目录下的game.exe文件到c盘的gangzi.exe)
set copy1=createobject("scripting.filesystemobject")
copy1.getfile("game.exe").copy("c:\gangzi.exe")
VBS获取系统临时目录
Dim fso
Set