2013-10-03 27 views
0

我想启动一个exe文件,它是使用vbscript的特殊文件夹中的一个消息箱,但我什么也没得到。这是我的代码vbscript从特殊文件夹启动一个exe文件

option explicit 
dim shellobj,startup,objShell,objFolder,filesystemobj,installdir,installname 
set shellobj = wscript.createobject("wscript.shell") 
set filesystemobj = createobject("scripting.filesystemobject") 
Set objShell = CreateObject("Shell.Application") 
installdir = "%appdata%\Microsoft" 
installname = wscript.scriptname 
filesystemobj.copyfile wscript.scriptfullname,installdir & installname,true 
startup = shellobj.specialfolders("%appdata%\Microsoft") &"\msg.exe" 
if filesystemobj.fileexists(startup) Then 
    shellobj.run "wscript.exe //B " & chr(34) & startup & chr(34)  
    Wscript.Quit 
end if 

回答

0
  1. startup = shellobj.specialfolders("%appdata%\Microsoft") &"\msg.exe"

    SpecialFolders财产不展开环境变量。你需要的是ExpandEnvironmentStrings方法:

    shellobj.ExpandEnvironmentStrings("%APPDATA%\Microsoft") & "\msg.exe" 
    

    SpecialFolders属性必须像这样使用:

    shellobj.SpecialFolders("APPDATA") & "\Microsoft\msg.exe" 
    
  2. if filesystemobj.fileexists(startup) Then

    由于specialfolders("%appdata%\Microsoft")返回一个空字符串的startup值变为\msg.exe,这不存在。因此,Then分支中的语句永远不会执行。

  3. shellobj.run "wscript.exe //B " & chr(34) & startup & chr(34)

    而且即使 “\ msg.exe” 的存在,你不能用wscript.exe运行它。 wscript.exe是VBScripts的解释器之一。更改声明这一点:

    shellobj.Run Chr(34) & startup & Chr(34), 1, True 
    
+0

感谢你的回答。我用下面的代码解决了它:Dim WSHShell,AppData,objWshShl,objFSO Set objFSO = CreateObject(“Scripting.FileSystemObject”) Set WSHShell = WScript.CreateObject(“WScript.Shell”) Set objWshShl = WScript.CreateObject(“ WScript.Shell “) 应用程序数据= objWshShl.SpecialFolders(” 应用程序数据“)& ”\微软\ msg.exe“ 如果objFSO.FileExists(应用程序数据)然后 组oExec = WshShell.Exec(应用程序数据) WScript.Quit \t其他 \t WScript.echo“找不到文件” \t结束时,如果 – user2842276

+0

不客气。如果解决了您的问题,请考虑[接受答案](http://meta.stackexchange.com/a/5235)。另外,请避免在评论中张贴代码,因为它往往变得不可读。 –

相关问题