I want to store a URL prefix in an Windows environment variable. The ampersands in the query string makes this troublesome though.
For example: I have a URL prefix of "http://example.com?foo=1&bar=", and want to create a full URL by providing a value for the bar param. I then want to launch that url using the "start" command.
Adding quotes around the value for the SET operation is easy enough:
set myvar="http://example.com?foo=1&bar="
Windows includes the quotes in the actual value though (thanks Windows!):
echo %myvar%
"http://example.com?foo=1&bar=true"
I know that I can strip quotes away from batch file arguments by using tilde:
echo %~1
However, I can't seem to do it to named variables:
echo %~myvar%
%~myvar%
What's the syntax for accomplishing this?
-
I think this should do it:
for /f "tokens=*" %i in (%myvar%) do set %myvar%=%~i
But you do not need this,
set myvar="http://example.com?foo=1&bar=" start "" %myvar%
Will work too, you just need to supply a title to the start command.
Craig Walker : The start command doesn't work. The ampersand in the URL terminates the line, thus dropping the last URL param. It launches the browser but it's the wrong URL. -
This is not a limitation of the environment variable, but rather the command shell.
Enclose the entire assignment in quotes:
set "myvar=http://example.com?foo=1&bar="
Though if you try to echo this, it will complain as the shell will see a break in there.
You can echo it by enclosing the var name in quotes:
echo "%myvar%"
Or better, just use the set command to view the contents:
set myvar
-
echo %myvar:"=%
-
Use delayed environment variable expansion and use !var:~1,-1! to remove the quotes:
@echo off setlocal enabledelayedexpansion set myvar="http://example.com?foo=1&bar=" set myvarWithoutQuotes=!myvar:~1,-1! echo !myvarWithoutQuotes!
0 comments:
Post a Comment