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

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

Posted on 2014-12-04
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
  • 2
LVL 53

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 84

Accepted Solution

oBdA earned 500 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 53

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
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…

856 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