59

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 parameter. 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?

Thomas Weller
  • 55,411
  • 20
  • 125
  • 222
Craig Walker
  • 49,871
  • 54
  • 152
  • 212
  • 12
    Rarely, a question alone helps me solve my current problem (how to strip quotes from a batch file argument). Here, it did. Thanks and +1 :-) – Jan Feb 22 '12 at 14:47

9 Answers9

39

echo %myvar:"=%

35

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
zdan
  • 28,667
  • 7
  • 60
  • 71
  • Should this work with SET /P? I try SET /P "var=question". var is then equal to "my response with spaces" – RoboJ1M Nov 08 '12 at 16:09
11

While there are several good answers already, another way to remove quotes is to use a simple subroutine:

:unquote
  set %1=%~2
  goto :EOF

Here's a complete usage example:

@echo off
setlocal ENABLEDELAYEDEXPANSION ENABLEEXTENSIONS

set words="Two words"
call :unquote words %words%
echo %words%

set quoted="Now is the time"
call :unquote unquoted %quoted%
echo %unquoted%

set word=NoQuoteTest
call :unquote word %word%
echo %word%

goto :EOF

:unquote
  set %1=%~2
  goto :EOF
jimhark
  • 4,938
  • 2
  • 27
  • 28
  • Glad someone else thought of this, though, thinking deeper -- what if the variable doesn't have quotes? would be nice to have an early abort check, rather than just truncating the existing var... – BrainSlugs83 Apr 03 '14 at 21:45
  • %~2 removes any surrounding quotes. If there are no quotes it doesn't truncate. – jimhark Apr 04 '14 at 16:21
  • What's the `rem set %1=%~2` for? It looks like a commented out duplicate of the next line. Also, if the contents of the variable are not quoted and contain a space, this seems to truncate everything after the first space. Unless I've done something wrong, that would seem to make this unsuitable for dealing with file names. – jpmc26 May 13 '14 at 21:32
  • @jpmc26, that was a meaningless comment left in by mistake. I've removed it. Thanks for bringing this to my attention. – jimhark May 13 '14 at 21:51
9

This works

for %a in (%myvar%) do set myvar=%~a

I would also use this if I wanted to print a variable that contained and ampersand without the quotes.

for %a in ("fish & chips") do echo %~a
Chris
  • 91
  • 1
  • 1
3

To remove only beginning and ending quotes from a variable:

SET myvar=###%myvar%###
SET myvar=%myvar:"###=%
SET myvar=%myvar:###"=%
SET myvar=%myvar:###=%

This assumes you don't have a ###" or "### inside your value, and does not work if the variable is NULL.

Credit goes to http://ss64.com/nt/syntax-esc.html for this method.

Keith
  • 924
  • 6
  • 14
2

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!
Christian d'Heureuse
  • 5,090
  • 1
  • 32
  • 28
1

Use multiple variables to do it:

set myvar="http://example.com?foo=1&bar="

set bar=true

set launch=%testvar:,-1%%bar%"

start iexplore %launch%
axel22
  • 32,045
  • 9
  • 125
  • 137
CmdCrazy
  • 11
  • 2
0
@echo off
set "myvar=http://example.com?foo=1&bar="
setlocal EnableDelayedExpansion
echo !myvar!

This is because the variable contains special shell characters.

Amr Ali
  • 3,020
  • 1
  • 16
  • 11
-1

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.

wimh
  • 15,072
  • 6
  • 47
  • 98
  • 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. – Craig Walker Nov 20 '08 at 23:44