quote and double-quote " " "

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
GEOVAN
Posts: 192
Joined: 03 Mar 2022, 11:12

quote and double-quote " " "

05 Oct 2023, 13:57

Hello,

I have the following command that woks OK:
Runwait, cmd.exe /c " "%A_ScriptDir%\DATA\gsc.exe" -o "%outtemp1%" -sDEVICE=pdfwrite "%A_LoopFilePath%" pdfmark.txt "
As it can be seen there are TWO quote marks one at the begining (after /c) and the other at the end (after pdfmark.txt)
If i do not add the last quote mark it will NOT work, and this understandable OK.

But here is a strange behavior at the following command:
Runwait, cmd.exe /c " "%A_ScriptDir%\DATA\qpdf.exe" --empty --pages "%outtemp1%" -- "%out1%" "
As it can be seen there are TWO quote marks one at the begining (after /c) and the other at the end (after "%out1%")
BUT - here is the QUESTION --> If i do not add the last quote mark it will AGAIN work.
BUT it should NOT work. WHY it works please?

I will appreciate your commentσ
Thank you very very much!
User avatar
andymbody
Posts: 993
Joined: 02 Jul 2017, 23:47

Re: quote and double-quote " " "

05 Oct 2023, 14:27

Do you by chance have a double-quote in one of the variables? Out1 maybe?

Code: Select all

out1 = has a quote"
Msgbox % out1
GEOVAN
Posts: 192
Joined: 03 Mar 2022, 11:12

Re: quote and double-quote " " "

05 Oct 2023, 15:34

Yes , this is a very interesting point - actually the only point make sense, but i can not see where is this EXTRA WRONG quote mark.
The 3 variables used are as follows:

Code: Select all

FileSelectFolder output1,, 3, Select the output directory

SplitPath A_LoopFilePath,,,extens, fnBare

out1 := output1 "\Converted_Files\" fnBare "." extens
outtemp1 :=  output1 "\Converted_Files\" "Temp" "\" fnBare "." extens

BUT, can you please see where is this EXTRA WRONG quote mark ?
User avatar
andymbody
Posts: 993
Joined: 02 Jul 2017, 23:47

Re: quote and double-quote " " "

05 Oct 2023, 16:31

