Run RunWait A_ComSpec expressions with variables: extra quote marks issue

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
neogna2
Posts: 586
Joined: 15 Sep 2016, 15:44

Run RunWait A_ComSpec expressions with variables: extra quote marks issue

02 Dec 2021, 10:54

https://lexikos.github.io/v2/docs/commands/Run.htm#Remarks
When running a program via Comspec (cmd.exe) ... if the path or name of the executable contains spaces, the entire string should be enclosed in an outer pair of single-quote marks.

Code: Select all

Run A_ComSpec ' /c "C:\My Utility.exe" "param 1" "second param" >"C:\My File.txt"'
Ok, but what if we instead want to use variables that contain paths with spaces?

I first tried this pattern.

Code: Select all

App := "C:\My Utility.exe"
Text := "C:\My File.txt"
Run A_ComSpec ' /c "' App '" "param 1" "second param" > "' Text '"'
But the cmd window gives the error "'C:\My ' is not recognized as an internal or external command"

To fix the error we can add an extra double-quote after /c and before the last single-quote.
Run A_ComSpec ' /c " "' App '" "param 1" "second param" > "' Text '" " '

Why are those extra double-quotes needed? Is it a bug? Incomplete/incorrect documentation? neogna2 comprehension problem?

Follow these steps to reproduce the issue. I use Handle only as an example here, the pattern is the same also with other apps as long as there are spaces in the app's path or filename.
- Download Handle from Microsoft's SysInternals, https://download.sysinternals.com/files/Handle.zip (800kB)
- Create the folder C:\a b\ and copy handle64.exe into that folder
- Save the below v2 scripts as C:\a b\test.ahk
- Open one more File Explorer window and navigate to C:\ , which Handle should find if all goes well.
- Run the script with AutoHotkey v2 beta 3

Code: Select all

;test 1 (error)
Command := "C:\a b\handle64.exe"
Path := "C:"
RunWait A_ComSpec ' /k "' Command '" -p Explorer.exe "' Path '" '
; using /k instead of /c to persist cmd window with error text until manually closed

;test 2 (no error, notice the extra double-quotes)
RunWait A_ComSpec ' /k " "' Command '" -p Explorer.exe "' Path '" " '
edit: fixed some copy and paste mistakes. Clarified topic title.
Last edited by neogna2 on 03 Dec 2021, 05:24, edited 1 time in total.
neogna2
Posts: 586
Joined: 15 Sep 2016, 15:44

Re: Run RunWait A_ComSpec expressions and doublequote singlequote escaping

03 Dec 2021, 05:10

Some pasting/typing mistakes made parts of my first post confusing. Sorry about that, fixed now.

Continuing on topic. After more testing I now think there is an error in the documentation at
https://lexikos.github.io/v2/docs/commands/Run.htm#Remarks
When running a program via Comspec (cmd.exe) [...] if the path or name of the executable contains spaces, the entire string should be enclosed in an outer pair of single-quote marks.
Run A_ComSpec ' /c "C:\My Utility.exe" "param 1" "second param" >"C:\My File.txt"'
Because this line, which uses the format suggested by the documentation example above, causes an error in the cmd window
Run A_ComSpec ' /k "C:\a b\handle64.exe" -p Explorer.exe "C:"'

The error text in the cmd window is C:\a' is not recognized as an internal or external command, operable program or batch file.

To fix the error the line must be (notice the extra pair of double-quote marks starting after /k)
Run A_ComSpec ' /k " "C:\a b\handle64.exe" -p Explorer.exe "C:" "'

