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.

3
  • I am also having the same problem, will apply your solution now and see. Commented Aug 12, 2015 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. Commented Sep 5, 2016 at 6:05
  • Sorry, didn't read question properly Commented Sep 5, 2016 at 9:26

2 Answers 2

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"
Sign up to request clarification or add additional context in comments.

3 Comments

what did you change?
@SketchBookGames added quotes to my "Param is" printing
What if you don't want the double quotes printed, though?
1

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

Source: Escape Characters

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.