Solved

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

Posted on 2014-12-04
4
285 Views
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:  

http://stackoverflow.com/questions/5156903/vbscript-problem
http://stackoverflow.com/questions/11628413/vbscript-opening-a-file-parameter-that-has-spaces-in-its-name

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
Else
  '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
  next
  LikeFunction = bValue

End Function

Open in new window

0
Comment
Question by:stephenlecomptejr
[X]
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
4 Comments
 
LVL 55

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


~bp
0
 
LVL 85

Accepted Solution

by:
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

0
 
LVL 1

Author Comment

by:stephenlecomptejr
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)
0
 
LVL 55

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.

~bp
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

729 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