8

This is what I have and can not get the bat to run, if I move the bat to a folder without spaces in the name it works. My problem is that the actual bat is in a folder with spaces, so I need this to work.

Set objShell = WScript.CreateObject("WScript.Shell")
objShell.Run("%comspec% /K C:\Program Files\ping.bat"), 1, True
Yazan
  • 6,074
  • 1
  • 19
  • 33
WACs
  • 101
  • 1
  • 1
  • 5

4 Answers4

8

You need to quote the file specification:

Run("%comspec% /K ""C:\Program Files\ping.bat""")
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
  • This did not work for me in Windows 10 1909 `WScript.CreateObject("Wscript.Shell").Run("%comspec% /K ""\\NETWORK\Test Folder""")` It opens CMD and can't find "string before space". This does work but I need something with spaces `WScript.CreateObject("Wscript.Shell").Run("\\NETWORK\TestFolder")` – DGM Oct 08 '21 at 14:33
3

I had a similar problem with a directory path in a VBScript that had empty spaces:

E.g.

The following did not work:

objShell.Run("C:\Program Files\NetBeans 8.0.2\bin\netbeans64.exe") 

I simply included two extra double quotations on either side of the path and it worked for me:

objShell.Run("""C:\Program Files\NetBeans 8.0.2\bin\netbeans64.exe""")
0

Try this one

Set objShell = WScript.CreateObject("WScript.Shell")
strCommand = chr(34)&"%comspec% /K C:\Program Files\ping.bat"&chr(34)
objShell.Run strCommand,1,True
Amol Chavan
  • 3,835
  • 1
  • 21
  • 32
-2

I know that this is an old question, but I found a fix that works for me.
It's the double quotes you need.
Try below:

objShell.Run("%comspec% /K " & """C:\Program Files\ping.bat""""), 1, True);
zx485
  • 28,498
  • 28
  • 50
  • 59
Tamati
  • 11
  • 1
  • 7