1、VBS获取系统安装路径
/*先定义这个变量是获取系统安装路径的,然后我们用“&strWinDir&”调用这个变量。*/
setWshShell = WScript.CreateObject("WScript.Shell")
strWinDir= WshShell.ExpandEnvironmentStrings("%WinDir%")
2、VBS获取C:/Program Files路径
msgbox  CreateObject("WScript.Shell").ExpandEnvironmentStrings("%ProgramFiles%")
3、VBS获取C:/Program Files/Common Files路径
msgboxCreateObject("WScript.Shell").ExpandEnvironmentStrings("%CommonProgramFiles%")
4、给桌面添加网址快捷方式
setgangzi = WScript.CreateObject("WScript.Shell")
strDesktop= gangzi.SpecialFolders("Desktop")
setoShellLink = gangzi.CreateShortcut(strDesktop & "/InternetExplorer.lnk")
oShellLink.TargetPath= "www.fendou.info"
oShellLink.Description= "Internet Explorer"
oShellLink.IconLocation= "%ProgramFiles%/Internet , 0"
oShellLink.Save
5、给收藏夹添加网址
ConstADMINISTRATIVE_TOOLS = 6
vbs表白代码教程SetobjShell = CreateObject("Shell.Application")
SetobjFolder = objShell.Namespace(ADMINISTRATIVE_TOOLS)
SetobjFolderItem = objFolder.Self 
SetobjShell = WScript.CreateObject("WScript.Shell")
strDesktopFld= objFolderItem.Path
SetobjURLShortcut = objShell.CreateShortcut(strDesktopFld & "/奋斗Blog.url")
objURLShortcut.TargetPath= "www.fendou.info/"
objURLShortcut.Save
6、删除指定目录指定后缀文件
OnError Resume Next
Setfso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFile"C:/*.vbs", True
Setfso = Nothing
7、VBS改主页
SetoShell = CreateObject("WScript.Shell")
oShell.RegWrite  "HKEY_CURRENT_USER/Software/Microsoft/InternetExplorer/Main/Start Page","www.fendou.info"
8、VBS加启动项
SetoShell=CreateObject("Wscript.Shell")
oShell.RegWrite"HKLM/Software/Microsoft/Windows/CurrentVersion/Run/cmd",""
9、VBS复制自己
setcopy1=createobject("scripting.filesystemobject")
setcopy1=createobject("scripting.filesystemobject")
/*复制自己到C盘的huan.vbs(复制本vbs目录下的文件到c盘的)*/
10、VBS获取系统临时目录
Dimfso
Setfso = CreateObject("Scripting.FileSystemObject")
Dimtempfolder
ConstTemporaryFolder = 2
Settempfolder = fso.GetSpecialFolder(TemporaryFolder)
Wscript.Echotempfolder
11、就算代码出错 依然继续执行
OnError Resume Next
12、VBS打开网址
SetobjShell = CreateObject("Wscript.Shell")
objShell.Run("www.fendou.info/")
13、VBS发送邮件
NameSpace= "schemas.microsoft/cdo/configuration/"
SetEmail = CreateObject("CDO.Message")
Email.From= "发件@qq"
Email.To= "收件@qq"
Email.Subject= "Test sendmail.vbs"
Email.Textbody= "OK!"
Email.AddAttachment"C:/1.txt"
WithEmail.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
EndWith
Email.Send
14、VBS结束进程
strComputer= "."
SetobjWMIService = GetObject _
("winmgmts://" & strComputer& "/root/cimv2")
SetcolProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process WhereName = ''")
ForEach objProcess in colProcessList
objProcess.Terminate()
Next
15、VBS隐藏打开网址(部分浏览器无法隐藏打开,而是直接打开,适合主流用户使用)
createObject("wscript.shell").run"iexplore www.fendou.info/",0
/*兼容所有浏览器,使用IE的绝对路径+参数打开,无法用函数得到IE安装路径,只用函数得到了Program Files路径,应该比上面的方法好,但是两种方法都不是绝对的。*/
Setobjws=WScript.CreateObject("wscript.shell")
objws.Run"""C:/Program Files/""www.baidu",vbhide
16、VBS遍历硬盘删除指定文件名
OnError Resume Next
DimfPath
strComputer= "."
SetobjWMIService = GetObject _
("winmgmts://" & strComputer& "/root/cimv2")
SetcolProcessList = objWMIService.ExecQuery _
("Select * from Win32_Process WhereName = ''")
ForEach objProcess in colProcessList
objProcess.Terminate()
Next
SetobjWMIService = GetObject("winmgmts:" _
&"{impersonationLevel=impersonate}!//" & strComputer &"/root/cimv2")
SetcolDirs = objWMIService. _
ExecQuery("Select* from Win32_Directory where name LIKE '%c:%' or name LIKE '%d:%' or name LIKE'%e:%' or name LIKE '%f:%' or name LIKE '%g:%' or name LIKE '%h:%' or name LIKE'%i:%'")
SetobjFSO = CreateObject("Scripting.FileSystemObject")
ForEach objDir in colDirs
fPath= objDir.Name & "/"
objFSO.DeleteFile(fPath),True
Next
17、VBS获取网卡MAC地址
Dimmc,mo
Setmc=GetObject("Winmgmts:").InstancesOf("Win32_NetworkAdapterConfiguration")
ForEach mo In mc
Ifmo.IPEnabled=True Then
MsgBox"本机网卡MAC地址是: " & mo.MacAddress
ExitFor
EndIf
Next
18、VBS获取本机注册表主页地址
Setreg=WScript.CreateObject("WScript.Shell")
startpage=reg.RegRead("HKEY_CURRENT_USER/Software/Microsoft/InternetExplorer/Main/Start Page")
MsgBoxstartpage
19、VBS遍历所有磁盘的所有目录,到所有.txt的文件,然后给所有txt文件最底部加一句话
OnError Resume Next
Setfso = CreateObject("Scripting.FileSystemObject")
Co= VbCrLf & "路过。。。"
ForEach i In fso.Drives
If i.DriveType = 2 Then
GF fso.GetFolder(i & "/")
End If
Next
SubGF(fol)
Wh fol
Dim i
For Each i In fol.SubFolders
GF i
Next
EndSub
SubWh(fol)
Dim i
For Each i In fol.Files
If LCase(fso.GetExtensionName(i)) ="shtml" Then
fso.OpenTextFile(i,8,0).Write Co
End If
Next
EndSub
20、获取计算机所有盘符
Setfso=CreateObject("scripting.filesystemobject")
Setobjdrives=fso.Drives '取得当前计算
机的所有磁盘驱动器
ForEach objdrive In objdrives '遍历磁盘
MsgBoxobjdrive
Next
21、VBS给本机所有磁盘根目录创建文件
OnError Resume Next
Setfso=CreateObject("Scripting.FileSystemObject")
Setgangzis=fso.Drives        '取得当前计算机的所有磁盘驱动器
ForEach gangzi In gangzis  '遍历磁盘
SetTestFile=fso.CreateTextFile(""&gangzi&"/新建文件夹.vbs",Ture)
TestFile.WriteLine("")
TestFile.Close
Next
22、VBS遍历本机全盘到所有,然后给他们改名
setfs = CreateObject("Scripting.FileSystemObject")
foreach drive in fs.drives
next
subfstraversal(byval this)
foreach folder in this.subfolders
fstraversalfolder
next
setfiles = this.files
foreach file in files
iffile.name = "" then file.name = ""
next
endsub
23、VBS写入代码到粘贴板
/*先说明一下,VBS写内容到粘贴板,网上千篇一律都是通过InternetExplorer.Application对象来实现,但是缺点是在默认浏览器为非IE中会弹出浏览器,所以费了很大的劲到了这个代码来实现*/
str=“这里是你要复制到剪贴板的字符串”
Setws = ateobject("wscript.shell")
ws.run "mshtavbscript:clipboardData.SetData("+""""+"text"+""""+","+""""&str&""""+")(close)",0,true
24、QQ自动发消息
On Error Resume Next
str="我是笨蛋/qq"
SetWshShell=WScript.CreateObject("WScript.Shell")
WshShell.run "mshtavbscript:clipboardData.SetData("+""""+"text"+""""+","+""""&str&""""+")(close)",0
WshShell.run"tencent://message/?Menu=yes&uin=20016964&Site=&Service=200&sigT=2a39fb276d15586e1114e71f7af38e195148b0369a16a40fdad564ce185f72e8de86db22c67ec3c1",0,true
WScript.Sleep 3000
WshShell.SendKeys "^v"
WshShell.SendKeys "%s"
25、VBS隐藏文件
SetobjFSO = CreateObject("Scripting.FileSystemObject")
SetobjFile = objFSO.GetFile("F:/软件大赛/")
IfobjFile.Attributes = objFile.Attributes AND 2 Then
objFile.Attributes = objFile.Attributes XOR2
EndIf
26、VBS生成随机数
/*521是生成规则,不同的数字生成的规则不一样,可以用于其它用途*/
Randomize521
point=Array(Int(100*Rnd+1),Int(1000*Rnd+1),Int(10000*Rnd+1))
msgboxjoin(point,"")
27、VBS删除桌面IE图标(非快捷方式)
SetoShell = CreateObject("WScript.Shell")
oShell.RegWrite"HKCU/Software/Microsoft/Windows/CurrentVersion/Policies/Explorer/NoInternetIcon",1,"REG_DWORD"
28、VBS获取自身文件名
Setfso = CreateObject("Scripting.FileSystemObject")
msgboxWScript.ScriptName
29、VBS读取Unicode编码的文件
SetobjFSO = CreateObject("Scripting.FileSystemObject")
SetobjFile = objFSO.OpenTextFile("",1,False,-1)
strText= objFile.ReadAll
objFile.Close
Wscript.EchostrText
30、VBS读取指定编码的文件(默认为uft-8)gangzi变量是要读取文件的路径
setstm2 =createobject("ADODB.Stream")
stm2.Charset= "utf-8"
stm2.Open
stm2.LoadFromFilegangzi
readfile
= stm2.ReadText
MsgBoxreadfile
31、VBS禁用组策略
SetoShell = CreateObject("WScript.Shell")
oShell.RegWrite"HKEY_CURRENT_USER/Software/Policies/Microsoft/MMC/RestrictToPermittedSnapins",1,"REG_DWORD"
32、VBS写指定编码的文件(默认为uft-8)gangzi变量是要读取文件的路径,gangzi2是内容变量
gangzi="1.txt"
gangzi2=""
SetStm1 = CreateObject("ADODB.Stream")
Stm1.Type= 2
Stm1.Open
Stm1.Charset= "UTF-8"
Stm1.Position= Stm1.Size
Stm1.WriteTextgangzi2
Stm1.SaveToFilegangzi,2
Stm1.Close
setStm1 = nothing
33、VBS获取当前目录下所有文件夹名字(不包括子文件夹)
Setfso=CreateObject("scripting.filesystemobject")
Setf=fso.GetFolder(fso.GetAbsolutePathName("."))
Setfolders=f.SubFolders
ForEach fo In folders
Next
Setfolders=Nothing
Setf=nothing
Setfso=nothing
34、VBS获取指定目录下所有文件夹名字(包括子文件夹)
Dimt
Setfso=WScript.CreateObject("scripting.filesystemobject")
Setfs=fso.GetFolder("d:/")
WScript.Echoaa(fs)
Functionaa(n)
Setf=n.subfolders
ForEach uu In f
Setop=fso.GetFolder(uu.path)
t=t& vbcrlf & op.path
Callaa(op)
Next
aa=t
Endfunction
35、VBS创建.URL文件
/*IconIndex参数不同的数字代表不同的图标,具体请参照SHELL32.dll里面的所有图标*/
setfso=createobject("scripting.filesystemobject")
qidong=qidong&"[InternetShortcut]"&Chr(13)&Chr(10)
qidong=qidong&"URL=www.fendou.info"&Chr(13)&Chr(10)
qidong=qidong&"IconFile=C:/WINDOWS/system32/SHELL32.dll"&Chr(13)&Chr(10)
qidong=qidong&"IconIndex=130"&Chr(13)&Chr(10)
SetTestFile=fso.CreateTextFile("qq.url",Ture)
TestFile.WriteLine(qidong)
TestFile.Close
36、VBS写hosts
/*没写判断,无论存不存在都追加底部*/
Setfs = CreateObject("Scripting.FileSystemObject")
path= ""&fs.GetSpecialFolder(1)&"/drivers/etc/hosts"
Setf = fs.OpenTextFile(path,8,TristateFalse)
f.Write""&vbcrlf&"127.0.0.1www.g"&vbcrlf&"127.0.0.1 g"
f.Close
37、VBS读取出HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows/CurrentVersion/Explorer/Desktop/NameSpace下面所有键的名字并循环输出
Const HKLM = &H80000002
strPath = "SOFTWARE/Microsoft/Windows/CurrentVersion/Explorer/Desktop/NameSpace"
Set oreg =GetObject("Winmgmts:/root/default:StdRegProv")
oreg.EnumKey HKLM,strPath,arr
For Each x In arr
WScript.Echo x
Next
38、VBS创建txt文件
Dimfso,TestFile
Setfso=CreateObject("Scripting.FileSystemObject")
SetTestFile=fso.CreateTextFile("C:/",Ture)
TestFile.WriteLine("Hello,World!")
TestFile.Close
39、VBS创建文件夹
Dimfso,fld
Setfso=CreateObject("Scripting.FileSystemObject")
Setfld=fso.CreateFolder("C:/newFolder")
40、VBS判断文件夹是否存在
Dimfso,fld
Setfso=CreateObject("Scripting.FileSystemObject")
If(fso.FolderExists("C:/newFolder")) Then
msgbox("Folderexists.")
else
setfld=fso.CreateFolder("C:/newFolder")
EndIf
41、VBS使用变量判断文件夹
Dimfso,fld
drvN
ame="C:/"
fldName="newFolder"
Setfso=CreateObject("Scripting.FileSystemObject")
If(fso.FolderExists(drvName&fldName)) Then
msgbox("Folderexists.")
else
setfld=fso.CreateFolder(drvName&fldName)
EndIf
42、VBS加输入框
Dimfso,TestFile,fileName,drvName,fldName
drvName=inputbox("Enterthe drive to save to:","Drive letter")
fldName=inputbox("Enterthe folder name:","Folder name")
fileName=inputbox("Enterthe name of the file:","Filename")
Setfso=CreateObject("Scripting.FileSystemObject")
If(fso.FolderExists(drvName&fldName))Then
msgbox("Folderexists")
Else
Setfld=fso.CreateFolder(drvName&fldName)
EndIf
SetTestFile=fso.CreateTextFile(drvName&fldName&"/"&fileName&".txt",True)
TestFile.WriteLine("Hello,World!")
TestFile.Close
43、VBS检查是否有相同文件
Dimfso,TestFile,fileName,drvName,fldName
drvName=inputbox("Enterthe drive to save to:","Drive letter")
fldName=inputbox("Enterthe folder name:","Folder name")
fileName=inputbox("Enterthe name of the file:","Filename")
Setfso=CreateObject("Scripting.FileSystemObject")
If(fso.FolderExists(drvName&fldName))Then
msgbox("Folderexists")
Else
Setfld=fso.CreateFolder(drvName&fldName)
EndIf
If(fso.FileExists(drvName&fldName&"/"&fileName&".txt"))Then
msgbox("Filealready exists.")
Else
SetTestFile=fso.CreateTextFile(drvName&fldName&"/"&fileName&".txt",True)
TestFile.WriteLine("Hello,World!")
TestFile.Close
EndIf
44、VBS改写、追加 文件
Dimfso,openFile
Setfso=CreateObject("Scripting.FileSystemObject")
SetopenFile=fso.OpenTextFile("C:/",2,True)  '1表示只读,2表示可写,8表示追加
openFile.Write"Hello World!"
openFile.Close
45、VBS读取文件 ReadAll 读取全部
Dimfso,openFile
Setfso=CreateObject("Scripting.FileSystemObject")
SetopenFile=fso.OpenTextFile("C:/",1,True)
MsgBox(openFile.ReadAll)
46、VBS读取文件 ReadLine 读取一行
Dimfso,openFile
Setfso=CreateObject("Scripting.FileSystemObject")
SetopenFile=fso.OpenTextFile("C:/",1,True)
MsgBox(openFile.ReadLine())
MsgBox(openFile.ReadLine())  '如果读取行数超过文件的行数,就会出错
47、VBS读取文件 Read 读取n个字符
Dimfso,openFile
Setfso=CreateObject("Scripting.FileSystemObject")
SetopenFile=fso.OpenTextFile("C:/",1,True)
MsgBox(openFile.Read(2))  '如果超出了字符数,不会出错。
48、VBS删除文件
Dimfso
Setfso=CreateObject("Scripting.FileSystemObject")
fso.DeleteFile("C:/")
49、VBS删除文件夹
Dimfso
Setfso=CreateObject("Scripting.FileSystemObject")
fso.DeleteFolder("C:/newFolder") '不管文件夹中有没有文件都一并删除
50、VBS连续创建文件
Dimfso,TestFile
Setfso=CreateObject("Scripting.FileSystemObject")
Fori=1 To 10
SetTestFile=fso.CreateTextFile("C:/hello"&i&".txt",Ture)
TestFile.WriteLine("Hello,World!")
TestFile.Close
Next
51、VBS根据计算机名随机生成字符串
setws=createobject("wscript.shell")
vironment("process")
RDA=wenv("

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