1

I'm trying to write a batch file that can generate some XML config files, but escaping the < and > signs does not seem to work. At the start of my script i wrote a test:

SET test=^<foo^>^<bar what="cat"^>^</foo^>
ECHO %test%

The output looks like this:

Z:\mypath>SET test=<foo><bar what="cat"></foo>
< was unexpected at this time.

Z:\mypath>ECHO <foo><bar what="cat"></foo>

The rest of the script is not executed, as no of the latter ECHOs is fired.

What am I doing wrong? How to fix this? I'm working on Windows 8.1.

  • i would seriously recommend considering another scripting language. any other, really. – njzk2 Jul 30 '15 at 15:03
  • @njzk2 Yeah, I know. I did recommend other scripting languages, too. Sadly, that was not how things went. – K.L. Jul 30 '15 at 16:33
3

The escape is working, except you aren't escaping often enough.

With your code, you must escape twice: once for the SET command, and a second time for the ECHO. The code for the SET has double escapes. Ater execution, the value of test will contain the single escapes so that the ECHO works properly.

SET test=^^<foo^^>^^<bar what="cat"^^>^^</foo^^>
ECHO %test%

You can get by with single escapes if you ECHO the value directly:

ECHO test=^<foo^>^<bar what="cat"^>^</foo^>

Or if you use delayed expansion:

setlocal enableDelayedExpansion
SET test=^<foo^>^<bar what="cat"^>^</foo^>
ECHO !test!

You can eliminate all escapes if you enclose the assignment in quotes:

setlocal enableDelayedExpansion
SET "test=<foo><bar what="cat"></foo>"
ECHO !test!

Bear in mind that in the above example, "cat" is no longer quoted during the assignment, so you would have to escape any poison characters that might appear there.

If you have lots of text to write, then it might be worth using my PrintHere.bat utility that emulates the unix here document feature. Assuming you have PrintHere.bat in a folder that appears within your PATH, then you could use:

call PrintHere :test
<foo><bar what="cat"></foo>
:test
  • Are you sure about the last example? SET "test=<foo><bar what="cat"></foo>" followed by set test show the quotes are around cat -> test=<foo><bar what="cat"></foo> – DavidPostill Jul 30 '15 at 11:26
  • @DavidPostill - Yes, the quotes are there, but they do not protect the content during the assignment. For example: set "test= quoted " unquoted " quoted ". So the assignment would fail if "cat" were to be changed to "this&that". – dbenham Jul 30 '15 at 11:34
2

Escaping the < sign with ^ does not work in .bat file

You can get around this by quoting the whole of the set arguments:

SET "test=<foo><bar what="cat"></foo>"

This will set the value of test correctly:

F:\test>set test
test=<foo><bar what="cat"></foo>

You can echo the value of test by quoting it:

F:\test>echo "%test%"
"<foo><bar what="cat"></foo>"

If you echo test without the quotes you will get an error:

F:\test>echo %test%
< was unexpected at this time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.