If I'm right about this then the v2 documentation should be updated to read
When running a program via Comspec (cmd.exe) [...] if the path or name of the executable contains spaces, the entire string that follows after /c or /k should be enclosed in an extra pair of double-quote marks.
Run A_ComSpec ' /c " "C:\My Utility.exe" "param 1" "second param" >"C:\My File.txt" " '
Which would make it in line with how and where the v1 documentation instructs us to place extra double-quote marks:
https://www.autohotkey.com/docs/commands/Run.htm#Remarks
When running a program via ComSpec (cmd.exe) [...] if the path or name of the executable contains spaces, the entire string should be enclosed in an outer pair of quotes.
Run %ComSpec% /c ""C:\My Utility.exe" "param 1" "second param" >"C:\My File.txt""
It would also help to add to the v2 documentation a ComSpec example with spaces and variables, either directly below the previous example in the text or in the examples section at the bottom of the Run / RunWait help page

Code: Select all

App := "C:\My Utility.exe"
File := "C:\My File.txt"
Run A_ComSpec ' /c " "' App '" "param 1" "second param" >"' File '" " '
neogna2
Posts: 586
Joined: 15 Sep 2016, 15:44

Re: Run RunWait A_ComSpec expressions with variables: extra quote marks issue

03 Dec 2021, 06:30

Wait there is more! Run with ComSpec seems to require the extra double-quote marks if there are spaces in the executable path or name but also if there are spaces in any of its subsequent arguments.

To see that first move Handle to C:\a\handle64.exe , open C:\a b in File Explorer and Run this v2 script

Code: Select all

;Error in cmd window "The filename, directory name, or volume label syntax is incorrect."
RunWait A_ComSpec ' /k "C:\a\handle64.exe" -p Explorer.exe "C:\a b" '

;No error
RunWait A_ComSpec ' " /k "C:\a\handle64.exe" -p Explorer.exe "C:\a b" " '
The v2 documentation should then be changed to
When running a program via Comspec (cmd.exe) [...] if there are spaces in the executable's path or name or arguments then the entire string that follows after /c or /k should be enclosed in an extra pair of double-quote marks.
Run A_ComSpec ' /c " "C:\My Utility.exe" "param 1" "second param" >"C:\My File.txt" " '
... To complicate further this second source of error from spaces in arguments is maybe not general but dependent on how the specific executable parses and expects its arguments? If yes then the documentation could say
When running a program via Comspec (cmd.exe) [...] if the executable's path or name contains spaces the entire string that follows after /c or /k should be enclosed in an extra pair of double-quote marks. Some executables also require that extra pair of double-quote marks if their arguments contain spaces.
safetycar
Posts: 435
Joined: 12 Aug 2017, 04:27

Re: Run RunWait A_ComSpec expressions with variables: extra quote marks issue

03 Dec 2021, 10:53

This is more related to cmd.exe than to ahk. I tried to search a link where I read about this but I couldn't find it.
The explanation was on how comspec handles quotes. It has 2 modes, one preserves quotes, and the other removes the first and last quote.
It's better that you read it from the source... Open cmd.exe, and type as input "cmd.exe /?" and press enter, you might need to keep pressing enter to read the rest of the help text.
You should pay some attention to the /S switch description to not get lost while reading.

So for example not meeting the requirement of "- exactly two quote characters" makes it fall to case 2 and strip your first and last quotes (understood as if the remaining quotes were trying to quote " -p Explorer.exe ").
Something more bulletproof would be always include /S to know that you are working with case 2, but that would require the inconvenience of always putting the extra quotes.
neogna2
Posts: 586
Joined: 15 Sep 2016, 15:44

Re: Run RunWait A_ComSpec expressions with variables: extra quote marks issue

04 Dec 2021, 11:52

@safetycar Thanks.

I read the cmd /? help text (also online version at Microsoft) and these long discussions
https://stackoverflow.com/questions/355988/how-do-i-deal-with-quote-characters-when-using-cmd-exe
https://stackoverflow.com/questions/9866962/what-is-cmd-s-for

My takeaway for AHK v2 ComSpec is
* Don't use /s
* If no element (executable path or argument) has spaces then don't quote enclose anything (case 1)
* Otherwise quote enclose each element that has spaces and put an extra outside pair of quotes starting after the /c or /k (case 2-4)

