1

I have a plain text file with one url per line, enclosed with <link></link> tags. ECHO-ing the variablee (including the tags) works fine but now I'd like to remove the tags. escaping the angle brackets with one or multiple ^ does not work. here's the code

FOR /F "tokens=* USEBACKQ" %%F IN (`findstr "<link>" test.txt`) DO (
SET what=%%F
SET result=%what:<link>=%
ECHO %result%
)

is there another way of doing it?

Community
  • 1
  • 1
mihau
  • 35
  • 4

1 Answers1

1

You need delayed expansion and quotes when you are using > or <:

@echo off
setlocal enableDelayedExpansion
FOR /F "tokens=* USEBACKQ" %%F IN (`findstr "<link>" test.txt`) DO (
    SET "what=%%F"
    SET "result=!what:<link>=!"
    ECHO !result!
)
endlocal
JosefZ
  • 22,747
  • 4
  • 38
  • 62
npocmaka
  • 51,748
  • 17
  • 123
  • 166
  • @JosefZ: Not the case. Reaching EOF is equivalent to an `endlocal`. – Magoo Jan 24 '15 at 02:12
  • Not the case. You need delayed expansion here because the variables in use are within a block. The quotes serve only to ensure trailing spaces are not included in the value assigned. – Magoo Jan 24 '15 at 02:14
  • @Magoo yes, but I do not see an `EOF`, I don't see whether or not the OP's code snippet is embedded in some outer loop etc... Let's go to _meta_ with it... – JosefZ Jan 24 '15 at 02:50
  • @JosefZ: Did you removed some comments here? Please, _don't_! Remember that this is a public forum and any other user that read these comments (like me) will have not idea of what you and Magoo are talking about... `:(` – Aacini Jan 24 '15 at 22:20
  • @Aacini Sorry, I did not remove anything; see [my edit Summary](http://stackoverflow.com/posts/28120693/revisions). It states literally: _'setlocal' should be always coupled with corresponding 'endlocal' as a kind of parentheses_ – JosefZ Jan 24 '15 at 23:23