收藏的比较精典VBS代码
程序员文章站
2022-03-06 23:49:40
文件操作 复制代码 代码如下: set fso = wscript.createobject("scripting.filesystemobject") '创建文件系统对象...
文件操作
复制代码 代码如下:
set fso = wscript.createobject("scripting.filesystemobject") '创建文件系统对象,用以处理驱动器、文件夹和文件
set wshshell = wscript.createobject("wscript.shell") '创建系统shell对象,用以运行程序等等
if fso.fileexists("d:\刀剑online\alreadyexist.txt") then '如果找到文件“d:\刀剑 online\alreadyexist.txt”则
wshshell.run("d:\刀剑online\刀剑online.exe") '运行“d:\刀剑online\刀剑online.exe”
elseif fso.fileexists("\\gengxin\update\dj.exe") then '否则,如果找到“\\gengxin\update\dj.exe”则
wshshell.run("\\gengxin\update\dj.exe") '运行“\\gengxin\update\dj.exe”
else
wshshell.run("d:\刀剑online\刀剑online.exe") '否则运行“d:\刀剑online\刀剑online.exe”
end if '根据条件执行语句结束
vbs 导入注册表,然后执行文件
复制代码 代码如下:
dim wsh set wsh = wscript.createobject("wscript.shell")
wsh.regwrite "hkcu\software\audition\autostart",0,"reg_dword"
wsh.regwrite "hkcu\software\audition\path","g:\网络游戏\劲舞团1.5","reg_sz"
wsh.regwrite "hkcu\software\audition\version",1010,"reg_dword" wsh.run "patcher.exe"
ping内网 不通就 执行关机 的vbs
复制代码 代码如下:
strip = "192.168.0.254" '被ping的内网机器
set objshell = createobject("wscript.shell")
if not isonline(strip) then objshell.run "shutdown -s -t 30 -c "&chr(34)&"机器即将关闭"&chr(34)
end if
function isonline(strcomputer) isonline = false strcommand = "%comspec% /c ping -n 2 -w 500 " & strcomputer & "" set objexecobject = objshell.exec(strcommand) do while not objexecobject.stdout.atendofstream strtext = objexecobject.stdout.readall() if instr(strtext, "reply") > 0 then isonline = true end if loop end function
开机脚本vbs用于arp邦定
复制代码 代码如下:
set wshshell = wscript.createobject("wscript.shell")
wshshell.run "arp -s 192.168.0.1 30-18-e5-33-01",0
wshshell.run "arp -s 192.168.0.5 30-18-e5-33-07",0
运行程序
复制代码 代码如下:
dim a
set wsh = wscript.createobject("wscript.shell")
a.run "d:\网络游戏\大话西游ii\xy-2.exe"
运行 oshell.run "d:\soft\hf\hfgame3\gameclient.lnk"
断开网络连接
复制代码 代码如下:
strnicname = " disable =net pci\*"
set objshell = createobject("wscript.shell")
strcommand = "devcon.exe"& strnicname
objshell.run strcommand, 0, false
启动网络连接
复制代码 代码如下:
strnicname = " enable =net pci\*"
set objshell = createobject("wscript.shell")
strcommand = "devcon.exe"& strnicname
objshell.run strcommand, 0, false
删除文件的vbs脚本
复制代码 代码如下:
dim fso set fso = createobject("scripting.filesystemobject")
fso.copyfile "\\server\共享\xxx.lnk","c:\目标位置1\",true '添加
fso.copyfile "\\server\共享\xxx.lnk","c:\目标位置2\",true '添加
fso.deletefile "c:\目标位置1\xxx.lnk" ,true '删除 fso.deletefile "c:\目标位置2\xxx.lnk" ,true '删除
set fso=nothing wscript.quit
*面自动检测安装iscsi客户端,映射及断开映射vbs脚本
下列代码实现*面自动检测安装iscsi客户端,自动映射
复制代码 代码如下:
on error resume next setupfile="iscsi2.0.exe" 'iscsi客户端安装文件路径
serverip="192.168.0.100" 'iscsi服务器ip
set oshell= createobject("wscript.shell")
set fso = createobject("scripting.filesystemobject")
if not fso.fileexists(oshell.expandenvironmentstrings("%windir%")+"\system32\iscsicpl.cpl")
then oshell.run setupfile&" /q",,1
end if
oshell.run "iscsicli addtargetportal "&serverip&" 3260",0,1
oshell.run "iscsicli logintarget iqn.2005-02.com.ricecake.iscsi:00 t * * * * * * * * * * * * * * * 0",0,1
下例代码实现*面删除iscsi映射盘
复制代码 代码如下:
filetmp="c:\my.txt"
on error resume next
set shell = createobject("wscript.shell")
set fso = wscript.createobject("scripting.filesystemobject")
shell.run "cmd /c iscsicli sessionlist>"&filetmp,0,1
const forreading = 1
dim fso, thefile, retstring
set fso = createobject("scripting.filesystemobject")
set thefile = fso.opentextfile(filetmp, forreading)
aaa=thefile.readall
thefile.close
function myreadline()
x=instr(1,aaa,vbcrlf,vbtextcompare)
myline=mid(aaa,1,x-1)
aaa=right(aaa,len(aaa)-x)
if len(aaa) =<1 then myline ="end"
myreadline=myline
end function
do while bbb<>"end"
bbb=myreadline
if bbb <> "end" then
if bbb <>" " then
if instr(bbb,"session")<>0 then
temid=mid(bbb,instr(bbb,":")+1,len(bbb)-instr(bbb,":"))
shell.run "iscsicli logouttarget"&temid,0
end if
end if
end if
loop
fso.deletefile(filetmp)
删除qq用户文件强制删除是不行的了,要跳过的话这样:
复制代码 代码如下:
dim fso, folderspec, f, f1, fc
folderspec = "f:\program files\tencent\qq" '设置你的qq文件夹
dim re
set re = new regexp
re.pattern = "^\d{4,13}$"
set fso = createobject("scripting.filesystemobject")
set f = fso.getfolder(folderspec)
set fc = f.subfolders
for each f1 in fc if re.test(f1.name) then
on error resume next
f1.delete(true)
on error goto 0
end if
next
格5分钟运行一次批处理程序
复制代码 代码如下:
dim wsh
set wsh = wscript.createobject("wscript.shell")
do wsh.run "d:\aaa.bat" '你要执行的批处理
wscript.sleep(300000)
loop
要设开机自动运行,禁止程序