常用VBS代码
1.VBS获取路径集合
1.1.VBS获取系统安装路径
 程序代码
set WshShell = WScript.CreateObject("WScript.Shell")
strWinDir = WshShell.ExpandEnvironmentStrings("%WinDir%")

上面的代码意思是先定义这个变量是获取系统安装路径的,然后我们用"&strWinDir&"调用这个变量。
1.2.C:\Program Files路径
 程序代码
msgbox CreateObject("WScript.Shell").ExpandEnvironmentStrings("%ProgramFiles%")


1.3.C:\Program Files\Common Files路径
 程序代码
msgbox CreateObject("WScript.Shell").ExpandEnvironmentStrings("%CommonProgramFiles%")

2.给桌面添加网址快捷方式
 程序代码
set gangzi = WScript.CreateObject("WScript.Shell")
strDesktop = gangzi.SpecialFolders("Desktop")
vbs小程序代码大全set oShellLink = gangzi.CreateShortcut(strDesktop & "\Internet Explorer.lnk")
oShellLink.TargetPath = "www.9934"
oShellLink.Description = "Internet Explorer"
oShellLink.IconLocation = "%ProgramFiles%\Internet , 0"
oShellLink.Save

3.给收藏夹添加网址
 程序代码
Const ADMINISTRATIVE_TOOLS = 6

Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(ADMINISTRATIVE_TOOLS)
Set objFolderItem = objFolder.Self   

Set objShell = WScript.CreateObject("WScript.Shell")
strDesktopFld = objFolderItem.Path
Set objURLShortcut = objShell.CreateShortcut(strDesktopFld & "\小游戏网站.url")
objURLShortcut.TargetPath = "/?ie"
objURLShortcut.Save

4.删除指定目录指定后缀文件
 程序代码
On Error Resume Next
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFile "C:\*.vbs", True
Set fso = Nothing

上面代码为删除C盘根目录下后缀为vbs的文件
5.VBS改主页
 程序代码
Set oShell = CreateObject("WScript.Shell")
oShell.RegWrite "HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\Start Page","www.654.la"

6.VBS加启动项
 程序代码
Set oShell=CreateObject("Wscript.Shell")
oShell.RegWrite "HKLM\Software\Microsoft\Windows\CurrentVersion\Run\cmd",""

7.VBS复制自己
 程序代码
set copy1=createobject("scripting.filesystemobject")
file(wscript.scriptfullname).copy("c:\huan.vbs")

复制自己到C盘的huan.vbs
 程序代码
set copy1=createobject("scripting.filesystemobject")
file("").copy("c:\")

复制本vbs目录下的文件到c盘的
8.VBS获取系统临时目录
 程序代码
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Dim tempfolder
Const TemporaryFolder = 2
Set tempfolder = fso.GetSpecialFolder(TemporaryFolder)
Wscript.Echo tempfolder

9.就算代码出错 依然继续执行
 程序代码
On Error Resume Next

10.VBS打开网址
 程序代码
Set objShell = CreateObject("Wscript.Shell")
objShell.Run("/")

11.VBS发送邮件
 程序代码
NameSpace = "schemas.microsoft/cdo/configuration/"
Set Email = CreateObject("CDO.Message")
Email.From = "发件@qq"
Email.To = "收件@qq"
Email.Subject = "Test sendmail.vbs"
Email.Textbody = "OK!"
Email.AddAttachment "C:\1.txt"
With Email.Configuration.Fields
.Item(NameSpace&"sendusing") = 2
.
Item(NameSpace&"smtpserver") = "smtp.邮件服务器"
.Item(NameSpace&"smtpserverport") = 25
.Item(NameSpace&"smtpauthenticate") = 1
.Item(NameSpace&"sendusername") = "发件人用户名"
.Item(NameSpace&"sendpassword") = "发件人密码"
.Update
End With
Email.Send

12.VBS结束进程
 程序代码
strComputer = "."
Set objWMIService = GetObject _
    ("winmgmts:\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = ''")
For Each objProcess in colProcessList
    objProcess.Terminate()
Next

13.VBS隐藏打开网址
13.1.部分浏览器无法隐藏打开,而是直接打开,适合主流用户使用
 程序代码
createObject("wscript.shell").run "iexplore /",0

13.2.兼容所有浏览器,使用IE的绝对路径+参数打开,无法用函数得到IE安装路径,只用函数得到了Program Files路径,应该比上面的方法好,但是两种方法都不是绝对的。(本方法由刚子原创)
 程序代码
Set objws=WScript.CreateObject("wscript.shell")
objws.Run """C:\Program Files\Internet ""www.baidu",vbhide

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。