vbscript - 使用VBS运行带参数的程序

标签 vbscript

我是一个完全的 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"

如果我随后将其粘贴到开始 > 运行,它就会完全按照我的要求运行。

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

希望命令在 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”错误消息。

最佳答案

1.首先:我建议你养成使用这个引用功能的习惯 让您在这些情况下更容易引用变量!

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

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

因此,我下载了这个应用程序 (speedyfox.exe),并在我的 Windows 10(32 位)

上对其进行了测试

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

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
'-----------------------------------------

关于vbscript - 使用VBS运行带参数的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58423102/

相关文章:

c# - 如何检查是否已安装 IIS 6 Management Compatibility 功能?

vbscript - 在 VBScript 中检索 Cookie 和发送 Cookie 和发布变量

VBscript,它将使本地用户在下次登录时更改密码

ms-access - 将 VBScript 常规日期时间导入 MS Access 日期/时间列

VBScript 错误 5 尝试使用 'System.Security.Cryptography.SHA512Managed' 计算 sha512

batch-file - 并排启动两个资源管理器窗口

vbscript - 从文件中获取扩展属性

asp-classic - 如何使用服务器端 vbscript 获取图像宽度?经典的ASP

asp-classic - 经典 ASP/ session 结束重定向

arrays - 我的数组中有多少维或获取最后一个