v2 examples
Case 1 Run A_ComSpec ' /c C:\a.exe abc'
Case 2 Run A_ComSpec ' /c " "C:\a b\a.exe" abc " '
Case 3 Run A_ComSpec ' /c " C:\a.exe "hello world" " '
Case 4 Run A_ComSpec ' /c " "C:\a b\a.exe" "hello world" " '

Use variables according to the general AHK v2 syntax rules.
Case 4 example with variables.

Code: Select all

App := "C:\a b\a.exe"
Arg := "hello world"
Run A_ComSpec ' /c " "' App '" "' Arg '" " '
The v2 documentation's current sentence and example is still incorrect and should be changed.
Here's an updated short suggestion that I think is clear enough together with the corrected example.
When running a program via Comspec (cmd.exe) [...] if there are spaces in the executable's path or name or arguments then the entire string that follows after /c or /k should be enclosed in an extra pair of double-quote marks. This is because of how cmd.exe parses input.
Run A_ComSpec ' /c " "C:\My Utility.exe" "param 1" "second param" >"C:\My File.txt" " '
The added second sentence could also link to the Microsoft cmd help text and/or Stack Overflow threads.

Anyone disagreeing with this updated suggestion?

edit: In addition the documentation sentence "In the following example, the outer single-quote marks are shown in red and all the inner double-quote marks are shown in black:" can be removed, because incorrect and because the example is clear enough on its own, even without any quotation marks colored red.
Last edited by neogna2 on 04 Dec 2021, 12:15, edited 1 time in total.
safetycar
Posts: 435
Joined: 12 Aug 2017, 04:27

Re: Run RunWait A_ComSpec expressions with variables: extra quote marks issue

04 Dec 2021, 12:09

Looks correct to me part of the purpose of the double quotes was lost in the v2 update.
I'll mention @Ragnar here to see if he can read this.
User avatar
Ragnar
Posts: 611
Joined: 30 Sep 2013, 15:25

Re: Run RunWait A_ComSpec expressions with variables: extra quote marks issue

07 Dec 2021, 16:58

Thanks for reporting. I've fixed the Run example in PR #529.

Note that I have not applied all your suggestions. I only changed what needed to be changed so that it's in line with the v1 docs. In my opinion, additional info regarding cmd.exe is not necessary and can be looked up by the users themselves.
neogna2
Posts: 586
Joined: 15 Sep 2016, 15:44

Re: Run RunWait A_ComSpec expressions with variables: extra quote marks issue

08 Dec 2021, 07:17

Ragnar wrote:
07 Dec 2021, 16:58
Thanks for reporting. I've fixed the Run example in PR #529.
Thanks for updating.
Ragnar wrote:
07 Dec 2021, 16:58
Note that I have not applied all your suggestions. I only changed what needed to be changed so that it's in line with the v1 docs.
I still think this if the path or name of the executable contains spaces is incomplete and should be if the executable's path or name or arguments contains spaces because I above gave a Run ComSpec example where spaces in arguments causes it to fail if no extra quotes, despite there being no spaces in the executable's name or path. I haven't so far found any example where both (a) an argument has spaces and (b) applying the extra quotes are redundant or causes unwanted behaviour. (If anyone finds one, post it here!) This change only "costs" 8 extra characters of documentation length.
Ragnar wrote:
07 Dec 2021, 16:58
In my opinion, additional info regarding cmd.exe is not necessary and can be looked up by the users themselves.
Minor quibble: many cmd.exe details are off-topic yeah, but still helpful to say that cmd.exe's parsing rules, not AHK itself, explains why the extra quotes are needed. E.g. This is because of how cmd.exe parses input. That hints readers to look for more details in external cmd.exe help resources, not the AHK docs. That's IMO more important than both these In the following example, the outer quotes are highlighted in yellow: and
-- perhaps because you need to redirect the program's input or output --.

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: a_bolog, Descolada, imstupidpleshelp, ositoMalvado, Rohwedder and 32 guests