vbs cmd path space

南楼画角 提交于 2019-11-29 16:15:40

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

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!