WshShell.Run "path with spaces" fails
Author Message
WshShell.Run "path with spaces" fails

When I run a path\pgm from WSH Shell object, it
will always fail when there is a space in the path.

How can I run a local exe where the path has a space in it?

Example:

' NFG
Dim szTemp
szTemp   = "c:\program files\myappfolder\myapp.exe"
WshShell = Wscript.CreateObject(Wscript.Shell)
WshShell.Run szTemp

Where as...

Dim szTemp
szTemp   = "c:\nospace_foobar\myappfolder\myapp.exe"
WshShell = Wscript.CreateObject(Wscript.Shell)
WshShell.Run szTemp

runs o.k.

Also, unlike in my examples above, in my real-world application
(script) I have no way of knowing the path until runtime. What I am
doing is fetching the path from HKEY_LOCAL_MACHINE\software\....\path.

- allen

Fri, 28 Jun 2002 03:00:00 GMT
WshShell.Run "path with spaces" fails
Or you can write \program files\ as \progra~1\ and that will work also.

-Mark

Quote:
> You have to double quote the path, which takes a workaround since the
double
> quote has special meaning in WSH:  What I usually us is:

>     szTemp   = """c:\program files\myappfolder\myapp.exe"""

> Or you could use:

>     szTemp   = chr(34) & "c:\program files\myappfolder\myapp.exe" &
chr(34)

> There are other ways to do this, but these are what I use.  The first one
is
> more compact, while the 2nd one is easier to interpret.

> --
> Bill James

> Free Win9x VBScript Utilities

> > When I run a path\pgm from WSH Shell object, it
> > will always fail when there is a space in the path.

> > How can I run a local exe where the path has a space in it?

> > Example:

> > ' NFG
> >    Dim szTemp
> >    szTemp   = "c:\program files\myappfolder\myapp.exe"
> >    WshShell = Wscript.CreateObject(Wscript.Shell)
> >    WshShell.Run szTemp

> > Where as...

> >    Dim szTemp
> >    szTemp   = "c:\nospace_foobar\myappfolder\myapp.exe"
> >    WshShell = Wscript.CreateObject(Wscript.Shell)
> >    WshShell.Run szTemp

> > runs o.k.

> > Also, unlike in my examples above, in my real-world application
> > (script) I have no way of knowing the path until runtime. What I am
> > doing is fetching the path from HKEY_LOCAL_MACHINE\software\....\path.

> > - allen

Fri, 28 Jun 2002 03:00:00 GMT
WshShell.Run "path with spaces" fails
You have to double quote the path, which takes a workaround since the double
quote has special meaning in WSH:  What I usually us is:

szTemp   = """c:\program files\myappfolder\myapp.exe"""

Or you could use:

szTemp   = chr(34) & "c:\program files\myappfolder\myapp.exe" & chr(34)

There are other ways to do this, but these are what I use.  The first one is
more compact, while the 2nd one is easier to interpret.

--
Bill James

Free Win9x VBScript Utilities

Quote:
> When I run a path\pgm from WSH Shell object, it
> will always fail when there is a space in the path.

> How can I run a local exe where the path has a space in it?

> Example:

> ' NFG
>    Dim szTemp
>    szTemp   = "c:\program files\myappfolder\myapp.exe"
>    WshShell = Wscript.CreateObject(Wscript.Shell)
>    WshShell.Run szTemp

> Where as...

>    Dim szTemp
>    szTemp   = "c:\nospace_foobar\myappfolder\myapp.exe"
>    WshShell = Wscript.CreateObject(Wscript.Shell)
>    WshShell.Run szTemp

> runs o.k.

> Also, unlike in my examples above, in my real-world application
> (script) I have no way of knowing the path until runtime. What I am
> doing is fetching the path from HKEY_LOCAL_MACHINE\software\....\path.

> - allen

