SomeDude
SomeDude

Reputation: 14228

How to find strings with xml pattern in batch file?

This question is about finding a string in a string that is xml-like. Batch will have problems with characters like "<", but thats not the end, my text also has "&" echo %line% with an & in it throws me a "is not recognized as an external command" I am finding it hard to find an answer for this. I have a text file that contains an xml string like :

<ExampleTag1><error>Error message</error></ExampleTag1>

I wanted to use

set "line=<ExampleTag1><error>Error message</error></ExampleTag1>"
echo %line% | findstr /C:"ExampleTag1"

but its showing me error : "< was unexpected at this time"

I believe this is because %line% has "<" in it.

or actually is there a way to echo xml strings in batch?

Just echo %line% will show me the above error.

Can you guys help?

UPDATE : Actually I made a choice not to use batch to parse my text.

Upvotes: 0

Views: 1137

Answers (1)

Magoo
Magoo

Reputation: 79983

@ECHO OFF
SETLOCAL
set "line=<ExampleTag1><error>Error message</error></ExampleTag"
echo "%line%" | findstr /C:"ExampleTag1"
ECHO %errorlevel%
GOTO :EOF

...assuming you want to set/examine errorlevel

Upvotes: 1

Related Questions