2

I am trying to get a variable, put that in between two set strings and finally open the concatenated string in Chrome. There is, however, part of the string that I am having issues with which I am presuming is due to the usage of special characters.

@echo off set loc=%1 set link1=https://query.yahooapis.com/v1/public/yql?q=select * from geo.places where text=' set link2='&format=json set linkdone=%link1%%loc%%link2% "chrome.exe" "%linkdone%" 

link2 ('&format=json) is giving issues here, when I echo after setting it. It is an empty string and the batch goes on to echo invalid drive specification which is due to the erroneous reading of link2 I would assume.

I have tried escaping it as ^'^&format^=json but it does not seem to work. The rest of the code (notably link1 without escapechars) seems to work fine.

2
  • 1
    There is no escaping necessary if you are following the following syntax: set "VAR=value" (note the quotes); since the final command line also contains the variable value quoted, there is still no need for escaping; special characters lose their particular meaning when placed within ""; finally, instead of %1 you should state %~1... Commented Nov 22, 2016 at 15:08
  • Not sure whether you have to %-escape some characters within the query select * from geo.places where text= for the browser, like %20 for space, or %3D for =, for example... Commented Nov 22, 2016 at 15:16

3 Answers 3

2

To properly quote the argument to set you have to quote the entire argument, this includes the variable name:

set "link2='&format=json" 

Note also that normal variable expansion is simple text substitution while a command is parsed, so you will have to properly escape every occurrence of that variable afterwards, such as

set "linkdone=%link1%%loc%%link2%" 

In your case I'd probably opt for something a lot simpler, though:

@chrome "https://query.yahooapis.com/v1/public/yql?q=select * from geo.places where text='%1'&format=json" 

No need to fiddle with variables when unsure how to escape and quote in this variant.

Sign up to request clarification or add additional context in comments.

2 Comments

Cheers mate, the last one worked perfectly! I'm not too familiar with batch commands yet so I wasn't sure whether pasting a variable in a string would cause issues.
Causes a lot fewer issues than what you tried. It can be made to work, but unless you actually need the pieces for something else I wouldn't usually bother.
0

Escaping the & in a set command has failed for me and I confess I don't know why

So I propose a workaround for this. Use __amp__ to replace & in the set. Replace it by the actual & afterwards (here escaping works!)

@echo off set loc=%1 set link1=https://query.yahooapis.com/v1/public/yql?q=select * from geo.places where text=' set link2='__amp__format=json set linkdone=%link1%%loc%%link2% echo %linkdone:__amp__=^&% 

result with the_loc as a parameter:

https://query.yahooapis.com/v1/public/yql?q=select * from geo.places where text='the_loc'&format=json 

(BTW no need to escape single quote, which has no special meaning in batch)

Comments

0
set "link2='^&format=json" 

should set link2 as you require (you don't say...)

With an argument of hello, echoing the "chrome" line produces

"chrome.exe" "https://query.yahooapis.com/v1/public/yql?q=select * from geo.places where text='hello'&format=json" 

for me - and produces no error report.

4 Comments

You don't need the ^ in there.
@Joey : Umm, perhaps you should try it...fails without the caret for me (win 10)
Same error message, tried putting quotation marks around link1 too, no difference.
I pasted the code directly from my test. For me, it generates no error. Perhaps you are using a different OS? Are you cutting-and-pasting the code? Which editor are you using (A WP or Notepad have been known to produce strange errors as the application tries to "format" the code)?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.