Jump to content

Spaces in @comspec /c exe path


Recommended Posts

How can I syntax the following correctly for a exe path with a space in it. The following works fine until there is a space

 

$command = ' /c C:DIR1EXE "file name.ext" /A /O'
$PID = Run(@ComSpec &$command, "C:DIR1SUB1SUB2")

so I assumed that I needed to do this

$command = ' /c "C:\DIR 1\EXE" "file name.ext" /A /O'
$PID = Run(@ComSpec &$command, "C:\DIR1\SUB1\SUB2")

but this just flashes up the CMD box and then it disappeared. Can someone point me the right way to handle the space in the exe path as well as the file name?

Link to comment
Share on other sites

You want to use CHR(34) to format your string:

$command = " /c " & CHR(34) & "C:\DIR 1\EXE" & CHR(34) & " " & CHR(34) & "file name.ext" & CHR(34) & " /A /O"

( Above code not tested, but if you need a quotation mark in your string, use CHR(34) ).

Please correct me if I am wrong in any of my posts. I like learning from my mistakes too.

Link to comment
Share on other sites

Hi Lee,

Is executing cmd.exe even necessary?

Which might seem like a silly question, however you know what executable you are trying to run through cmd and we don't.

How about:

$command = '"C:\DIR 1\EXE" "file name.ext" /A /O'
$PID = Run($command, "C:\DIR 1\SUB1\SUB2")
Is the program you are trying to execute a console application also?

Vlad

Edited by Mobius

wtfpl-badge-1.png

Link to comment
Share on other sites

MrBeatnik your suggestion unfortunately did not work. 

Mobius you were quite right it wasn't necessary using your suggestion i got it to work. I was also able to get it to work by writing the line into a batch file and running that.

I was still curious though why @Comspec was not handling a path in speech marks. I did find this older form post before making this one  but it left me no wiser.

Thank you for the help but would welcome any further insight into the @comspec issue might be of use toothers and in the future though.

Link to comment
Share on other sites

#cs
Taken from CMD.exe stdout reference

If /C or /K is specified, then the remainder of the command line after
the switch is processed as a command line, where the following logic is
used to process quote (") characters:

    1.  If all of the following conditions are met, then quote characters
        on the command line are preserved:

        - no /S switch
        - exactly two quote characters
        - no special characters between the two quote characters,
          where special is one of: &<>()@^|
        - there are one or more whitespace characters between the
          two quote characters
        - the string between the two quote characters is the name
          of an executable file.

    2.  Otherwise, old behavior is to see if the first character is
        a quote character and if so, strip the leading character and
        remove the last quote character on the command line, preserving
        any text after the last quote character.
#ce
$DBGMSG = 0
; #EG 1
; So this will not work because of the number of double quotes in the command line
; < Resulting error is intentional for example purposes
$command = ' /k "c:\DIR 1\EXE.exe" "file name.ext" /A /O'
$PID = RunWait(@COMSPEC & $command,"C:\DIR 1\SUB1\SUB2")
IF $DBGMSG THEN msgbox(0,"EG 1","PID: "& $PID & @LF &"ERR: "& @ERROR)
; #EG 2
; You can get around this flaw by setting the working directory for cmd.exe
; to the directory of the executable you are running, negating the need for its path
; to be specified.
$command = ' /k EXE.exe "C:\DIR 1\SUB1\SUB2\file name.ext" /A /O'
$PID = RunWait(@COMSPEC & $command,"C:\DIR 1")
IF $DBGMSG THEN msgbox(0,"EG 2","PID: "& $PID & @LF &"ERR: "& @ERROR)
; #EG 3
; However good luck if your exe you are running also has a space in its filename
; < Resulting error is intentional for example purposes
$command = ' /k My EXE.exe "C:\DIR 1\SUB1\SUB2\file name.ext" /A /O'
$PID = RunWait(@COMSPEC & $command,"C:\DIR 1")
IF $DBGMSG THEN msgbox(0,"EG 3","PID: "& $PID & @LF &"ERR: "& @ERROR)
; #EG 4
; Obviously you cannot use the above (#EG 2) method to set the working directory
; to that of the file you are passing to your executable instead because I can almost
; guarantee that executable expects that file on its command line to be wrapped
; in double quotes if the file path contains spaces
$command = ' /k "c:\DIR 1\EXE.exe" file name.ext /A /O'
$PID = RunWait(@COMSPEC & $command,"C:\DIR 1\SUB1\SUB2")
IF $DBGMSG THEN msgbox(0,"EG 4","PID: "& $PID & @LF &"ERR: "& @ERROR)
This archive contains executables - files and folders that are identical to the conditions and parameters posted by you, it is not strictly necessary to understand the above code but it might compliment it.

Maybe it might explain why I often complain when users use @comspec as part of the Run/Runwait functions when not absolutely necessary.

Then again probably not.

Vlad

Edited by Mobius

wtfpl-badge-1.png

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...