1

DOS doesn't seem to deal well with the "&" character in a string. I'm trying to parse url's, many of which have one or more &'s. If I do something like:

    set var = "http://finance.yahoo.com/mbview/threadview/?v=m&bn=2"
    echo %var%
    pause

I get

ECHO is off. Press any key to continue . . .

Removing the & character rectifies this, which would be a suitable work-around, but I need to do this in code. Is there any help for this?

shockley
  • 11
  • 1
  • 2

3 Answers3

2

You don't need to escape the ampersand when you use delayed expansion

setlocal EnableDelayedExpansion
set "var=http://finance.yahoo.com/mbview/threadview/?v=m&bn=2"
Echo !var!
jeb
  • 78,592
  • 17
  • 171
  • 225
  • 1
    +1, You beat me this time :-) I was trying to write a full explanation. – dbenham Mar 03 '13 at 12:53
  • that worked. very nice. Thought I was sunk. – shockley Mar 04 '13 at 05:23
  • However, while this line works right: echo !str! > my.txt this one loses everything after the '&', including the '&': echo !str! | clip – shockley Mar 04 '13 at 07:13
  • `echo !str! > my.txt` works, but `echo !str! | clip` fails, as pipes create a new cmd.exe context, so the line is parsed twice. For more you should open a new question – jeb Mar 04 '13 at 08:06
  • Oh -- so you gave me a solution that only works once! LOL Seems like this is similar to the byval/byref scenariio. – shockley Mar 04 '13 at 12:25
  • No, my solution works. You didn't ask about pipes, in batch this makes a huge difference – jeb Mar 04 '13 at 13:38
  • didn't mean to sound ungrateful. May take a look at one of these interpreters under the hood. regards. – shockley Mar 04 '13 at 17:07
1

If it is Windows Command line that must be what you are looking for.

set "var=http://finance.yahoo.com/mbview/threadview/?v=m^&bn=2"
echo %var%
pause

watch ^& in code

Mahmut Ali ÖZKURAN
  • 1,120
  • 2
  • 23
  • 28
1

Remove the space between var and =:

set var="http://finance.yahoo.com/mbview/threadview/?v=m&bn=2"
echo %var%
pause
Sergey Podobry
  • 7,101
  • 1
  • 41
  • 51
  • actually, I only typed it wrong in the question box -- actual code was typed as you have it. Thanx. – shockley Mar 04 '13 at 05:24