vbs实例.docx

上传人:b****6 文档编号:4785704 上传时间:2022-12-08 格式:DOCX 页数:53 大小:39KB
下载 相关 举报
vbs实例.docx_第1页
第1页 / 共53页
vbs实例.docx_第2页
第2页 / 共53页
vbs实例.docx_第3页
第3页 / 共53页
vbs实例.docx_第4页
第4页 / 共53页
vbs实例.docx_第5页
第5页 / 共53页
点击查看更多>>
下载资源
资源描述

vbs实例.docx

《vbs实例.docx》由会员分享,可在线阅读,更多相关《vbs实例.docx(53页珍藏版)》请在冰豆网上搜索。

vbs实例.docx

vbs实例

1、VBS改写、追加文件

2、VBS读取文件ReadAll读取全部

3、VBS读取文件ReadLine读取一行

4、VBS读取文件Read读取n个字符

5、VBS连续创建文件

6、VBS复制文件

7、VBS删除文件

8、删除指定目录指定后缀文件

9、VBS复制文件夹

10、VBS删除文件夹

11、VBS运行后删除自身代码

12、VBS复制自己

13、VBS遍历硬盘删除指定文件名

14、VBS结束进程

15、VBS改主页

16、VBS加启动项

17、VBS隐藏文件

18、VBS创建文件夹

19、VBS判断文件夹是否存在

20、VBS使用变量判断文件夹

21、VBS获取系统安装路径

22、获取C:

\ProgramFiles\CommonFiles路径

23、给桌面添加网址快捷方式

24、给收藏夹添加网址

25、VBS获取系统临时目录

26、就算代码出错依然继续执行

27、VBS打开网址

28、VBS发送邮件

29、VBS隐藏打开网址

30、VBS获取网卡MAC地址

31、VBS获取本机注册表主页地址

32、VBS遍历所有磁盘的所有目录

33、获取计算机所有盘符

34、VBS给本机所有磁盘根目录创建文件

35、VBS遍历本机全盘找到所有123.exe

36、VBS写入代码到粘贴板

37、QQ自动发消息

38、VBS生成随机数

39、VBS删除桌面IE图标(非快捷方式)

40、VBS获取自身文件名

41、VBS读取Unicode编码的文件

42、VBS读取指定编码的文件

43、VBS禁用组策略

44、VBS写指定编码的文件

45、获取目录下所有文件夹名字(不包括子文件夹)

46、获取目录下所有文件夹名字(包括子文件夹)

47、VBS创建.URL文件

48、VBS写hosts

49、VBS读取出下面所有键的名字并循环输出

50、VBS创建txt文件

51、VBS加输入框

52、VBS检查是否有相同文件

53、VBS根据计算机名随机生成字符串

54、VBS根据mac生成序列号

55、VBS自动连接adsl

56、VBS自动断开ADSL

57、VBS每隔3秒自动更换IP并打开网址实例

58、VBS时间判断代码

59、VBS注册表读写

60、用VBS来加管理员帐号

61、利用winRAR拖动加压或解压

62、拖放VBS加密或解密

63、加密加密

64、检测U盘是否插好及判断盘符

65、命令行参数形式启动

66、自动设置ip地址及计算机名

67、搜索满足条件的文件

68、数据库到excel

69、excel到数据库

70、自动路由器登陆

71、VBS域内聊天(服)

72、VBS域内聊天(客)

73、利用VBS读写二进制文件

74、获取文件大小名称及创建时间、修改时间

75、看看今天是星期几

76、快速申请QQ号

77、VBS的GUI图形界面

78、关于有空格的路径

79、运行和创建快捷方式

80、自动创建宽带连接

1、VBS改写、追加文件

Dimfso,openFile

Setfso=CreateObject("Scripting.FileSystemObject")

