I am attempting to dynamically create a small XML file with a Batch Script
Stop what you are doing, scrap it. Batch script is not a tool you should use to create XML, and even though you can bang on it for long enough to make it work in a specific case, it still isn't the right tool to create XML files.
If you want ubiquitous, no configuration or special permissions needed, runs on every Windows machine XML creation, that can be done in VBScript, using an actual XML API (MSXML2).
For a ubiquitous, some configuration and permissions needed approach, you can turn to PowerShell.
I'll provide a code sample if you specify your desired output more closely.
Following up the OP's request in the comments, here is an exemplary setup using VBScript. The key point of course is not which tool to use, but to use a tool that has a semantic understanding of XML.
Base XML template, e.g. project_template.xml
:
<project outputDir="" baseDir="" xmlns="http://confuser.codeplex.com">
<rule pattern="true" preset="maximum" inherit="false" />
</project>
VBScript to fill it dynamically, e.g. project.vbs
:
Option Explicit
Const NODE_ELEMENT = 1
Const CONFUSER_NS = "http://confuser.codeplex.com"
Dim doc, moduleElem, args, arg
Set args = WScript.Arguments
Set doc = CreateObject("MSXML2.DOMDocument")
doc.async = False
doc.load "project_template.xml"
If doc.parseError.errorCode Then
WScript.Echo doc.parseError
WScript.Quit 1
End If
For Each arg In args.Named
doc.documentElement.setAttribute arg, args.Named(arg)
Next
For Each arg In args.Unnamed
Set moduleElem = doc.createNode(NODE_ELEMENT, "module", CONFUSER_NS)
moduleElem.setAttribute "path", arg
doc.documentElement.appendChild moduleElem
Next
Doc.save "project.xml"
Usage:
cscript /nologo project.vbs /outputDir:"xx1" /baseDir:"xx2" "xx3" "xx4" "xx5"
Output (saved as project.xml
)
<project outputDir="xx1" baseDir="xx2" xmlns="http://confuser.codeplex.com">
<rule pattern="true" preset="maximum" inherit="false"/>
<module path="xx3"/><module path="xx4"/><module path="xx5"/>
</project>