Fri, 28 Jun 2002 03:00:00 GMT
WshShell.Run "path with spaces" fails
You are making an assumption that Program Files will be progra~1, but that
is not necessarily true.

Bill James

Quote:
> Or you can write \program files\ as \progra~1\ and that will work also.

> -Mark

> > You have to double quote the path, which takes a workaround since the
> double
> > quote has special meaning in WSH:  What I usually us is:

> >     szTemp   = """c:\program files\myappfolder\myapp.exe"""

> > Or you could use:

> >     szTemp   = chr(34) & "c:\program files\myappfolder\myapp.exe" &
> chr(34)

> > There are other ways to do this, but these are what I use.  The first
one
> is
> > more compact, while the 2nd one is easier to interpret.

> > --
> > Bill James

> > Free Win9x VBScript Utilities

> > > When I run a path\pgm from WSH Shell object, it
> > > will always fail when there is a space in the path.

> > > How can I run a local exe where the path has a space in it?

> > > Example:

> > > ' NFG
> > >    Dim szTemp
> > >    szTemp   = "c:\program files\myappfolder\myapp.exe"
> > >    WshShell = Wscript.CreateObject(Wscript.Shell)
> > >    WshShell.Run szTemp

> > > Where as...

> > >    Dim szTemp
> > >    szTemp   = "c:\nospace_foobar\myappfolder\myapp.exe"
> > >    WshShell = Wscript.CreateObject(Wscript.Shell)
> > >    WshShell.Run szTemp

> > > runs o.k.

> > > Also, unlike in my examples above, in my real-world application
> > > (script) I have no way of knowing the path until runtime. What I am
> > > doing is fetching the path from HKEY_LOCAL_MACHINE\software\....\path.

> > > - allen

Fri, 28 Jun 2002 03:00:00 GMT
WshShell.Run "path with spaces" fails

You could use fso.GetFile(long path) and use the ShortPath property of the File object returned.

--
Michael Harris

You are making an assumption that Program Files will be progra~1, but that
is not necessarily true.

Bill James

> Or you can write \program files\ as \progra~1\ and that will work also.
>
> -Mark
>

> > You have to double quote the path, which takes a workaround since the
> double
> > quote has special meaning in WSH:  What I usually us is:
> >
> >     szTemp   = """c:\program files\myappfolder\myapp.exe"""
> >
> > Or you could use:
> >
> >     szTemp   = chr(34) & "c:\program files\myappfolder\myapp.exe" &
> chr(34)
> >
> > There are other ways to do this, but these are what I use.  The first
one
> is
> > more compact, while the 2nd one is easier to interpret.
> >
> > --
> > Bill James
> >
> > Free Win9x VBScript Utilities
> >

> > > When I run a path\pgm from WSH Shell object, it
> > > will always fail when there is a space in the path.
> > >
> > > How can I run a local exe where the path has a space in it?
> > >
> > > Example:
> > >
> > > ' NFG
> > >    Dim szTemp
> > >    szTemp   = "c:\program files\myappfolder\myapp.exe"
> > >    WshShell = Wscript.CreateObject(Wscript.Shell)
> > >    WshShell.Run szTemp
> > >
> > > Where as...
> > >
> > >    Dim szTemp
> > >    szTemp   = "c:\nospace_foobar\myappfolder\myapp.exe"
> > >    WshShell = Wscript.CreateObject(Wscript.Shell)
> > >    WshShell.Run szTemp
> > >
> > > runs o.k.
> > >
> > > Also, unlike in my examples above, in my real-world application
> > > (script) I have no way of knowing the path until runtime. What I am
> > > doing is fetching the path from HKEY_LOCAL_MACHINE\software\....\path.
> > >
> > > - allen
> > >
> > >
> >
> >
>
>

Fri, 28 Jun 2002 03:00:00 GMT
WshShell.Run "path with spaces" fails

Hi,
Have you tried """c:\program files\myappfolder\myapp.exe""" ???
This will embedding cotes.