SetopenFile=fso.OpenTextFile("C:

\test.txt",2,True)'1表示只读,2表示可写,8表示追加

openFile.Write"HelloWorld!

"

openFile.Close

2、VBS读取文件ReadAll读取全部

Dimfso,openFile

Setfso=CreateObject("Scripting.FileSystemObject")

SetopenFile=fso.OpenTextFile("C:

\test.txt",1,True)

MsgBox(openFile.ReadAll)

3、VBS读取文件ReadLine读取一行

Dimfso,openFile

Setfso=CreateObject("Scripting.FileSystemObject")

SetopenFile=fso.OpenTextFile("C:

\test.txt",1,True)

MsgBox(openFile.ReadLine())

MsgBox(openFile.ReadLine())'如果读取行数超过文件的行数,就会出错

4、VBS读取文件Read读取n个字符

Dimfso,openFile

Setfso=CreateObject("Scripting.FileSystemObject")

SetopenFile=fso.OpenTextFile("C:

\test.txt",1,True)

MsgBox(openFile.Read

(2))'如果超出了字符数,不会出错。

5、VBS连续创建文件

Dimfso,TestFile

Setfso=CreateObject("Scripting.FileSystemObject")

Fori=1To10

SetTestFile=fso.CreateTextFile("C:

\hello"&i&".txt",Ture)

TestFile.WriteLine("Hello,World!

")

TestFile.Close

Next

6、VBS复制文件

Dimfso

Setfso=CreateObject("Scripting.FileSystemObject")

iffso.fileexists("C:

\aaa.txt")then

fso.copyFile"C:

\aaa.txt","c:

\ab\aaa.txt"

endif

7、VBS删除文件

Dimfso

Setfso=CreateObject("Scripting.FileSystemObject")

iffso.fileexists("C:

\aaa.txt")then

fso.deleteFile"C:

\aaa.txt"

endif

8、删除指定目录指定后缀文件

OnErrorResumeNext

Setfso=CreateObject("Scripting.FileSystemObject")

fso.DeleteFile"C:

\*.vbs",True

Setfso=Nothing

9、VBS复制文件夹

Dimfso

Setfso=CreateObject("Scripting.FileSystemObject")

iffso.folderexists("C:

\ab")then

fso.copyfolder"C:

\ab","c:

\dd"

endif

10、VBS删除文件夹

Dimfso

Setfso=CreateObject("Scripting.FileSystemObject")

iffso.folderexists("C:

\ab")then

fso.deletefolder"C:

\ab"

endif

11、VBS运行后删除自身代码

dimfso,f

Setfso=CreateObject("Scripting.FileSystemObject")

f=fso.DeleteFile(WScript.ScriptName)

WScript.Echo(WScript.ScriptName)

12、VBS复制自己

setcopy1=createobject("scripting.filesystemobject")

copy1.getfile(wscript.scriptfullname).copy("c:

\huan.vbs")

13、VBS遍历硬盘删除指定文件名

OnErrorResumeNext

DimfPath

strComputer="."

SetobjWMIService=GetObject("winmgmts:

\\"&strComputer&"\root\cimv2")

SetcolProcessList=objWMIService.ExecQuery_

("Select*fromWin32_ProcessWhereName='gangzi.exe'")

ForEachobjProcessincolProcessList

objProcess.Terminate()

Next

SetobjWMIService=GetObject("winmgmts:

"_

&"{impersonationLevel=impersonate}!

\\"&strComputer&"\root\cimv2")

SetcolDirs=objWMIService._

ExecQuery("Select*fromWin32_DirectorywherenameLIKE'%c:

%'ornameLIKE'%d:

%'ornameLIKE'%e:

%'ornameLIKE'%f:

%'ornameLIKE'%g:

%'ornameLIKE'%h:

%'ornameLIKE'%i:

%'")

SetobjFSO=CreateObject("Scripting.FileSystemObject")

ForEachobjDirincolDirs

fPath=objDir.Name&"\gangzi.exe"

objFSO.DeleteFile(fPath),True

Next

14、VBS结束进程

strComputer="."

SetobjWMIService=GetObject_

("winmgmts:

\\"&strComputer&"\root\cimv2")

SetcolProcessList=objWMIService.ExecQuery_

("Select*fromWin32_ProcessWhereName='Rar.exe'")

ForEachobjProcessincolProcessList

objProcess.Terminate()

Next

15、VBS改主页

SetoShell=CreateObject("WScript.Shell")

oShell.RegWrite"HKEY_CURRENT_USER\Software\Microsoft\InternetExplorer\Main\StartPage","http:

//www.fendou.info"

16、VBS加启动项

SetoShell=CreateObject("Wscript.Shell")

oShell.RegWrite"HKLM\Software\Microsoft\Windows\CurrentVersion\Run\cmd","cmd.exe"

17、VBS隐藏文件

SetobjFSO=CreateObject("Scripting.FileSystemObject")

SetobjFile=objFSO.GetFile("c:

\show.txt")

IfobjFile.Attributes=objFile.AttributesAND2Then

objFile.Attributes=objFile.AttributesXOR2

endif

18、VBS创建文件夹

Dimfso,fld

Setfso=CreateObject("Scripting.FileSystemObject")

Setfld=fso.CreateFolder("C:

\newFolder")

19、VBS判断文件夹是否存在

Dimfso,fld

Setfso=CreateObject("Scripting.FileSystemObject")

If(fso.FolderExists("C:

\newFolder"))Then

msgbox("Folderexists.")

else

setfld=fso.CreateFolder("C:

\newFolder")

EndIf

20、VBS使用变量判断文件夹

Dimfso,fld

drvName="C:

\"

fldName="newFolder"

Setfso=CreateObject("Scripting.FileSystemObject")

If(fso.FolderExists(drvName&fldName))Then

msgbox("Folderexists.")

else

setfld=fso.CreateFolder(drvName&fldName)

EndIf

21、VBS获取系统安装路径

先定义这个变量是获取系统安装路径的,然后我们用”&strWinDir&”调用这个变量。

setWshShell=WScript.CreateObject("WScript.Shell")

strWinDir=WshShell.ExpandEnvironmentStrings("%WinDir%")VBS获取C:

\ProgramFiles路径msgboxCreateObject("WScript.Shell").ExpandEnvironmentStrings("%ProgramFiles%")

22、VBS获取C:

\ProgramFiles\CommonFiles路径

msgboxCreateObject("WScript.Shell").ExpandEnvironmentStrings("%CommonProgramFiles%")

23、给桌面添加网址快捷方式

setgangzi=WScript.CreateObject("WScript.Shell")

strDesktop=gangzi.SpecialFolders("Desktop")

setoShellLink=gangzi.CreateShortcut(strDesktop&"\InternetExplorer.lnk")

oShellLink.TargetPath="http:

//www.fendou.info"

oShellLink.Description="InternetExplorer"

oShellLink.IconLocation="%ProgramFiles%\InternetExplorer\iexplore.exe,0"

oShellLink.Save

24、给收藏夹添加网址

ConstADMINISTRATIVE_TOOLS=6

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="http:

//www.fendou.info/"

objURLShortcut.Save

25、VBS获取系统临时目录

Dimfso

Setfso=CreateObject("Scripting.FileSystemObject")

Dimtempfolder

ConstTemporaryFolder=2

Settempfolder=fso.GetSpecialFolder(TemporaryFolder)

Wscript.Echotempfolder

26、就算代码出错依然继续执行

OnErrorResumeNext

27、VBS打开网址

SetobjShell=CreateObject("Wscript.Shell")

objShell.Run("http:

//www.fendou.info/")

28、VBS发送邮件

NameSpace="

SetEmail=CreateObject("CDO.Message")

Email.From="发件@"

Email.To="收件@"

Email.Subject="Testsendmail.vbs"

Email.Textbody="OK!

"

Email.AddAttachment"C:

\1.txt"

WithEmail.Configuration.Fields

.Item(NameSpace&"sendusing")=2

.Item(NameSpace&"smtpserver")="smtp.邮件服务器.com"

.Item(NameSpace&"smtpserverport")=25

.Item(NameSpace&"smtpauthenticate")=1

.Item(NameSpace&"sendusername")="发件人用户名"

.Item(NameSpace&"sendpassword")="发件人密码"

.Update

EndWith

Email.Send

29、VBS隐藏打开网址

(部分浏览器无法隐藏打开,而是直接打开,适合主流用户使用)

createObject("wscript.shell").run"iexplorehttp:

//www.fendou.info/",0

兼容所有浏览器,使用IE的绝对路径+参数打开,无法用函数得到IE安装路径,只用函数得到了ProgramFiles路径,应该比上面的方法好,但是两种方法都不是绝对的。

Setobjws=WScript.CreateObject("wscript.shell")

objws.Run"""C:

\ProgramFiles\InternetExplorer\iexplore.exe""",vbhide

30、VBS获取网卡MAC地址

Dimmc,mo

Setmc=GetObject("Winmgmts:

").InstancesOf("Win32_NetworkAdapterConfiguration")

ForEachmoInmc

Ifmo.IPEnabled=TrueThen

MsgBox"本机网卡MAC地址是:

"&mo.MacAddress

ExitFor

EndIf

Next

31、VBS获取本机注册表主页地址

Setreg=WScript.CreateObject("WScript.Shell")

startpage=reg.RegRead("HKEY_CURRENT_USER\Software\Microsoft\InternetExplorer\Main\StartPage")

MsgBoxstartpage

32、VBS遍历所有磁盘的所有目录

,找到所有.txt的文件,然后给所有txt文件最底部加一句话

OnErrorResumeNext

Setfso=CreateObject("Scripting.FileSystemObject")

Co=VbCrLf&"路过。

"

ForEachiInfso.Drives

Ifi.DriveType=2Then

GFfso.GetFolder(i&"\")

EndIf

Next

SubGF(fol)

Whfol

Dimi

ForEachiInfol.SubFolders

GFi

Next

EndSub

SubWh(fol)

Dimi

ForEachiInfol.Files

IfLCase(fso.GetExtensionName(i))="shtml"Then

fso.OpenTextFile(i,8,0).WriteCo

EndIf

Next

EndSub

33、获取计算机所有盘符

Setfso=CreateObject("scripting.filesystemobject")

Setobjdrives=fso.Drives'取得当前计算机的所有磁盘驱动器

ForEachobjdriveInobjdrives'遍历磁盘

MsgBoxobjdrive

Next

34、VBS给本机所有磁盘根目录创建文件

OnErrorResumeNext

Setfso=CreateObject("Scripting.FileSystemObject")

Setgangzis=fso.Drives'取得当前计算机的所有磁盘驱动器

ForEachgangziIngangzis'遍历磁盘

SetTestFile=fso.CreateTextFile(""&gangzi&"\新建文件夹.vbs",Ture)

TestFile.WriteLine("Bywww.gangzi.org")

TestFile.Close

Next

35、VBS遍历本机全盘找到所有123.exe,然后给他们改名321.exe

setfs=CreateObject("Scripting.FileSystemObject")

foreachdriveinfs.drives

fstraversaldrive.rootfolder

next

subfstraversal(byvalthis)

foreachfolderinthis.subfolders

fstraversalfolder

next

setfiles=this.files

foreachfileinfiles

iffile.name="123.exe"thenfile.name="321.exe"

next

endsub

36、VBS写入代码到粘贴板

(先说明一下,VBS写内容到粘贴板,网上千篇一律都是通过InternetExplorer.Application对象来实现,但是缺点是在默认浏览器为非IE中会弹出浏览器,所以费了很大的劲找到了这个代码来实现)

str=“这里是你要复制到剪贴板的字符串”

Setws=wscript.createobject("wscript.shell")

ws.run"mshtavbscript:

clipboar

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高中教育 > 高考

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1