3

I'm a total vbs novice trying to perform the supposedly simple task of using a vbscript to run a single program (with parameters).

The path the to program is: C:\Program Files (x86)\SpeedyFox\speedyfox.exe and the parameter switch that must go with it is: /Firefox:C:\Program Files\Firefox\Data\profile

If I wrap both sections in quotes (due to the spaces in their paths) it gives the following combined single command:

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

If I then paste this into Start > Run it works exactly as I want.

I'm just trying to achieve the same thing from a vbs script instead of manually pasting into the Run box.

I do not want the command to run within a CMD console (as other questions on here have asked). All I am trying to do is to get "C:\Program Files (x86)\SpeedyFox\speedyfox.exe" "/Firefox:C:\Program Files\Firefox\Data\profile" to work with the shell.ShellExecute line of the script below.

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

but try as I might, I just keep getting WSH "Expected end of statement" error messages.

Pemhoo
  • 109
  • 5
  • 1
    Possible duplicate of [Run Command Line & Command From VBS](https://stackoverflow.com/questions/16087470/run-command-line-command-from-vbs) – user692942 Oct 17 '19 at 06:45

2 Answers2

2

1.First : I recommend you Make it a habit to use this quote function to make it easy for you to quote variables in these situations !

2. Second : You should use MsgBox or Wscript.echo in order to show and debug your variables easily !

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

So, I downloaded this application (speedyfox.exe) and i tested it on my Windows 10 (32bits)

So, here is what i tested and it works like a charm on my side :

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
'-----------------------------------------
Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • Thank you for reply. You went to a lot of trouble to download the program and test it, which was above and beyond, and very kind of you. Unfortunately I think my wording about me testing the command in CMD gave the impression that I wanted the final result to also be run within a console. This is not what was intended. I have therefore reworded my question for clarity. – Pemhoo Oct 17 '19 at 20:42
2

Sigh, reminds me of my vbscript days, now I use Ruby and it's just as simple as

´my_shell_command params´

However, back to your question: the shortest way to use ShellExecute is

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

See this documentation for explanation of the parameters.

EDIT: You have to pay attention at the quotes, they need to be passed to the shell also by using two quotes

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

peter
  • 41,770
  • 5
  • 64
  • 108
  • Here's what I wrote: `CreateObject("Shell.Application").ShellExecute "C:\Program Files (x86)\SpeedyFox\speedyfox.exe", " /Waterfox:C:\Program Files\Waterfox\Data\profile"` It does not work. If I remove the parameter part and just run the executable, the window opens, so I know that part works (but that bit worked in my original script anyway). But as soon as I add required the switch, it won't work. I know that actual switch command is correct because it works from **Start > Run** – Pemhoo Oct 18 '19 at 13:40