Question

I would like to be able to call the following cmd command from within a vbs script:

cmd Client\setupclient.exe /q /targetdir "c:\program files\Microsoft CRM"

I came up with the following vbs script:

Set oShell = WScript.CreateObject ("WScript.Shell") 
oShell.Run "cmd /c Client\setupclient.exe /q /targetdir c:\program files\Microsoft CRM", 1, true

As far as I am concerned, this would work properly if the targetdir had no spaces, e.g c:\bla. Then the app would be installed in that particular folder.

The obvious question is, how can I define the targetdir with spaces as the path location.
I tried to surround it with ' ' but that didn't work for me. Any suggestions?

Cheers chris

Was it helpful?

Solution

Paths with spaces are typically enclosed in quote characters ("). In VBScript, to insert a quote character into a string you use double quotes (""). So, your code should look like this:

oShell.Run "cmd /c Client\setupclient.exe /q /targetdir ""c:\program files\Microsoft CRM""", 1, true

Also, I'm not sure if cmd /c is actually needed here, so it might work this way as well:

oShell.Run "Client\setupclient.exe /q /targetdir ""c:\program files\Microsoft CRM""", 1, true

OTHER TIPS

I ended up with

AMPath = "E:\Program Files (x86)\Dropbox\Client\Dropbox.exe"
If FileSyst.Fileexists(AMPath) Then 
 AMPath = chr(34) & AMPath & chr(34)
OBJ_Shell.run (AMPath)
End If 

1、If your OS supports 8.3 filename,you can try short filename:

cd c:\
dir /x
2017/04/17  20:53    <DIR>          PROGRA~1     Program Files
2017/04/18  03:40    <DIR>          PROGRA~2     Program Files (x86)

Then repalce C:\Program Files\ with PROGRA~1.

2、use two double-quotes within full path.

WScript.CreateObject("WScript.Shell").Run """C:\Program Files\DirName\FileName.exe"" /option1 value1 /option2 vaule2 argv3"

This is not exactly the problem described, in that the called program rather than a parameter contains a space. Googling "whshell.run does not work if filename contains blanks" got me here.

When the called program contains a space in its name, it needs to be triple quoted. (The starting and ending quotes define a string with blanks and the enclosed double quotes are mapped to single quotes within that string.) There are two working examples. The first uses triple quotes. The second effectively removes the blanks from the name. The non-working examples show what not to do (and what I tried first.)

' Drive D:\Program Files\Batch\Monitor.bat with no associated command window

Set WshShell = CreateObject("WScript.Shell")

' These methods work: (Select one)
  Return = WshShell.Run("""D:\Program Files\Batch\Monitor.bat""", 0)
' Return = WshShell.Run("D:\.D-DISK\Monitor.bat", 0)
' Note: Here "D:\.D-DISK\Monitor.bat" is a symbolic link to
'       "D:\Program Files\Batch\Monitor.bat"

' The following methods fail because of the space in the filename.
' WshShell.Run( chr(34) & D:\Program Files\Batch\Monitor.bat & Chr(34), 0 )
' Return = WshShell.Run("D:\Program Files\Batch\Monitor.bat", 0)
' Return = WshShell.Run(""D:\Program Files\Batch\Monitor.bat"", 0)

Set WshShell = Nothing
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top