0

I hope this is an appropriate use of the valuable resource that is this web-site. I understand using % as batch file escape code but can't seem to work through the many examples like this:

[Use %2* in string variable in Windows batch file

They all seem unclear to me. I am looking for a clear, crisp explanation of when these codes are, and are not needed. Obviously I am an amateur programmer. I think the below provides a good example to work with.

I am trying to launch Google searches like this one, from a Win 7 batch file but can't get the params correct. The idea of course is to have it (or something like it) run for different users for instance from a logon script. Any help is appreciated.

Here is a URL that works:

START "" "https://www.google.com/search?num=30&newwindow=1&safe=off&hl=en&biw=1254&bih=661&tbs=qdr%3Am%2Csbd%3A1&q=wschloss+-schloss+-bucci&oq=wschloss+-schloss+-bucci&gs_l=serp.3...44549.47736.0.48642.7.7.0.0.0.0.72.377.7.7.0....0...1c.1.64.serp..7.0.0.Gdz2I8khqmc"

I am trying to do something like this:

START "" "https://www.google.com/search?num=30&newwindow=1&safe=off&hl=en&biw=1254&bih=661&tbs=qdr%3Am%2Csbd%3A1&q=%%USERNAME%%+-schloss+-bucci&oq=%%USERNAME%%+-schloss+-bucci&gs_l=serp.3...44549.47736.0.48642.7.7.0.0.0.0.72.377.7.7.0....0...1c.1.64.serp..7.0.0.Gdz2I8khqmc"

Where, of course, USERNAME is a Windows environment variable

Community
  • 1
  • 1
wschloss
  • 15
  • 1
  • 5

1 Answers1

1

%variablename% expands the variable contents so %USERNAME% should have single %, conversely you'll have to escape the single % in the search query by repeating them (%%):

start "" "https://www.google.com/search?num=30&newwindow=1&safe=off&hl=en&biw=1254&bih=661&tbs=qdr%%3Am%%2Csbd%%3A1&q=%USERNAME%+-schloss+-bucci&oq=%USERNAME%+-schloss+-bucci&gs_l=serp.3...44549.47736.0.48642.7.7.0.0.0.0.72.377.7.7.0....0...1c.1.64.serp..7.0.0.Gdz2I8khqmc"
wOxxOm
  • 65,848
  • 11
  • 132
  • 136
  • To add: There's 2 independent scopes/contexts where % has meaning: 1) To delineate batch variables. 2) To escape special characters in URL's. You need to ensure that your batch variables expand correctly first of all, and secondly that any %'s that need to remain in the URL after the batch processor is finished (for the browser etc to use) is still there and isn't also interpreted or eaten by the batch processor. This means basically that you double up the %'s in the string to "escape" them so the batch processor will output them. – W.Prins Jul 14 '15 at 22:17
  • Thanks wOxxOm that makes sense, but how exactly are "batch variables" defined? Same question "special characters" I.e. which characters in my URL have to be treated that way, and how do I know exactly where each string, that needs to be escaped, starts and ends? – wschloss Jul 14 '15 at 22:27
  • Hmm,maybe this is not the way to do this--seems too convoluted. Could I achieve this perhaps with webget,exe, or perhaps with a Google API? I know just enough about systems to know what an API is, but not how to use. I am very good with Excel and if anyone knows where I could find a good table that lays out the "batch variables" and "special characters" question from my last comment, I could probably build a formula to create the correct query. I have done something similar with Google search engines. VLookup(),subst(), code() char() are my friends. – wschloss Jul 14 '15 at 22:32
  • 1. USERNAME is defined by Windows OS, and user-defined batch-variables are defined by `set VARIABLE=value`. 2. Except for the mentioned escaping of a single `%` as `%%` you don't have to escape anything as long as you use doublequotes around your url. – wOxxOm Jul 14 '15 at 22:34
  • But if that's the case then the second URL I posted above should have worked, no? – wschloss Jul 14 '15 at 22:38
  • No, because you've inverted escaping: `%%` for variables and `%` for the search query. – wOxxOm Jul 15 '15 at 10:12