2013-10-03 26 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)。另外,請避免在評論中張貼代碼,因爲它往往變得不可讀。 –

相關問題