1

I am trying to output the following line to an xml file.

I am using the below command:

echo "<?xml version="1.0" encoding="utf-8"?>" >> MyFile.xml

The only issue is that it is including the quotation marks at the begining and end in the file so inside of my file it looks like this:

"<?xml version="1.0" encoding="utf-8"?>"

Instead of this:

<?xml version="1.0" encoding="utf-8"?>

But if I try to run the batch file without the first and last quotes it fails and says >> was unexpected at this time.

So... those quotation marks are needed for it to work but it then prints those marks... Is there any way to do this?

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Given `cmd` and `batch-file` -- this is on Windows, right? – Charles Duffy Sep 28 '18 at 22:00
  • 3
  • [Generating html with batch .. escape quotes](https://stackoverflow.com/q/7942330/995714), [using batch echo with special characters](https://stackoverflow.com/q/7308586/995714), [Windows Batch System Info to HTML](https://stackoverflow.com/q/22029285/995714), [batch file write <> to text file](https://stackoverflow.com/q/11407438/995714), [How to write an XML tag with double quotes using command prompt echo command](https://stackoverflow.com/q/33015273/995714) – phuclv Sep 29 '18 at 03:05
  • It would probably be better if you used a scripting language that has native support to reading and writing XML files. – Squashman Sep 29 '18 at 04:00

2 Answers2

0

I assume you are using windows, so try this:

echo ^<?xml version="1.0" encoding="utf-8"?^> >> MyFile.xml
dong bao
  • 16
  • 1
0

One possible workaround is to use delayed expansion:

@echo off
setlocal enableDelayedExpansion
set "xml_line=<?xml version="1.0" encoding="utf-8"?>"
echo !xml_line!
:::
endlocal
npocmaka
  • 55,367
  • 18
  • 148
  • 187