Pass string into cmd.exe as forced expression Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Pass string into cmd.exe as forced expression

26 Sep 2019, 17:22

It works:

Code: Select all

File := A_Desktop . "\test.html"
Run, %ComSpec% /c ""C:\Program Files\Google\Chrome\Application\chrome.exe" %File%",, Hide
How to write it as forced expression?

Here is what I tried:

Code: Select all

Run, % ComSpec . A_Space . "/c ""C:\Program Files\Google\Chrome\Application\chrome.exe"" . File",, Hide

Code: Select all

Run, % ComSpec . A_Space . "/c ""C:\Program Files\Google\Chrome\Application\chrome.exe""" . File,, Hide

Doesn't work, though.
Last edited by john_c on 26 Sep 2019, 18:32, edited 1 time in total.
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Pass string into cmd.exe as forced expression

26 Sep 2019, 17:31

Code: Select all

% comspec "/c """"pathchrome.exe"" " file """"
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: Pass string into cmd.exe as forced expression

26 Sep 2019, 17:40

@swagfag Hm, doesn't work for me
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Pass string into cmd.exe as forced expression

26 Sep 2019, 17:46

cant test it myself right now. if File contains spaces, it too would probably have to be enclosed in quotes, so u can try doing that
User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: Pass string into cmd.exe as forced expression

26 Sep 2019, 18:03

This works for me:

Code: Select all

file := "C:\Users\xxx\Projects\WebPages\longlist.html"
Runwait %ComSpec% /c ""C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" "%File%"",, Hide
and so does this:

Code: Select all

Runwait %ComSpec% /c ""C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" %File%",, Hide
see help file here: https://www.autohotkey.com/docs/commands/Run.htm
14.3 & 1.3.7
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: Pass string into cmd.exe as forced expression

26 Sep 2019, 18:22

@flyingDman Your second version is a copy of mine.

Your first version is a slight improvement over it, thanks.

However, I want to use the forced expression. Your examples doesn't use it.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Pass string into cmd.exe as forced expression  Topic is solved

26 Sep 2019, 18:35

I think swagfag's was right apart from missing a space before the /c.

Code: Select all

;legacy style:
Run, %ComSpec% /c ""C:\Program Files\Google\Chrome\Application\chrome.exe" %File%",, Hide
;expression style:
Run, % ComSpec " /c """"C:\Program Files\Google\Chrome\Application\chrome.exe"" " File """",, Hide
;more forwards compatible:
Run, % ComSpec " /c " Chr(34) Chr(34) "C:\Program Files\Google\Chrome\Application\chrome.exe" Chr(34) " " File Chr(34),, Hide

;legacy style:
Run, %ComSpec% /c ""C:\Program Files\Google\Chrome\Application\chrome.exe" "%File%"",, Hide
;expression style:
Run, % ComSpec " /c """"C:\Program Files\Google\Chrome\Application\chrome.exe"" """ File """""",, Hide
;more forwards compatible:
Run, % ComSpec " /c " Chr(34) Chr(34) "C:\Program Files\Google\Chrome\Application\chrome.exe" Chr(34) " " Chr(34) File Chr(34) Chr(34),, Hide
Last edited by jeeswg on 26 Sep 2019, 19:47, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: Pass string into cmd.exe as forced expression

26 Sep 2019, 18:39

What about this:?

Code: Select all

run % ComSpec /c """C:\Program Files (x86)\Google\Chrome\Application\chrome.exe""" """" File """" ,, Hide
Edit: had not seem @jeeswg 's response
Last edited by flyingDman on 26 Sep 2019, 18:42, edited 2 times in total.
14.3 & 1.3.7
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: Pass string into cmd.exe as forced expression

26 Sep 2019, 18:41

@jeeswg But they don't work either :D

At least for me. Win7, AHK 1.1.30.03

Edit: I was wrong, it works.
Last edited by john_c on 26 Sep 2019, 18:44, edited 1 time in total.
User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: Pass string into cmd.exe as forced expression

26 Sep 2019, 18:44

Note: my Chrome.exe is in Program Files (x86)
14.3 & 1.3.7
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: Pass string into cmd.exe as forced expression

26 Sep 2019, 19:02

@flyingDman thanks, works for me, for both spaced and not-spaced files:

Code: Select all

test.html
test test.html
It is strange, however, that /c is not enclosed in quote marks.

The @jeeswg version works for test.html but not for test test.html for now.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Pass string into cmd.exe as forced expression

26 Sep 2019, 19:48

I've updated my earlier post to handle "%File%".
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
john_c
Posts: 493
Joined: 05 May 2017, 13:19

Re: Pass string into cmd.exe as forced expression

26 Sep 2019, 20:40

@jeeswg Thanks, I changed the accepted answer to yours.

It seems that the most clear version should follow 2 rules:

* Always use A_Space instead of normal space in case the space is located close to the quote mark.
* Always use """" for escaped quote marks.

Thus, the result will be:

Code: Select all

Run, % ComSpec . A_Space . "/c" . A_Space . """" . """" . "C:\Program Files\Google\Chrome\Application\chrome.exe" . """" . A_Space . """" . File . """" . """"
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Pass string into cmd.exe as forced expression

27 Sep 2019, 17:38

I would use one of 3 approaches, that I list at the top of this thread:
AHK v2 and strings - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=37&t=36833
In this particular example, we are creating a command-line string within a command-line string, so for each approach, I create the command-line string via a two-stage process.

If I had to use an expression one-liner, I would do this:

Code: Select all

vDQ := Chr(34)
vPath := A_Desktop "\test.html"
vTarget := A_ComSpec " /c " vDQ vDQ "C:\Program Files\Google\Chrome\Application\chrome.exe" vDQ " " VDQ vPath vDQ vDQ
MsgBox, % vTarget
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: NinjoOnline and 240 guests