![](https://iknow-base.cdn.bcebos.com/lxb/notice.png)
用vbs编写:如果a.exe存在,就执行。如果a.exe不存在就执行b.exe 怎么做?
http://zhidao.baidu.com/question/104515710.html?an=0&si=1这个帖子的答案,永远也执行不了a.exe一定执行的是b....
http://zhidao.baidu.com/question/104515710.html?an=0&si=1 这个帖子的答案,永远也执行不了a.exe 一定执行的是b.exe 即使a.exe存在也会执行b.exe 不知道是不是哪里写错了。
例如:双击打开 x.vbs 后,第一路径指向c:\123\a.exe .(相当于批处理start c:\123\a.exe)如果这个文件存在,就运行。如果不存在运行第二指向d:\xyz\b.vbs (相当于批处理d:\xyz\b.vbs )
请问,x.vbs 的脚本要怎么写。 展开
例如:双击打开 x.vbs 后,第一路径指向c:\123\a.exe .(相当于批处理start c:\123\a.exe)如果这个文件存在,就运行。如果不存在运行第二指向d:\xyz\b.vbs (相当于批处理d:\xyz\b.vbs )
请问,x.vbs 的脚本要怎么写。 展开
4个回答
展开全部
set list=getobject("winmgmts:\\.\root\cimv2").execquery("select * from win32_process")
set shell=createobject("wscript.shell")
function pfv(byval find)
for each i in list
if instr(lcase(i.caption),lcase(find))>0 then pfv=i.caption:exit for:else pfv=""
next
end function
if pfv("a.exe")<>""then shell.run "a.exe" else shell.run "b.exe"
set shell=createobject("wscript.shell")
function pfv(byval find)
for each i in list
if instr(lcase(i.caption),lcase(find))>0 then pfv=i.caption:exit for:else pfv=""
next
end function
if pfv("a.exe")<>""then shell.run "a.exe" else shell.run "b.exe"
更多追问追答
追问
也不对啊,不过你的比一楼给的要正确。一楼是无限循环验证,b被执行后关闭了一会儿会继续被打开。我看了很多种写法都是无论a存在与否都会执行b。难道你们指定必须是exe文件?我a用exe,b用vbs可以不?或者两种不同的而文件写法有所不同?
追答
我是按照你的逻辑写的不对我就没办法了,你自己好好解释清楚,解释清楚后再说.
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
经验证 代码绝对可行
option Explicit
dim wmi,proc,procs,proname,flag,WshShell
Do
proname="a.exe" '输入要检测的进程名称
set wmi=getobject("winmgmts:{impersonationlevel=impersonate}!\\.\root\cimv2")
set procs=wmi.execquery("select * from win32_process")
flag=true
for each proc in procs
if strcomp(proc.name,proname)=0 then
flag=false
exit for
end if
next
set wmi=nothing
if flag then
Set WshShell = Wscript.CreateObject("Wscript.Shell")
WshShell.Run ("C:\b.exe") '输入你要执行的B.exe的路径
end if
wscript.sleep 50000000 '检测间隔时间,这里是50秒
loop
option Explicit
dim wmi,proc,procs,proname,flag,WshShell
Do
proname="a.exe" '输入要检测的进程名称
set wmi=getobject("winmgmts:{impersonationlevel=impersonate}!\\.\root\cimv2")
set procs=wmi.execquery("select * from win32_process")
flag=true
for each proc in procs
if strcomp(proc.name,proname)=0 then
flag=false
exit for
end if
next
set wmi=nothing
if flag then
Set WshShell = Wscript.CreateObject("Wscript.Shell")
WshShell.Run ("C:\b.exe") '输入你要执行的B.exe的路径
end if
wscript.sleep 50000000 '检测间隔时间,这里是50秒
loop
追问
我试验过了,不过你这个代码执行了以后,依然始终只打开b.exe ,并且无法关闭b.exe 如果关闭了,每隔50秒就自动又打开b.exe 了。 可能您没有理解我描述的意思。我仅仅是要在a.exe这个文件不存在时候执行一次b.exe。如果两个都不存在就没反应,当然我不会让b.exe不存在的。还有:我不是做病毒或者木马,无限启动,似乎目的不纯。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
如果仅实现这样的功能,批处理比vbs更简单方便。
追问
这个我知道,但是根据我得实际情况,我需要用vbs脚本
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
在C4X的基础上。
把copy d:\aaa\a.bat "C:\Documents and Settings\All Users\「开始」菜单\程序\启动\." (复制到启动,注意要加双引号)
改成
copy d:\aaa\a.bat "%SystemDrive%\Documents and Settings\All Users\「开始」菜单\程序\启动\." (复制到启动,注意要加双引号)
把copy d:\aaa\a.bat "C:\Documents and Settings\All Users\「开始」菜单\程序\启动\." (复制到启动,注意要加双引号)
改成
copy d:\aaa\a.bat "%SystemDrive%\Documents and Settings\All Users\「开始」菜单\程序\启动\." (复制到启动,注意要加双引号)
追问
加启动项干什么?你没看懂我问的什么问题吧。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询