如果使用vbscript路径未知,如何打开任何exe?

时间:2016-10-04 09:36:12

标签: vbscript

我可以使用以下方式打开EXE。但有时候,路径并不固定。如果用户更改安装路径,则以下代码将无用。

Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Exec("C:\Program Files (x86)\MyAppFolder\MyApp.exe") 
Set objShell = Nothing

我已按照建议推荐以下链接

1- Launch programs whose path contains spaces:但这建议使用路径或使用Run。在我的情况下,路径不固定。用户可以随时随地安装应用程序。我尝试在该链接中使用Run as,但我的应用程序没有从Run中打开,如示例所示

2- How to get program files environment setting from VBScript:此链接还包含一些获取程序文件路径的解决方案。但正如我上面所说,用户可以随意安装。

如果路径没有预先确定,请建议如何打开EXE。

1 个答案:

答案 0 :(得分:0)

您可以为搜索做点什么:

Dim Folder : Folder = "C:\program files"
Dim File : File = "WinRAR.exe"
Dim ws : Set ws = CreateObject("WScript.Shell")
Dim OutPut : OutPut = ws.ExpandEnvironmentStrings("%tmp%\Path_"& File &".txt")
wscript.echo OutPut
Command = "Cmd /c @echo off & for /f "& DblQuote("delims=")&" %a in ('Where.exe /r "& DblQuote(Folder) & " " & DblQuote(File) &"') do echo %a > "& DblQuote(OutPut) &""
wscript.echo Command
Call Run(Command,0,True)
wscript.echo "Finish"
ws.run DblQuote(OutPut)
'****************************************
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'****************************************
Function Run(StrCmd,Console,bWaitOnReturn)
    Dim ws,MyCmd,Result
    Set ws = CreateObject("wscript.Shell")
'A value of 0 to hide the MS-DOS console
    If Console = 0 Then
        MyCmd = "CMD /C " & StrCmd & ""
        Result = ws.run(MyCmd,Console,bWaitOnReturn)
        If Result = 0 Then
            'MsgBox "Success"
        Else
            MsgBox "An unknown error has occurred!",16,"An unknown error has occurred!"
        End If
    End If
'A value of 1 to show the MS-DOS console
    If Console = 1 Then
        MyCmd = "CMD /K " & StrCmd & ""
        Result = ws.run(MyCmd,Console,bWaitOnReturn)
        If Result = 0 Then
            'MsgBox "Success"
        Else
            MsgBox "An unknown error has occurred!",16,"An unknown error has occurred!"
        End If
    End If
    Run = Result
End Function
'****************************************