views:

1784

answers:

3

How do I escape ampersands in a batch file (or from the Windows command line) in order to use the start command to open web pages with ampersands in the URL?

Double quotes will not work with start; this starts a new command line window instead.

Update 1: Wael Dalloul's solution works. In addition, if there are URL encoded characters (e.g. space is encoded as %20) in the URL and it is in a batch file then '%' must be encoded as '%%'. This is not the case in the example.

Example, from the command line (CMD.EXE):

start http://www.google.com/search?client=opera&rls=en&q=escape+ampersand&sourceid=opera&ie=utf-8&oe=utf-8

will result in

http://www.google.com/search?client=opera

being opened in the default browser and these errors in the command line window:

'rls' is not recognized as an internal or external command,
operable program or batch file.
'q' is not recognized as an internal or external command,
operable program or batch file.
'sourceid' is not recognized as an internal or external command,
operable program or batch file.
'ie' is not recognized as an internal or external command,
operable program or batch file.
'oe' is not recognized as an internal or external command,
operable program or batch file.

Platform: Windows XP 64 bit SP2.

+2  A: 

Have you tried enclosing the url in quotes?

Note that you need to supply a dummy first argument in this case, as start will treat the first argument as a title for the new console windows, if it is quoted. So the following should work (and does here):

start "" "http://www.google.com/search?client=opera&rls=en&q=escape+ampersand&sourceid=opera&ie=utf-8&oe=utf-8"
belugabob
Yes, see the second paragraph in the question.
Peter Mortensen
Doh - need to read the questions better!
belugabob
OK, I will admit the title does not completely match the question inside. (Do you think I should make the title longer?)
Peter Mortensen
Not so 'Doh', after all - enclosing in quotes was the right idea - just didn't know about the console window title thing.Thanks Johannes!
belugabob
+1  A: 
start "http://www.google.com/search?client=opera&rls=...."

EDIT:

explorer "http://www.google.com/search?client=opera&rls=...."
adatapost
No, see the second paragraph in the question. A new command line window is opened instead.
Peter Mortensen
Could it be platform dependent? I am using Windows XP - I will add this information to the question.
Peter Mortensen
+8  A: 

'&' is used to separate commands therefore you can use ^ to escape the '&'

Wael Dalloul
That works. Thanks!
Peter Mortensen