【问题标题】:Use VBS to run a program with parameters使用VBS运行带参数的程序
【发布时间】:2019-10-16 23:59:13
【问题描述】:

我是一个完整的 vbs 新手,试图执行使用 vbscript 运行单个程序(带参数)的所谓简单任务。

程序的路径是: C:\Program Files (x86)\SpeedyFox\speedyfox.exe 并且必须与之配套的参数开关是: /Firefox:C:\Program Files\Firefox\Data\profile

如果我将两个部分都用引号引起来(由于它们的路径中有空格),它会给出以下组合的单个命令:

"C:\Program Files (x86)\SpeedyFox\speedyfox.exe" "/Firefox:C:\Program Files\Firefox\Data\profile"

如果我将其粘贴到 Start > Run 中,它会完全按照我的意愿工作。

我只是想通过 vbs 脚本实现相同的功能,而不是手动粘贴到 运行 框中。

我确实不希望命令在 CMD 控制台中运行(正如此处的其他问题所问的那样)。我要做的就是让"C:\Program Files (x86)\SpeedyFox\speedyfox.exe" "/Firefox:C:\Program Files\Firefox\Data\profile" 使用下面脚本的 shell.ShellExecute 行。

Set objShell = Wscript.CreateObject ("Wscript.shell")
set shell=CreateObject("Shell.Application")
shell.ShellExecute  ** WHAT DO I PUT HERE? **
set shell=nothing

但尽我所能,我只是不断收到 WSH“Expected end of statement”错误消息。

【问题讨论】:

标签: vbscript


【解决方案1】:

1.First:我建议你养成使用这个报价功能的习惯 方便您在这些情况下引用变量!

2.第二:你应该使用MsgBox或Wscript.echo来显示 并轻松调试您的变量!

Wscript.echo DblQuote("Hello World !")
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function

所以,我下载了这个应用程序 (speedyfox.exe),并在我的 Windows 10 (32bits)

上对其进行了测试

所以,这是我测试过的,它对我来说就像一个魅力:

Option Explicit
Dim objShell,MyCommand,strProgramFiles,SpeedyFoxfile,Title
Title = "Execute SpeedyFox in Commandline"
Set objShell = CreateObject("Shell.Application")
strProgramFiles = GetProgramFilesPath()
SpeedyFoxfile = strProgramFiles & "\SpeedyFox\speedyfox.exe"
MsgBox "Without Double Quotes" & vbCrlf & SpeedyFoxfile,vbInformation,Title
MsgBox "With Double Quotes" & vbCrlf & DblQuote(SpeedyFoxfile),vbInformation,Title
MyCommand = "CD /D "& DblQuote(strProgramFiles &"\SpeedyFox\") &"&"& DblQuote(SpeedyFoxfile) & " " & DblQuote("/Firefox:default") & " " & DblQuote("/Chrome:Default")
MsgBox MyCommand,vbInformation,Title
Call Execute(MyCommand) 
'-----------------------------------------
Function Execute(StrCmd)
   Dim ws,MyCmd,Result
   Set ws = CreateObject("wscript.Shell")
   MyCmd = "CMD /K " & StrCmd & ""'
   Result = ws.run(MyCmd,1,True)
   Execute = Result
End Function
'-----------------------------------------
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'-----------------------------------------
Function GetProgramFilesPath()
Dim ws,OsType,strProgramFiles
Set ws = createObject("WScript.Shell")  
OsType = ws.RegRead("HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\PROCESSOR_ARCHITECTURE")    
    If OsType = "x86" then    
        strProgramFiles = ws.ExpandEnvironmentStrings("%PROGRAMFILES%")  
    elseif OsType = "AMD64" then   
        strProgramFiles = ws.ExpandEnvironmentStrings("%PROGRAMFILES(x86)%")  
    end if 
GetProgramFilesPath = strProgramFiles
End Function
'-----------------------------------------

【讨论】:

  • 感谢您的回复。你费了很多力气去下载程序并测试它,这超出了你的要求,你非常好。不幸的是,我认为我在 CMD 中测试命令的措辞给人的印象是我希望最终结果也能在控制台中运行。这不是本意。因此,为了清楚起见,我重新措辞了我的问题。
【解决方案2】:

叹息,让我想起了我的 vbscript 日子,现在我使用 Ruby,它就像简单一样

´my_shell_command params´

但是,回到你的问题:使用 ShellExecute 的最短方法是

CreateObject("Shell.Application").ShellExecute "application", "parameters", "dir", "verb", window

有关参数的说明,请参阅this 文档。

编辑: 您必须注意引号,它们也需要使用两个引号传递给 shell

例如CreateObject("Shell.Application").ShellExecute "C:\Program Files (x86)\SpeedyFox\speedyfox.exe", """/Waterfox:C:\Program Files\Waterfox\Data\profile"""

【讨论】:

  • 这是我写的:CreateObject("Shell.Application").ShellExecute "C:\Program Files (x86)\SpeedyFox\speedyfox.exe", " /Waterfox:C:\Program Files\Waterfox\Data\profile" 它不起作用。如果我删除参数部分并只运行可执行文件,则会打开窗口,所以我知道该部分有效(但无论如何,该部分在我的原始脚本中有效)。但是一旦我添加了所需的开关,它就不起作用了。我知道实际的 switch 命令是正确的,因为它从 Start > Run 开始工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-17
  • 2011-04-13
  • 2011-09-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多