**vbs调用外部程序**
WSH也就是用来解析Vbs的宿主,本身包含了几个个常用对象:
1、Scripting.FileSystemObject —> 提供一整套文件系统操作函数
2、Scripting.Dictionary —> 用来返回存放键值对的字典对象
3、Wscript.Shell —> 提供一套读取系统信息的函数,如读写注册表、查找指定文件的路径、读取DOS环境变量,读取链接中的设置
4、Wscript.NetWork —> 提供网络连接和远程打印机管理的函数。(其中,所有Scripting对象都存放在SCRRUN.DLL文件中,所有的Wscript对象都存放在WSHOM.ocx文件中。)
———————————————————————————————————
(打开对应应用如记事本,计算器,文件名或者路径)
option Explicit '规范定义告诉计算机先声明后使用
dim obj '变量名
Set obj = CreateObject("Wscript.Shell") 'set将一个对象引用赋给变量
'加true只打开对应的程序,关闭过后才打开下一个
'不加true,两个同时打开
obj.Run "notepad",,true 'notepad 记事本 calc 计算器 winword word文件名
obj.Run """路径名""" '也可以包含路径名,用三个双引号来包含
注意:
run 执行程序的路径,窗口的形式,脚本等待还是继续执行 (三个参数)
run也有返回值,1成功执行,0执行失败
———————————————————————————————————
处理错误
On Error Resume Next ——如果有错误跳过执行下一条语句
err 对象
on error resume next '如果有错误跳过执行吓一跳
a=11
b=0
c=a/b
if err.number<>0 then 'err.num 错误的个数
'description 原因 source来源
wscript.echo err.number & err.description & err.source
end if
创建一个文件
dim fs,s
set fs = wscript.createobject("scripting.filesystemobject") '创建一个对象
if(fs.folderexists("C:\Users\93997\Desktop\练习\1")) then '判断文件是否存在
s = "is available"
else
s = "not exiet"
set folder = fs.createfolder("C:\Users\93997\Desktop\练习\1") '不存在创建一个文件
end if
wscript.echo s '窗口提示
———————————————————————————————————
打印时间日期
dim date,clock
date = "February 6,2020" '格式也可以写成#2/6/2020,"16:09:53 PM"
clock = "16:10:59 PM" '时间也可以这样写
if(isdate(date)) then 'isdate 用来判断是否可以转换日期
date = CDate(date)
else
msgbox "不能转换"
end if
clock = CDate(clock)
msgbox "当前日期为:" & date & " " & clock '最后打印出来
———————————————————————————————————
删除文件
dim fs
set fs = createobject("scripting.filesystemobject")
if(fs.folderexists("xxxxxxxxxxxxxxxxxx")) then '判断文件是否存在
fs.deletefolder("xxxxxxxxxxxxxx")
msgbox "删除成功"
else
msgbox "文件不存在"
end if
———————————————————————————————————
自动创建文本文件
dim wshshell,autosavetime,txtfilename
autosavetime = 3000
set wshshell = createobject("wscript.shell")
txtfilename = inputbox("请输入你要创建的文件名:")
'打开激活记事本
wshshell.run "notepad"
wscript.sleep 200
wshshell.appactivate "无标题-记事本"
'三用输入名字存盘
wshshell.sendkeys "^s" 'ctrl+s
wscript.sleep 300
wshshell.sendkeys txtfilename
wscript.sleep 300
wshshell.sendkeys "%s" 'alt+s
wscript.sleep autosavetime
'四自动定时存盘
while wshshell.appactivate (txtfilename) = true 'appactivate 是定位到窗口的名字
wshshell.sendkeys "%s"
wscript.sleep autosavetime
wend
wscript.quit
———————————————————————————————————
刷屏
dim s
set s = createobject("wscript.shell")
s.appactivate "长路呀" '通过应用标题来找位置
wscript.sleep 2000
for i=0 to 20
s.sendkeys "1"
s.sendkeys "{ENTER}"
next
**查阅:https://www.jb51.net/article/53280.htm**