The only significant difference I see between your first example (that works), and the second example (that doesn't work) is that the string ends with text in the first, and ends with a variable in the second. This might affect how it is out together prior to performing the Run.

Try this:

Code: Select all

cmdStr = "%A_ScriptDir%\DATA\qpdf.exe --empty --pages %outtemp1% -- %out1%"
Runwait, cmd.exe /c %cmdStr%
GEOVAN
Posts: 192
Joined: 03 Mar 2022, 11:12

Re: quote and double-quote " " "

05 Oct 2023, 16:39

Thank you for that point.

But please note regarding my first question:

Code: Select all

Runwait, cmd.exe /c " "%A_ScriptDir%\DATA\qpdf.exe" --empty --pages "%outtemp1%" -- "%out1%" "
Note that the above it is working OK.
The question is:
If i do not add the last quote mark it will AGAIN work.

Code: Select all

Runwait, cmd.exe /c " "%A_ScriptDir%\DATA\qpdf.exe" --empty --pages "%outtemp1%" -- "%out1%" 
BUT it should NOT work. WHY it works please?
Where is the "tric" ?
User avatar
andymbody
Posts: 993
Joined: 02 Jul 2017, 23:47

Re: quote and double-quote " " "

05 Oct 2023, 16:42

GEOVAN wrote:
05 Oct 2023, 16:39
Where is the "tric" ?
It's hard to say, because I am unable to run this exact command to test different theories. So all I can do is to offer tests so you can see if anything changes. This may provide clues to the cause.

Try removing the spaces from the end quotes? Does this run?
Runwait, cmd.exe /c ""%A_ScriptDir%\DATA\qpdf.exe" --empty --pages "%outtemp1%" -- "%out1%""

Your original question is a good one. I don't see the cause. Sometimes these things pop up and don't make sense to most of us. In those times we need the insight from the developer @lexikos. There is usually an answer and sometimes he is the only one who can provide it.

There are also others here that are very sharp and may eventually provide an answer that eludes limited intellect such as mine.
Last edited by andymbody on 05 Oct 2023, 17:14, edited 1 time in total.
TAC109
Posts: 1129
Joined: 02 Oct 2013, 19:41
Location: New Zealand

Re: quote and double-quote " " "

05 Oct 2023, 17:13

You should always endeavour to use correct syntax, even if it appears to work otherwise, as a later update may tighten the syntax checking and cause an error message. In this case I expect either 'cmd.exe' or 'qpdf.exe' tolerate the mistake and make a silent correction.

Cheers
My scripts:-
XRef - Produces Cross Reference lists for scripts
ReClip - A Text Reformatting and Clip Management utility
ScriptGuard - Protects Compiled Scripts from Decompilation
I also maintain Ahk2Exe
GEOVAN
Posts: 192
Joined: 03 Mar 2022, 11:12

Re: quote and double-quote " " "

06 Oct 2023, 08:06

Hello,
Please let me explain my findings through TWO examples:

Example 1:

Code: Select all

SetWorkingDir, %A_ScriptDir%
out1 :=  A_ScriptDir "\output.pdf"
outtemp1 :=    A_ScriptDir "\input.pdf"

;BOTH the following commands are working
Runwait, cmd.exe /c " "%A_ScriptDir%\DATA\qpdf.exe" --empty --pages "%outtemp1%" -- "%out1%"
Runwait, cmd.exe /c " "%A_ScriptDir%\DATA\qpdf.exe" --empty --pages "%outtemp1%" -- "%out1%" "


Example 2:

Code: Select all

SetWorkingDir, %A_ScriptDir%
out1 :=  A_ScriptDir "\output.pdf"
outtemp1 :=    A_ScriptDir "\input.pdf"

;BOTH the following commands are working
Runwait, cmd.exe /c " "%A_ScriptDir%\DATA\gsc.exe" -o "%out1%" -sDEVICE=pdfwrite "%outtemp1%" "%A_ScriptDir%\DATA\pdfmark.txt" 
Runwait, cmd.exe /c " "%A_ScriptDir%\DATA\gsc.exe" -o "%out1%" -sDEVICE=pdfwrite "%outtemp1%" "%A_ScriptDir%\DATA\pdfmark.txt" "


As it is clearly seen from the above TWO examples the commands are working in BOTH CASES, that is:
CASE A --> Initial quote mark (after cmd.exe /c) WITHOUT closing quote at the very end (after pdfmark.txt)
CASE B --> Initial quote mark (after cmd.exe /c) AND closing quote at the very end (after pdfmark.txt)


Note:
The above are valid and working for BOTH EXAMPLES (1 - gsc.exe) and (2 - qpdf.exe)
(Excuse me because in my initial post comment i say something different - that was mistake)


FINALLY, according to the above, the FINAL QUESTION is the following:
STRICTLY speaking, what is the "more" correct SYNTAX to use? (CASE A or CASE B)?
User avatar
andymbody
Posts: 993
Joined: 02 Jul 2017, 23:47

Re: quote and double-quote " " "

06 Oct 2023, 08:40

GEOVAN wrote:
06 Oct 2023, 08:06
"Why do they both work"
I would say this is because the entire command is enclosed in quotes either way. I may be wrong, but I think the inner quotes are considered text, not quotes, no matter how many there are (from the perspective Runwait). And I don't think the inner quotes are needed for Runwait to perform its job. The outer quotes are needed tho, since there are spaces in the full command.

Which is why I asked you earlier to try the example below that only has outer quotes, but no inner quotes. Does this one work?

Try this:

Code: Select all

cmdStr = "%A_ScriptDir%\DATA\qpdf.exe --empty --pages %outtemp1% -- %out1%"
Runwait, cmd.exe /c %cmdStr%
GEOVAN
Posts: 192
Joined: 03 Mar 2022, 11:12

Re: quote and double-quote " " "

06 Oct 2023, 10:32

Hello,

CASE I
Yes, i try it and also your suggestion is working:

Code: Select all

cmdStr = "%A_ScriptDir%\DATA\qpdf.exe --empty --pages %outtemp1% -- %out1%"
Runwait, cmd.exe /c %cmdStr%

CASE II
Also according to your thoughts, the following is working:

Code: Select all

Runwait, cmd.exe /c " %A_ScriptDir%\DATA\qpdf.exe --empty --pages %outtemp1% -- %out1% "

CASE III
Also as mention in my earlier comment the following is working:

Code: Select all

Runwait, cmd.exe /c " "%A_ScriptDir%\DATA\qpdf.exe" --empty --pages "%outtemp1%" -- "%out1%"

CASE IV
And also as mention in my earlier comment the following is working:

Code: Select all

Runwait, cmd.exe /c " "%A_ScriptDir%\DATA\qpdf.exe" --empty --pages "%outtemp1%" -- "%out1%" "

FINALLY, according to the above, the FINAL QUESTION is the following:
STRICTLY speaking, what is the "more" correct SYNTAX to use? (CASE I or CASE II OR CASE III or CASE IV)?
User avatar
andymbody
Posts: 993
Joined: 02 Jul 2017, 23:47

Re: quote and double-quote " " "

06 Oct 2023, 10:49

GEOVAN wrote:
06 Oct 2023, 10:32
STRICTLY speaking, what is the "more" correct SYNTAX to use? (CASE I or CASE II OR CASE III or CASE IV)?
In this case I would suggest 1 or 2, but not 3 or 4 since they are confusing and include unnecessary quotes.

I suspect that the only time you need the inner quotes is if a path has spaces, but yours did not in this case.

For instance if the path was
%A_ScriptDir%\DATA 123\gsc.exe you would surround in quotes like this:
"%A_ScriptDir%\DATA 123\gsc.exe"

These inner quotes are for cmd.exe, not Runwait.

But I could be wrong about that. Someone else with more knowledge may be able to provide more correct info.

Andy
GEOVAN
Posts: 192
Joined: 03 Mar 2022, 11:12

Re: quote and double-quote " " "

06 Oct 2023, 11:48

Hello,
I try it and i confirm that your suspect is valid, that is if a path has spaces it NEEDS the inner quotes.
So to GENERALIZE the whole matter:

CASE I

Code: Select all

cmdStr = " "%A_ScriptDir%\DATA 123\qpdf.exe" --empty --pages %outtemp1% -- %out1% "
Runwait, cmd.exe /c %cmdStr%
CASE II

Code: Select all

Runwait, cmd.exe /c " "%A_ScriptDir%\DATA 123\qpdf.exe" --empty --pages %outtemp1% -- %out1% "

CASE III

Code: Select all

Runwait, cmd.exe /c " "%A_ScriptDir%\DATA 123\qpdf.exe" --empty --pages "%outtemp1%" -- "%out1%"

CASE IV

Code: Select all

Runwait, cmd.exe /c " "%A_ScriptDir%\DATA 123\qpdf.exe" --empty --pages "%outtemp1%" -- "%out1%" "

FINALLY, according to the above, the FINAL QUESTION is the following:
STRICTLY speaking, what is the "more" correct SYNTAX to use? (CASE I or CASE II OR CASE III or CASE IV)?
(I personally prefer CASE IV), but what is your opinion, please?
User avatar
andymbody
Posts: 993
Joined: 02 Jul 2017, 23:47

Re: quote and double-quote " " "

06 Oct 2023, 12:29

GEOVAN wrote:
06 Oct 2023, 11:48
(I personally prefer CASE IV), but what is your opinion, please?
If I were writing it, I would be doing it in v2 which allows single and double quotes and would do it similar to case 1. In v1 I would probably choose case 1 as well. Because I prefer to put strings in variables so I can inspect them when it doesn't work correctly (debugging). And I can name a variable in a descriptive way that tells me what it is for.
GEOVAN
Posts: 192
Joined: 03 Mar 2022, 11:12

Re: quote and double-quote " " "

06 Oct 2023, 14:21

Thank you very much all for your help on this topic.
You really help me.

Please let me further discuss:

(1)
If i want to use CASE IV, is it OK to use, i mean is it correct programmatically, please?




(2)
Only one last think i did not understand it is regarding CASE iii :

Code: Select all

Runwait, cmd.exe /c " "%A_ScriptDir%\DATA 123\qpdf.exe" --empty --pages "%outtemp1%" -- "%out1%"
Why it is working since the very end quote mark is missing?
"Programmatically" it shouldn't work.
Any explanation, please?



(3)
Also instead of using

Code: Select all

Runwait, cmd.exe

I have the opinion from what i read until now, that it is better in order to ENSURE working in any cases to use:

Code: Select all

Runwait,  %comspec%
In other words %comspec% is BETTER than cmd.exe. Do you agree with me, please?



I will appreciate your comments in (1), (2), and (3) , please.
User avatar
andymbody
Posts: 993
Joined: 02 Jul 2017, 23:47

Re: quote and double-quote " " "

06 Oct 2023, 14:47

GEOVAN wrote:
06 Oct 2023, 14:21
(1)
If i want to use CASE IV, is it OK to use, i mean is it correct programmatically, please?
I rarely use Run. So I am learning from your post. But if it works the way you prefer, continue to use it until it doesn't.


(2)
"Programmatically" it shouldn't work.
Any explanation, please?
As I stated earlier, I think the inner quotes are ignored by Run. The inner quotes would be used by cmd.exe. As you have demonstrated, it seems that cmd.exe may be very forgiving as @TAC109 suggested. But I cannot confirm this without testing.


(3)
Is %comspec% BETTER than cmd.exe.
As you were told in your separate post/thread on this question, Comspec may be the preferred method. I cannot confirm this because I have very little knowledge or experience with it. But the explanation made sense to me since the system uses this variable to point to the actual file path for cmd.exe. It's there for convenience and the environment variables were used a lot before Windows (in DOS days), and are still being used apparently. So it has not been abandoned, and points to a pretty safe bet to use it with confidence.
GEOVAN
Posts: 192
Joined: 03 Mar 2022, 11:12

Re: quote and double-quote " " "

06 Oct 2023, 16:39

I am really THANK YOU very very much for all your help!

My final conclusion is the following:

I will finally use CASE IV with %comspec%, as follows:

Code: Select all

Runwait, %comspec% /c " "%A_ScriptDir%\DATA 123\qpdf.exe" --empty --pages "%outtemp1%" -- "%out1%" "
WHY:
Because using CASE IV, covers ALL cases including the scenario where ANY path has spaces.
And also i will use %comspec% instead of cmd.exe because it is a better choice, since "covers" all the cases to ALWAYS do the "job" (even in the scenario where there is a change of the Path variable).

Please, if you see any reason that you believe i have a mistake regarding my above final conclusion, i will be very happy to comment here in order to correct my final conclusion.
Thanks!
User avatar
andymbody
Posts: 993
Joined: 02 Jul 2017, 23:47

Re: quote and double-quote " " "

06 Oct 2023, 16:57

GEOVAN wrote:
06 Oct 2023, 16:39
If you see any reason that you believe i have a mistake
Sounds like an educated choice to me.
TAC109
Posts: 1129
Joined: 02 Oct 2013, 19:41
Location: New Zealand

Re: quote and double-quote " " "

06 Oct 2023, 17:37

It is not clear from your code that it is necessary to run your program using either cmd.exe or %comspec% in the first instance. Have you tried this? In this case you would instead need to code the RunWait command like this:

Code: Select all

 Runwait, "%A_ScriptDir%\DATA 123\qpdf.exe" --empty --pages "%outtemp1%" -- "%out1%"
Cheers
My scripts:-
XRef - Produces Cross Reference lists for scripts
ReClip - A Text Reformatting and Clip Management utility
ScriptGuard - Protects Compiled Scripts from Decompilation
I also maintain Ahk2Exe
GEOVAN
Posts: 192
Joined: 03 Mar 2022, 11:12

Re: quote and double-quote " " "

06 Oct 2023, 18:22

TAC109 wrote:
06 Oct 2023, 17:37
It is not clear from your code that it is necessary to run your program using either cmd.exe or %comspec% in the first instance. Have you tried this? In this case you would instead need to code the RunWait command like this:

Code: Select all

 Runwait, "%A_ScriptDir%\DATA 123\qpdf.exe" --empty --pages "%outtemp1%" -- "%out1%"
Cheers
Unfortunately it is not working without using cmd.exe or %comspec%.

So, according to your comment
andymbody wrote:
GEOVAN wrote:
06 Oct 2023, 16:39
If you see any reason that you believe i have a mistake
Sounds like an educated choice to me.
Do you agree with my final conclusion that "covers" ALL situations, and it is good choice to use - that is CASE IV with %comspec% ?
User avatar
andymbody
Posts: 993
Joined: 02 Jul 2017, 23:47

Re: quote and double-quote " " "

06 Oct 2023, 18:26

GEOVAN wrote:
06 Oct 2023, 18:22
Do you agree with my final conclusion that "covers" ALL situations, and it is good choice to use - that is CASE IV with %comspec% ?
Yes

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: JKJadan and 250 guests