3

This is continuation on question How do I escape ampersands in batch files?.

That question presents some ways to use ampersands in batch files. However, it would seem none of those work for function parameters. Example:

@echo off
setlocal EnableDelayedExpansion

call:myFunction "http://www.google.com/search?client=opera&q=escape+ampersand"

goto:eof

:myFunction
echo Param is: %~1
goto:eof

I would always get

Param is: http://www.google.com/search?client=opera
'q' is not recognized as an internal or external command,
operable program or batch file.

I've tried ^ to escape it and that doesn't seem to work either. Is there a way?

If it matters, my actual use case is supplying a download URL to wget which is called within a batch function.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
eis
  • 51,991
  • 13
  • 150
  • 199
  • I am also having the same problem, will apply your solution now and see. – Haris ur Rehman Aug 12 '15 at 17:44
  • @MichaelFreidgeim isn't it already explained in the beginning of the question why this isn't a duplicate? None of the answers there work by themselves - what was needed was quotes *inside the function as well*, not just in the original call. – eis Sep 05 '16 at 06:05
  • Sorry, didn't read question properly – Michael Freidgeim Sep 05 '16 at 09:26

2 Answers2

2

Feeling a bit silly now...

@echo off
setlocal EnableDelayedExpansion

call:myFunction "http://www.google.com/search?client=opera&q=escape+ampersand"

goto:eof

:myFunction
echo Parameter is: "%~1"
goto:eof

Result:

Parameter is: "http://www.google.com/search?client=opera&q=escape+ampersand"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
eis
  • 51,991
  • 13
  • 150
  • 199
1

^& is the correct way to escape, but it isn't usually needed in double quotes.

Source: Escape Characters

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Patrick Meinecke
  • 3,963
  • 2
  • 18
  • 26