Solved

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

Posted on 2014-12-04
4
255 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
  • 2
4 Comments
 
LVL 51

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 83

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 51

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
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…
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…

746 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now