Executing an EXE inside a VBScript file that has spaces in the path.

I've done quite a bit of scripting but ran across something that baffles me.  Trying to execute a file in a directory with a spaces in the path would bomb.  I googled and found a thing that has me put [] inside to fix it but it didn't work.  Here is what i was trying to do, if there is no spaces in the path to the EXE including any directories, the script works fine.   Here is an example to a path for an EXE c:\winnt\system32\Ica PassThrough\pn.exe.  Here is the script code

Set wshShell = WScript.CreateObject ("WSCript.shell")
wshshell.run "c:\winnt\system32\Ica PassThrough\pn.exe", 6, True
set wshshell = nothing

Of course, this code would BOMB!  The script would return an error complaining it couldn't find the file.    Thanks to quick posting in the news://msnews.microsoft.public.windows.server.scripting newsgroup. 

Al Dunbar, world famous fellow MVP posted the answer to my prayers!

Your .run command is trying to run something called "C:\winnt\system32\ica" and pass it a parameter called "PassThrough\pn.exe". This is the same thing you would get if you typed the following at a command prompt:

    c:\winnt\system32\Ica PassThrough\pn.exe

If the name of the file to run is actually "c:\winnt\system32\Ica PassThrough\pn.exe", you would enter it at the command prompt as:

    "c:\winnt\system32\Ica PassThrough\pn.exe"

The double quotes in your code do not form part of the filename string being passed to the .run method, they are required to indicate a literal string.
You can prove this is the case by changing your script to look like this:

> Set wshShell = WScript.CreateObject ("WSCript.shell")
> wshshell.run c:\winnt\system32\Ica PassThrough\pn.exe, 6, True
> set wshshell = nothing


Which will throw a syntax error (for rather obvious reasons). You need to pass a string that actually contains the quoted filename, which can be done this way:

> wshshell.run """c:\winnt\system32\Ica PassThrough\pn.exe""", 6, True

Within a literal string, a single double-quote character is represented by two double-quote characters.   Notice the Three double quotes around the string,  This worked!  Thought I'd pass this tip along.

Published Sunday, March 14, 2004 12:13 AM by steve schofield

Comments

# re: Executing an EXE inside a VBScript file that has spaces in the path.

Tuesday, April 13, 2004 6:58 PM by Greg P.
Thanks! This solved the exact same problem for me! I was really hung up on this because I was testing for a file existance with:

objFSO.FileExists()

And that worked, WITH the spaces, yet running the file:

WshShell.Run()

Does not. I understand now why, but it's really inconsistant the way this works. In Real VB, you have an extra paramater in the shell function that is for the commandline parms. In this example, it's all one string... Way to go M$..

Anyway, thanks so much for posting this!
-Greg

# re: Executing an EXE inside a VBScript file that has spaces in the path.

Wednesday, April 14, 2004 5:47 AM by Steve Schofield
Glad this worked for ya. This is very flakey why you have to put 3 quotes on each side. The wierd i couldn't get to work with my scenerio is using the dos name.

# re: Executing an EXE inside a VBScript file that has spaces in the path.

Sunday, April 25, 2004 4:05 AM by Rob
Thanks for posting this info.
I had resorted to using the dir /x command to get the 8dot3 filename representation of each component in the path and substituting that into my script. It works but looks pretty ugly.

One additional wrinkle though. If you are passing additional parameters to the command to be run then you must remember to put the closing "" after the executable name and the final " after the command line parameters.

Yes it's obvious when you know.

# re: Executing an EXE inside a VBScript file that has spaces in the path.

Sunday, April 25, 2004 8:28 AM by Steve Schofield
Hi Rob, Do you have an example of what your talking about passing parameters to a script file that has spaces in the directory name.

# re: Executing an EXE inside a VBScript file that has spaces in the path.

Wednesday, May 05, 2004 6:53 PM by Ken Parmalee
Thanx! Works bueno!

# re: Executing an EXE inside a VBScript file that has spaces in the path.

Thursday, August 05, 2004 5:50 PM by Jeff Steward
Here's what I do to get around the quoting issue when passing parameters:

strCmd = "somepath\file.exe "
strCmd = strCmd & chr(34) & myParm & chr(34)

Using this method you can build rather complex paramaters yet easily troubleshoot them.

Cheers,

Jeff

# re: Executing an EXE inside a VBScript file that has spaces in the path.

Wednesday, November 29, 2006 6:07 PM by Steve J

In my situation, I had to run an import command... so basically, had to call an exe, in a folder with spaces, with a couple command line switches, and an INI file in a location with spaces. I ended up breaking apart the command line into sections. I was going to do the 8.3 Char names, but it looked to ugly for my tastes. So, here's what I did...

Example:

CmdLine1 = """C:\Program Files\Some Location\More Spaces\The.exe"

CmdLine2 = "/s /i"

CmdLine3 = """C:\Program Files\Location to Ini\More Spaces\The INI File Name.ini"""

CmdLine = CmdLine1 & " " CmdLine2 & CmdLine3

Wshell.Run CmdLine,1,True

Hope that helps someone...

-Steve J

# re: Executing an EXE inside a VBScript file that has spaces in the path.

Thursday, November 30, 2006 11:35 AM by steve schofield

Thanks Steve for posting this code.  This post has been a pretty popular article so hopefully your example can help someone.

# re: Executing an EXE inside a VBScript file that has spaces in the path.

Saturday, January 20, 2007 5:43 AM by John Varghese

Very help. I was searching for this.

Thanks,

John

# re: Executing an EXE inside a VBScript file that has spaces in the path.

Tuesday, January 23, 2007 10:50 PM by Dave R

Thanks!!!  

This was driving me crazy! You saved my butt!  

Dave

# re: Executing an EXE inside a VBScript file that has spaces in the path.

Wednesday, March 07, 2007 4:22 AM by Capricorn

Well thats nice but

if something like that happens:

for each f in files

.....

Wshell.Run <somepath>&f

.....

next

and in f - spaces exists, example "win dos.exe"

what to do than. How could I insert double " in f variable?????????????????????????????????????????

# Joannes Vermorel&#8217;s blog &raquo; Blog Archive &raquo;

Sunday, April 15, 2007 9:25 AM by Joannes Vermorel’s blog » Blog Archive »

PingBack from http://blog.vermorel.com/?p=59

# re: Executing an EXE inside a VBScript file that has spaces in the path.

Wednesday, April 18, 2007 4:04 AM by Mark

Thanks for the info. This was driving me nuts until I found your post.  Also, to pass switches I found you also have to do an &. Like this:

WshShell.Run """C:\Program Files\RivaTuner v2.0 Final Release\RivaTuner.exe""" & "/S", 1

Notice the "/S" switch.

# re: Executing an EXE inside a VBScript file that has spaces in the path.

Thursday, June 28, 2007 11:54 AM by miaminemo

Jeff -

Ive been stuck trying to pass parameters forever - finally got it today.

Thanks

I dont understand why the chr(34) works in place of a space but it does so I am happy.

# re: Executing an EXE inside a VBScript file that has spaces in the path.

Thursday, July 05, 2007 4:00 PM by Uri

We just had a related issue when using an ActiveX VBScript task with SQL Server 2000 DTS package.

The command we wanted to run was called log.vbs.

cmd = "\\unc\without\spaces\log \\unc\to\logfile\file.log ""Message to appear in the logfile"""

So here we have an argument that itself requires double-quotes, namely the contents of the log entry must be enclosed by double quotes.  The first weirdness was that the script had to have the .vbs extension before it would run using WshShell.Run, even though it ran fine on the command-line without it.  The solution ended up looking like so:

cmd="""\\unc\without\spaces\log.vbs"" \\unc\to\logfile\file.log "

msg = Now () & f.name & " file processed"

cmd = cmd & Chr(34) & msg & Chr(34)

WshShell.Run cmd, 1, True

Anyway, this thread helped us so we wanted to contribute for others.

# re: Executing an EXE inside a VBScript file that has spaces in the path.

Wednesday, August 15, 2007 5:35 PM by SJ

Why wouldn't this create the text file at the end.  It runs but somehow ignore ">> C:\Policies\ListAllGPOs.txt" part

   Set shell = CreateObject("WScript.Shell")

   shell.Run "C:\Policies\ListAllGPOs.wsf >> C:\Policies\ListAllGPOs.txt"

# re: Executing an EXE inside a VBScript file that has spaces in the path.

Tuesday, August 28, 2007 5:32 PM by Mike Ramsey

the >> does not work because you need %comspec% /c in there so it would be:

Set shell = CreateObject("WScript.Shell")

shell.Run "%comspec% /c C:\Policies\ListAllGPOs.wsf >> C:\Policies\ListAllGPOs.txt"

That should do it for ya.

-Mike

# re: Executing an EXE inside a VBScript file that has spaces in the path.

Tuesday, August 28, 2007 9:42 PM by automandc

Thanks, I ran into exactly this problem.  I knew it had something to do with the string literal, but I couldn't remember the syntax to escape a double quote in VBScript (three double-quotes? four?).  Your web page answered it when the MS Scripting documentation did not!

# ZenMojo.com &raquo; Blog Archive &raquo; Script to Install Macromedia Contribute Key

Pingback from  ZenMojo.com  &raquo; Blog Archive   &raquo; Script to Install Macromedia Contribute Key

# re: Executing an EXE inside a VBScript file that has spaces in the path.

Friday, September 21, 2007 2:51 AM by Ravin

Also try this..

Copy the path to the 'Path' variable in the Environment variables and run the .exe directly w/o giving any path.

For eg:

Set wshShell = WScript.CreateObject ("WSCript.shell")

> wshshell.run "pn.exe", 6, True

> set wshshell = nothing

and add the Path "c:\winnt\system32\Ica PassThrough\" to the environment variable

# re: Executing an EXE inside a VBScript file that has spaces in the path.

Wednesday, October 10, 2007 8:21 AM by prasad

I have to run one batch file.

and need to display its output.

Set wshShell = WScript.CreateObject("WSCript.shell")

strExec1 = "C:\Program Files\Operations"

strExec2 = "C:\Program Files\Operations\rap.bat"

set owshshell = wshshell.run(start&" "&strExec1&" "&strExec2)

Wscript.Echo(owshshell.StdOut.ReadAll())

What is the problem in this.

I tried with wshshell.exec also.

# FloSpace Blog &raquo; Blog Archive &raquo; Using FloPrompter with VBScript

Pingback from  FloSpace Blog  &raquo; Blog Archive   &raquo; Using FloPrompter with VBScript

# re: Executing an EXE inside a VBScript file that has spaces in the path.

Thursday, October 25, 2007 1:44 AM by Erwin

this helped me a lot! thanks! =)

# re: Executing an EXE inside a VBScript file that has spaces in the path.

Wednesday, October 31, 2007 10:25 AM by Oz

I had exactly the same problem running an executable and although the fix seems odd, it works fine!

# re: Executing an EXE inside a VBScript file that has spaces in the path.

Friday, November 02, 2007 1:56 AM by Hemant

cmd = """c:\program files\winrar\winrar.exe"" a " & ZIPPATH & BackupFileName & " " &  PATHNAME & FILE & "*.xls"

where my FILE = weekly stylewise designwise2007-11-02

I am using this in VB script but got an error

!   D:\Hemant\XP_SMTP_SendMail20071102.rar: Cannot open D:\Hemant\weekly

!   The system cannot find the file specified.

!   D:\Hemant\XP_SMTP_SendMail20071102.rar: Cannot open storewise

!   The system cannot find the file specified.

!   D:\Hemant\XP_SMTP_SendMail20071102.rar: Cannot open stylewise

!   The system cannot find the file specified.

!   D:\Hemant\XP_SMTP_SendMail20071102.rar: Cannot open designwise2007-11-

!   The system cannot find the file specified.

Can anybody help me out with this i.e. how to zip a file having spaces in the name or how to remove the spaces form the file name?