Best Regards.
PIERRE

Quote:
> -----Message d'origine-----

> Publi : lundi 10 janvier 2000 20:22
> Publi dans : wsh
> Conversation : WshShell.Run "path with spaces" fails
> Objet : WshShell.Run "path with spaces" fails

> When I run a path\pgm from WSH Shell object, it
> will always fail when there is a space in the path.

> How can I run a local exe where the path has a space in it?

> Example:

> ' NFG
>    Dim szTemp
>    szTemp   = "c:\program files\myappfolder\myapp.exe"
>    WshShell = Wscript.CreateObject(Wscript.Shell)
>    WshShell.Run szTemp

> Where as...

>    Dim szTemp
>    szTemp   = "c:\nospace_foobar\myappfolder\myapp.exe"
>    WshShell = Wscript.CreateObject(Wscript.Shell)
>    WshShell.Run szTemp

> runs o.k.

> Also, unlike in my examples above, in my real-world application
> (script) I have no way of knowing the path until runtime. What I am
> doing is fetching the path from HKEY_LOCAL_MACHINE\software\....\path.

> - allen

Sat, 29 Jun 2002 03:00:00 GMT
WshShell.Run "path with spaces" fails
You are right, only 99.99999999999999% of the time it will be. My error.

Quote:
> You are making an assumption that Program Files will be progra~1, but that
> is not necessarily true.

> Bill James

> > Or you can write \program files\ as \progra~1\ and that will work also.

> > -Mark

> > > You have to double quote the path, which takes a workaround since the
> > double
> > > quote has special meaning in WSH:  What I usually us is:

> > >     szTemp   = """c:\program files\myappfolder\myapp.exe"""

> > > Or you could use:

> > >     szTemp   = chr(34) & "c:\program files\myappfolder\myapp.exe" &
> > chr(34)

> > > There are other ways to do this, but these are what I use.  The first
> one
> > is
> > > more compact, while the 2nd one is easier to interpret.

> > > --
> > > Bill James

> > > Free Win9x VBScript Utilities

> > > > When I run a path\pgm from WSH Shell object, it
> > > > will always fail when there is a space in the path.

> > > > How can I run a local exe where the path has a space in it?

> > > > Example:

> > > > ' NFG
> > > >    Dim szTemp
> > > >    szTemp   = "c:\program files\myappfolder\myapp.exe"
> > > >    WshShell = Wscript.CreateObject(Wscript.Shell)
> > > >    WshShell.Run szTemp

> > > > Where as...

> > > >    Dim szTemp
> > > >    szTemp   = "c:\nospace_foobar\myappfolder\myapp.exe"
> > > >    WshShell = Wscript.CreateObject(Wscript.Shell)
> > > >    WshShell.Run szTemp

> > > > runs o.k.

> > > > Also, unlike in my examples above, in my real-world application
> > > > (script) I have no way of knowing the path until runtime. What I am
> > > > doing is fetching the path from

HKEY_LOCAL_MACHINE\software\....\path.

- Show quoted text -

Quote:

> > > > - allen

Sat, 29 Jun 2002 03:00:00 GMT
WshShell.Run "path with spaces" fails
The problem with the "adding qoutes" solution is that it assumes I have the
literal string.
Since I am getting the path from the system registery I have a variable like
in the following pseudo-code:

if szPath <> "" then
WshShell.Run szPath,1,FALSE
end if

Using Mike Harris's solution the pseudo-code is

if szPath <> "" then
Set  fso   = Wscript.CreateObject("Scripting.FileSystemObject")
Set  fo     = fso.GetFile(szPath)
szPath     = fo.ShortPath
WshShell.Run szPath,1,FALSE
end if

This works!!!

Dank 'u people,
- allen

Quote:
> When I run a path\pgm from WSH Shell object, it
> will always fail when there is a space in the path.

> How can I run a local exe where the path has a space in it?

> Example:

> ' NFG
>    Dim szTemp
>    szTemp   = "c:\program files\myappfolder\myapp.exe"
>    WshShell = Wscript.CreateObject(Wscript.Shell)
>    WshShell.Run szTemp

> Where as...

>    Dim szTemp
>    szTemp   = "c:\nospace_foobar\myappfolder\myapp.exe"
>    WshShell = Wscript.CreateObject(Wscript.Shell)
>    WshShell.Run szTemp

> runs o.k.

> Also, unlike in my examples above, in my real-world application
> (script) I have no way of knowing the path until runtime. What I am
> doing is fetching the path from HKEY_LOCAL_MACHINE\software\....\path.

> - allen

Sat, 29 Jun 2002 03:00:00 GMT
WshShell.Run "path with spaces" fails
I don't know if the percentage is quite that high, but Murphy's law says it
will always be the Boss who experiences the exception. <g>

--
Bill James

Free Win9x VBScript Utilities

Quote:
> You are right, only 99.99999999999999% of the time it will be. My error.

> > You are making an assumption that Program Files will be progra~1, but
that
> > is not necessarily true.

> > Bill James

> > > Or you can write \program files\ as \progra~1\ and that will work
also.

> > > -Mark

> > > > You have to double quote the path, which takes a workaround since
the
> > > double
> > > > quote has special meaning in WSH:  What I usually us is:

> > > >     szTemp   = """c:\program files\myappfolder\myapp.exe"""

> > > > Or you could use:

> > > >     szTemp   = chr(34) & "c:\program files\myappfolder\myapp.exe" &
> > > chr(34)

> > > > There are other ways to do this, but these are what I use.  The
first
> > one
> > > is
> > > > more compact, while the 2nd one is easier to interpret.

> > > > --
> > > > Bill James

> > > > Free Win9x VBScript Utilities

> > > > > When I run a path\pgm from WSH Shell object, it
> > > > > will always fail when there is a space in the path.

> > > > > How can I run a local exe where the path has a space in it?

> > > > > Example:

> > > > > ' NFG
> > > > >    Dim szTemp
> > > > >    szTemp   = "c:\program files\myappfolder\myapp.exe"
> > > > >    WshShell = Wscript.CreateObject(Wscript.Shell)
> > > > >    WshShell.Run szTemp

> > > > > Where as...

> > > > >    Dim szTemp
> > > > >    szTemp   = "c:\nospace_foobar\myappfolder\myapp.exe"
> > > > >    WshShell = Wscript.CreateObject(Wscript.Shell)
> > > > >    WshShell.Run szTemp

> > > > > runs o.k.

> > > > > Also, unlike in my examples above, in my real-world application
> > > > > (script) I have no way of knowing the path until runtime. What I
am
> > > > > doing is fetching the path from
> HKEY_LOCAL_MACHINE\software\....\path.

> > > > > - allen

Sat, 29 Jun 2002 03:00:00 GMT
WshShell.Run "path with spaces" fails
Both you guys are assuming I have the literal string or knowledge before
runtime of what the path is. Mike Harris had the best solution (use
FileSystemObject to get the FileObject, then use the FileObject to get the
ShortPath), although I certainly appreciate the knowledge about the qoutes.
I thought I read about that somewhere, and in fact while debugging I tried
hard coding a path in qoutes, but I did it incorrectly.

I also thought I read a solution where you could build and run a shortcut on
the fly. I found all kinds of info on builinding one, but not on launching
it.

Anyhoo, thanks for the feedback!!!

- allen

Quote:
> I don't know if the percentage is quite that high, but Murphy's law says
it
> will always be the Boss who experiences the exception. <g>

> --
> Bill James

> Free Win9x VBScript Utilities

> > You are right, only 99.99999999999999% of the time it will be. My error.

> > > You are making an assumption that Program Files will be progra~1, but
> that
> > > is not necessarily true.

> > > Bill James

> > > > Or you can write \program files\ as \progra~1\ and that will work
> also.

> > > > -Mark

> > > > > You have to double quote the path, which takes a workaround since
> the
> > > > double
> > > > > quote has special meaning in WSH:  What I usually us is:

> > > > >     szTemp   = """c:\program files\myappfolder\myapp.exe"""

> > > > > Or you could use:

> > > > >     szTemp   = chr(34) & "c:\program files\myappfolder\myapp.exe"
&
> > > > chr(34)

> > > > > There are other ways to do this, but these are what I use.  The
> first
> > > one
> > > > is
> > > > > more compact, while the 2nd one is easier to interpret.

> > > > > --
> > > > > Bill James

> > > > > Free Win9x VBScript Utilities
> > > > > http://home.earthlink.net/~wgjames/vbspage/

> > > > > > When I run a path\pgm from WSH Shell object, it
> > > > > > will always fail when there is a space in the path.

> > > > > > How can I run a local exe where the path has a space in it?

> > > > > > Example:

> > > > > > ' NFG
> > > > > >    Dim szTemp
> > > > > >    szTemp   = "c:\program files\myappfolder\myapp.exe"
> > > > > >    WshShell = Wscript.CreateObject(Wscript.Shell)
> > > > > >    WshShell.Run szTemp

> > > > > > Where as...

> > > > > >    Dim szTemp
> > > > > >    szTemp   = "c:\nospace_foobar\myappfolder\myapp.exe"
> > > > > >    WshShell = Wscript.CreateObject(Wscript.Shell)
> > > > > >    WshShell.Run szTemp

> > > > > > runs o.k.

> > > > > > Also, unlike in my examples above, in my real-world application
> > > > > > (script) I have no way of knowing the path until runtime. What I
> am
> > > > > > doing is fetching the path from
> > HKEY_LOCAL_MACHINE\software\....\path.

> > > > > > - allen

Sat, 29 Jun 2002 03:00:00 GMT
WshShell.Run "path with spaces" fails
A shortcut is launched via the shell's .Run method, something like this
...

Dim WshShell, MyShortcut, Result
Set WshShell = Wscript.CreateObject("WScript.Shell")
Set MyShortcut = WshShell.CreateShortcut(Shortcut & ".lnk")
MyShortcut.WorkingDirectory = "C:\My Files"
MyShortcut.Save
Cmd = "start " & Chr(34) & Shortcut & Chr(34)
Result = WshShell.Run(Cmd , 0, True)
if Result = 0 Then
Wscript.Echo "Shortcut was launched normally"
else
Wscript.Echo "Shortcut was not executed successful"
end if

Tom Lavedas
-----------
http://www.pressroom.com/~tglbatch/

Quote:

> Both you guys are assuming I have the literal string or knowledge before
> runtime of what the path is. Mike Harris had the best solution (use
> FileSystemObject to get the FileObject, then use the FileObject to get the
> ShortPath), although I certainly appreciate the knowledge about the qoutes.
> I thought I read about that somewhere, and in fact while debugging I tried
> hard coding a path in qoutes, but I did it incorrectly.

> I also thought I read a solution where you could build and run a shortcut on
> the fly. I found all kinds of info on builinding one, but not on launching
> it.

> Anyhoo, thanks for the feedback!!!

> - allen

> > I don't know if the percentage is quite that high, but Murphy's law says
> it
> > will always be the Boss who experiences the exception. <g>

> > --
> > Bill James

> > Free Win9x VBScript Utilities

> > > You are right, only 99.99999999999999% of the time it will be. My error.

> > > > You are making an assumption that Program Files will be progra~1, but
> > that
> > > > is not necessarily true.

> > > > Bill James

> > > > > Or you can write \program files\ as \progra~1\ and that will work
> > also.

> > > > > -Mark

> > > > > > You have to double quote the path, which takes a workaround since
> > the
> > > > > double
> > > > > > quote has special meaning in WSH:  What I usually us is:

> > > > > >     szTemp   = """c:\program files\myappfolder\myapp.exe"""

> > > > > > Or you could use:

> > > > > >     szTemp   = chr(34) & "c:\program files\myappfolder\myapp.exe"
> &
> > > > > chr(34)

> > > > > > There are other ways to do this, but these are what I use.  The
> > first
> > > > one
> > > > > is
> > > > > > more compact, while the 2nd one is easier to interpret.

> > > > > > --
> > > > > > Bill James

> > > > > > Free Win9x VBScript Utilities
> > > > > > http://home.earthlink.net/~wgjames/vbspage/

> > > > > > > When I run a path\pgm from WSH Shell object, it
> > > > > > > will always fail when there is a space in the path.

> > > > > > > How can I run a local exe where the path has a space in it?

> > > > > > > Example:

> > > > > > > ' NFG
> > > > > > >    Dim szTemp
> > > > > > >    szTemp   = "c:\program files\myappfolder\myapp.exe"
> > > > > > >    WshShell = Wscript.CreateObject(Wscript.Shell)
> > > > > > >    WshShell.Run szTemp

> > > > > > > Where as...

> > > > > > >    Dim szTemp
> > > > > > >    szTemp   = "c:\nospace_foobar\myappfolder\myapp.exe"
> > > > > > >    WshShell = Wscript.CreateObject(Wscript.Shell)
> > > > > > >    WshShell.Run szTemp

> > > > > > > runs o.k.

> > > > > > > Also, unlike in my examples above, in my real-world application
> > > > > > > (script) I have no way of knowing the path until runtime. What I
> > am
> > > > > > > doing is fetching the path from
> > > HKEY_LOCAL_MACHINE\software\....\path.

> > > > > > > - allen

Sat, 29 Jun 2002 03:00:00 GMT
WshShell.Run "path with spaces" fails
Tom Lavedas gave another solution that works pretty well.

Solutions in Summary:

1) If you have the literal string, you can place qoutes around it

szPath  = chr(34) & "c:\program files\myappfolder\myapp.exe" & chr(34)
WshShell.Run szPath,1, FALSE

2) You can use the ShortPath instead. Using the FileSystemObject and the
FileObject you can automate the conversion

Set  fso   = Wscript.CreateObject("Scripting.FileSystemObject")
Set  fo     = fso.GetFile(szPath)
szPath     = fo.ShortPath
WshShell.Run szPath,1,FALSE

3) You can build and launch a link

Set MyShortcut = WshShell.CreateShortcut(Shortcut & ".lnk")
MyShortcut.WorkingDirectory = "C:\My Files"
MyShortcut.Save
Cmd = "start " & Chr(34) & Shortcut & Chr(34)
Result = WshShell.Run(Cmd , 0, True)

Thanks everyone, you guys are fantastic!!!

- allen

Quote:
> When I run a path\pgm from WSH Shell object, it
> will always fail when there is a space in the path.

> How can I run a local exe where the path has a space in it?

> Example:

> ' NFG
>    Dim szTemp
>    szTemp   = "c:\program files\myappfolder\myapp.exe"
>    WshShell = Wscript.CreateObject(Wscript.Shell)
>    WshShell.Run szTemp

> Where as...

>    Dim szTemp
>    szTemp   = "c:\nospace_foobar\myappfolder\myapp.exe"
>    WshShell = Wscript.CreateObject(Wscript.Shell)
>    WshShell.Run szTemp

> runs o.k.

> Also, unlike in my examples above, in my real-world application
> (script) I have no way of knowing the path until runtime. What I am
> doing is fetching the path from HKEY_LOCAL_MACHINE\software\....\path.

> - allen

Sat, 29 Jun 2002 03:00:00 GMT

 Page 1 of 1 [ 12 post ]

Relevant Pages