Windows Vista Forums

VBS script with spaces in executable path
  1. #1


    Tyler Durden Guest

    VBS script with spaces in executable path

    I'm trying to write a vbs to run vncviewer on a relative path. The following
    code works, except when the path has spaces:

    Dim LaunchDir, FSO, WSHShell
    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set WSHShell = WScript.CreateObject("WScript.Shell")
    LaunchDir = FSO.GetFolder(".")

    wSHShell.Run LaunchDir & "\vncviewer.exe -notoolbar -autoscaling
    myhost.dyndns.org"

    Where's the error?
    Any help would be appreciated.






      My System SpecsSystem Spec

  2. #2


    James Whitlow Guest

    Re: VBS script with spaces in executable path

    "Tyler Durden" <abuse@xxxxxx> wrote in message
    news:e36RBu%23BJHA.4340@xxxxxx

    > I'm trying to write a vbs to run vncviewer on a relative path. The
    > following
    > code works, except when the path has spaces:
    >
    > Dim LaunchDir, FSO, WSHShell
    > Set FSO = CreateObject("Scripting.FileSystemObject")
    > Set WSHShell = WScript.CreateObject("WScript.Shell")
    > LaunchDir = FSO.GetFolder(".")
    >
    > wSHShell.Run LaunchDir & "\vncviewer.exe -notoolbar -autoscaling
    > myhost.dyndns.org"
    >
    > Where's the error?
    > Any help would be appreciated.
    Trying changing your 'Run' line to (watch for wrapping):

    wSHShell.Run """" & LaunchDir & "\vncviewer.exe"" -notoolbar -autoscaling
    myhost.dyndns.org"

    Also, if you are only using the 'Scripting.FileSystemObject' to get the
    current directory and nothing else, you can get it from 'WScript.Shell' &
    remove the instantiation of 'Scripting.FileSystemObject'

    LaunchDir = WSHShell.CurrentDirectory

    FYI: The '.CurrentDirectory' method is read/write, so you can use it to
    change the current directory as well.




      My System SpecsSystem Spec

  3. #3


    Tyler Durden Guest

    Re: VBS script with spaces in executable path

    James, tested here and worked perfectly. And is a better solution than mine.
    Thank you.


    "James Whitlow" <jwhitlow.60372693@xxxxxx> wrote in message
    news:%23ZKPDgGCJHA.2480@xxxxxx

    > "Tyler Durden" <abuse@xxxxxx> wrote in message
    > news:e36RBu%23BJHA.4340@xxxxxx

    >> I'm trying to write a vbs to run vncviewer on a relative path. The
    >> following
    >> code works, except when the path has spaces:
    >>
    >> Dim LaunchDir, FSO, WSHShell
    >> Set FSO = CreateObject("Scripting.FileSystemObject")
    >> Set WSHShell = WScript.CreateObject("WScript.Shell")
    >> LaunchDir = FSO.GetFolder(".")
    >>
    >> wSHShell.Run LaunchDir & "\vncviewer.exe -notoolbar -autoscaling
    >> myhost.dyndns.org"
    >>
    >> Where's the error?
    >> Any help would be appreciated.
    >
    > Trying changing your 'Run' line to (watch for wrapping):
    >
    > wSHShell.Run """" & LaunchDir & "\vncviewer.exe"" -notoolbar -autoscaling
    > myhost.dyndns.org"
    >
    > Also, if you are only using the 'Scripting.FileSystemObject' to get the
    > current directory and nothing else, you can get it from 'WScript.Shell' &
    > remove the instantiation of 'Scripting.FileSystemObject'
    >
    > LaunchDir = WSHShell.CurrentDirectory
    >
    > FYI: The '.CurrentDirectory' method is read/write, so you can use it to
    > change the current directory as well.
    >
    >
    >


      My System SpecsSystem Spec

  4. #4


    Tyler Durden Guest

    Re: VBS script with spaces in executable path

    James, just to make sure, the following is the complete script. Could it be
    optimized/cleaned in any other way?

    Dim LaunchDir, FSO, WSHShell
    Set WSHShell = WScript.CreateObject("WScript.Shell")
    LaunchDir = WSHShell.CurrentDirectory

    WSHShell.Run """" & LaunchDir & "\vncviewer.exe"" -notoolbar -autoscaling
    myhost.dyndns.org"


    "James Whitlow" <jwhitlow.60372693@xxxxxx> wrote in message
    news:%23ZKPDgGCJHA.2480@xxxxxx

    > "Tyler Durden" <abuse@xxxxxx> wrote in message
    > news:e36RBu%23BJHA.4340@xxxxxx

    >> I'm trying to write a vbs to run vncviewer on a relative path. The
    >> following
    >> code works, except when the path has spaces:
    >>
    >> Dim LaunchDir, FSO, WSHShell
    >> Set FSO = CreateObject("Scripting.FileSystemObject")
    >> Set WSHShell = WScript.CreateObject("WScript.Shell")
    >> LaunchDir = FSO.GetFolder(".")
    >>
    >> wSHShell.Run LaunchDir & "\vncviewer.exe -notoolbar -autoscaling
    >> myhost.dyndns.org"
    >>
    >> Where's the error?
    >> Any help would be appreciated.
    >
    > Trying changing your 'Run' line to (watch for wrapping):
    >
    > wSHShell.Run """" & LaunchDir & "\vncviewer.exe"" -notoolbar -autoscaling
    > myhost.dyndns.org"
    >
    > Also, if you are only using the 'Scripting.FileSystemObject' to get the
    > current directory and nothing else, you can get it from 'WScript.Shell' &
    > remove the instantiation of 'Scripting.FileSystemObject'
    >
    > LaunchDir = WSHShell.CurrentDirectory
    >
    > FYI: The '.CurrentDirectory' method is read/write, so you can use it to
    > change the current directory as well.
    >
    >
    >


      My System SpecsSystem Spec

  5. #5


    James Whitlow Guest

    Re: VBS script with spaces in executable path

    "Tyler Durden" <abuse@xxxxxx> wrote in message
    news:OldexvGCJHA.1632@xxxxxx

    > James, just to make sure, the following is the complete script. Could it
    > be optimized/cleaned in any other way?
    >
    > Dim LaunchDir, FSO, WSHShell
    > Set WSHShell = WScript.CreateObject("WScript.Shell")
    > LaunchDir = WSHShell.CurrentDirectory
    >
    > WSHShell.Run """" & LaunchDir & "\vncviewer.exe"" -notoolbar -autoscaling
    > myhost.dyndns.org"
    I personally think it looks good. Others in the group might have some
    helpful comments, though.

    The only things that I would do differently would be to add 'Option
    Explicit' as the first line to force variable declaration and use some form
    of Hungarian Notation to indicate what type your variables are (string,
    integer, object, etc.). I think most people prefer 3 letter abbreviations
    (ex: strLaunchDir, objWSHShell), I personally prefer 1 letter abbreviations
    (ex: sLaunchDir, oWSHShell).



      My System SpecsSystem Spec

  6. #6


    Tyler Durden Guest

    Re: VBS script with spaces in executable path

    Thank you.


    "James Whitlow" <jwhitlow.60372693@xxxxxx> wrote in message
    news:uBDZfgHCJHA.2476@xxxxxx

    > "Tyler Durden" <abuse@xxxxxx> wrote in message
    > news:OldexvGCJHA.1632@xxxxxx

    >> James, just to make sure, the following is the complete script. Could it
    >> be optimized/cleaned in any other way?
    >>
    >> Dim LaunchDir, FSO, WSHShell
    >> Set WSHShell = WScript.CreateObject("WScript.Shell")
    >> LaunchDir = WSHShell.CurrentDirectory
    >>
    >> WSHShell.Run """" & LaunchDir & "\vncviewer.exe"" -notoolbar -autoscaling
    >> myhost.dyndns.org"
    >
    > I personally think it looks good. Others in the group might have some
    > helpful comments, though.
    >
    > The only things that I would do differently would be to add 'Option
    > Explicit' as the first line to force variable declaration and use some
    > form of Hungarian Notation to indicate what type your variables are
    > (string, integer, object, etc.). I think most people prefer 3 letter
    > abbreviations (ex: strLaunchDir, objWSHShell), I personally prefer 1
    > letter abbreviations (ex: sLaunchDir, oWSHShell).
    >


      My System SpecsSystem Spec

  7. #7


    Al Dunbar Guest

    Re: VBS script with spaces in executable path


    "James Whitlow" <jwhitlow.60372693@xxxxxx> wrote in message
    news:uBDZfgHCJHA.2476@xxxxxx

    > "Tyler Durden" <abuse@xxxxxx> wrote in message
    > news:OldexvGCJHA.1632@xxxxxx

    >> James, just to make sure, the following is the complete script. Could it
    >> be optimized/cleaned in any other way?
    >>
    >> Dim LaunchDir, FSO, WSHShell
    >> Set WSHShell = WScript.CreateObject("WScript.Shell")
    >> LaunchDir = WSHShell.CurrentDirectory
    >>
    >> WSHShell.Run """" & LaunchDir & "\vncviewer.exe"" -notoolbar -autoscaling
    >> myhost.dyndns.org"
    >
    > I personally think it looks good. Others in the group might have some
    > helpful comments, though.
    >
    > The only things that I would do differently would be to add 'Option
    > Explicit' as the first line to force variable declaration and use some
    > form of Hungarian Notation to indicate what type your variables are
    > (string, integer, object, etc.). I think most people prefer 3 letter
    > abbreviations (ex: strLaunchDir, objWSHShell), I personally prefer 1
    > letter abbreviations (ex: sLaunchDir, oWSHShell).
    Personally, I mistrust the current directory and prefer to explicitly use
    the directory where the script is located, i.e.:

    replace( WScript.ScriptFullName, WScript.ScriptName, "vncviewer.exe")

    This will give the full path to a file called "vncviewer.exe" located in the
    same directory as the script itself - even if the current directory has been
    changed for some reason, or is pointing elsewhere in case your script is
    being executed by another script and not by double-clicking in explorer.

    Some will point out that the above replace reference will fail in the event
    that the path includes a folder that is a superset of the name of the
    script. A more bullet-proof method would use the GetParent method of the
    file system object. As an aside, it is almost always better to use the
    filename parsing capabilities of FSO instead of string concatenation and
    sub-stringing methods to calculate file paths.

    /Al



      My System SpecsSystem Spec

VBS script with spaces in executable path problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Spaces in script path JRJohnson PowerShell 1 05 Aug 2009
Re: How to run an executable with spaces in the command line ssg31415926 PowerShell 0 21 Apr 2008
How to run an executable with spaces in the command line ssg31415926 PowerShell 4 21 Apr 2008
path to executable newbee2 Software 0 10 Mar 2008
script with spaces in the path Tim PowerShell 3 15 Nov 2006