Vbscript 使用VBS运行带有参数的程序
我完全是一个vbs新手,试图执行使用vbscript运行单个程序(带参数)的简单任务 程序的路径为: C:\ProgramFiles(x86)\SpeedyFox\SpeedyFox.exe 必须随附的参数开关为: /Firefox:C:\Program Files\Firefox\Data\profile 如果我将两个部分都用引号括起来(由于它们的路径中有空格),则会给出以下组合的单个命令:Vbscript 使用VBS运行带有参数的程序,vbscript,Vbscript,我完全是一个vbs新手,试图执行使用vbscript运行单个程序(带参数)的简单任务 程序的路径为: C:\ProgramFiles(x86)\SpeedyFox\SpeedyFox.exe 必须随附的参数开关为: /Firefox:C:\Program Files\Firefox\Data\profile 如果我将两个部分都用引号括起来(由于它们的路径中有空格),则会给出以下组合的单个命令: “C:\ProgramFiles(x86)\SpeedyFox\SpeedyFox.exe”“/Fir
“C:\ProgramFiles(x86)\SpeedyFox\SpeedyFox.exe”“/Firefox:C:\ProgramFiles\Firefox\Data\profile”
如果我随后将其粘贴到Start>Run中,它将完全按照我的要求工作
我只是想从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.首先:我建议您养成使用此quote函数的习惯
使您在这些情况下引用变量变得容易
2。第二:您应该使用MsgBox
或Wscript.echo
来显示
并且可以轻松地调试变量
Wscript.echo DblQuote("Hello World !")
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
因此,我下载了这个应用程序(speedyfox.exe),并在我的Windows10(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时代,现在我使用Ruby,它就像
´my_shell_command params´
但是,回到您的问题:使用ShellExecute的最短方法是
CreateObject("Shell.Application").ShellExecute "application", "parameters", "dir", "verb", window
有关参数的说明,请参阅文档
编辑:
您必须注意引号,它们也需要使用两个引号传递给shell
例如
CreateObject(“Shell.Application”).Shell执行“C:\Program Files(x86)\SpeedyFox\SpeedyFox.exe”,“/Waterfox:C:\Program Files\WaterBox\Data\profile”“”
感谢您的回复,可能重复。你费了很大的劲才下载并测试了这个程序,你真是太好了。不幸的是,我认为我在CMD中测试命令的措辞给人的印象是,我希望最终结果也能在控制台中运行。这并非本意。因此,为了清楚起见,我重新编写了我的问题。以下是我写的:CreateObject(“Shell.Application”).ShellExecute“C:\ProgramFiles(x86)\SpeedyFox\SpeedyFox.exe”,“/waterbox:C:\ProgramFiles\waterbox\Data\profile”
它不起作用。如果我删除参数部分并运行可执行文件,窗口就会打开,因此我知道该部分可以工作(但该部分在我的原始脚本中仍然有效)。但只要我加上这个开关,它就不工作了。我知道实际的switch命令是正确的,因为它从Start>Run开始工作