欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

运行同一目录下的可执行程序的VBS代码

程序员文章站 2022-08-27 15:17:32
复制代码 代码如下: set objshell = createobject("wscript.shell") strcurrentdir = objshell.curre...
复制代码 代码如下:

set objshell = createobject("wscript.shell")
strcurrentdir = objshell.currentdirectory
set fso = createobject("scripting.filesystemobject")
set f=fso.getfolder(strcurrentdir)
for each i in f.files
if lcase(right(i,4)) = ".exe" then objshell.run i,,true
next


获取脚本所在目录还可以:
复制代码 代码如下:

set fso = createobject("scripting.filesystemobject")
fso.getparentfoldername(wscript.scriptfullname)

一时想不到更好的办法判断文件是不是.exe了…… 就这样了。
复制代码 代码如下:

option explicit

dim ofs : set ofs = createobject( "scripting.filesystemobject" )
dim ows : set ows = createobject( "wscript.shell" )

wscript.echo "script:"
wscript.echo "scriptfullname:", wscript.scriptfullname
wscript.echo "scriptfullpath:", ofs.getparentfoldername( wscript.scriptfullname )
wscript.echo "current:"
wscript.echo "fs:", ofs.getabsolutepathname( ".\" )
wscript.echo "ws:", ows.currentdirectory

-------- output -------------

c:\wis\_vbs\0506\dev\forum
cscript curdir.vbs
script:
scriptfullname: c:\wis\_vbs\0506\dev\forum\curdir.vbs
scriptfullpath: c:\wis\_vbs\0506\dev\forum
current:
fs: c:\wis\_vbs\0506\dev\forum
ws: c:\wis\_vbs\0506\dev\forum

c:\wis\_vbs\0506\dev\forum
cd ..

c:\wis\_vbs\0506\dev
cscript forum\curdir.vbs
script:
scriptfullname: c:\wis\_vbs\0506\dev\forum\curdir.vbs
scriptfullpath: c:\wis\_vbs\0506\dev\forum
current:
fs: c:\wis\_vbs\0506\dev
ws: c:\wis\_vbs\0506\dev

参考资料: