Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


How to open an .exe file using vbscript that has spaces in the name?

Posted on 2014-12-04
Medium Priority
Last Modified: 2014-12-04
All with the following I can get my .exe file opened up except when there are spaces in the name.
Please note the following below:

Because the program PDF Parser.exe has a space it never seems to find the file to open and run it.
How do I change the syntax below to make this work?  

I have tried several samples shown here:  


Is there another I can try?  
Does someone have an .exe file to try and open - perhaps rename it with a space and try the following code below:

sExecutable = "C:\Users\slecompte\Documents\SMW_Transfer\PDF Parser\PDF Parser.exe"

Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )

If LikeFunction(sExecutable) = False Then
  'this line works when a file has no spaces.
  objShell.Run "%comspec% /c start " & sExecutable, 0, True
  'this line doesn't work no matter what I try:
  Set objShell = WScript.CreateObject("WScript.Shell")
  strCommand = "%comspec% /c start "& chr(34) & sExecutable & chr(34)
  'sExecutable = chr(34) & chr(34) & chr(34) & sExecutable & chr(34) & chr(34) & chr(34)
  'MsgBox sExecutable
'"""C:\Program Files\abc_edf\cde\qw.exe""",1,true
  objShell.Run strCommand , 1, True
  'MsgBox strCommand
End if
Set objShell = Nothing

Public Function LikeFunction(sValue)

  Dim bValue
  Dim i
  For i = 1 to len(sValue)
     If mid(sValue, i, 1) = " " Then
       bValue = True
       Exit For
     End If
  LikeFunction = bValue

End Function

Open in new window

Question by:stephenlecomptejr
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 58

Expert Comment

by:Bill Prew
ID: 40481495
This should do it.

Since you are using the START command with a quoted file name, you need to include the title paramater.  So the effect you are going for is:

start /c "" "C:\Users\slecompte\Documents\SMW_Transfer\PDF Parser\PDF Parser.exe"

Open in new window

which you can code as:

  strCommand = "%comspec% /c start " & chr(34) chr(34) & " " & chr(34) & sExecutable & chr(34)
  objShell.Run strCommand , 1, True

Open in new window

LVL 85

Accepted Solution

oBdA earned 2000 total points
ID: 40481532
You don't need cmd.exe when starting an exe file. This should do the job just fine:
sExecutable = "C:\Users\slecompte\Documents\SMW_Transfer\PDF Parser\PDF Parser.exe"
Dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
strCommand = chr(34) & sExecutable & chr(34) &
objShell.Run strCommand , 1, True
Set objShell = Nothing

Open in new window


Author Comment

ID: 40481600
oBdA,  Your code worked even though - I swear I tried that exact code.  Of course I had to take the & at the end off.
Bill, I tried your code but it gave an error - expected end of statement - Line 13 - char: 48
which was this line:
  strCommand = "%comspec% /c start " & chr(34) chr(34) & " " & chr(34) & sExecutable & chr(34)
LVL 58

Expert Comment

by:Bill Prew
ID: 40481865
Whoops, forest, tress, etc.  I was focused in on the file you were executing, etc.  Glad you have a working solution.


Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question