Batch Write to Text File with <Angle Brackets>

╄→гoц情女王★ 提交于 2020-01-14 18:51:13

问题


I am attempting to dynamically create a small XML file with a Batch Script, but am having issues writing lines that begin and end with angle brackets.

1) If I do something like:

set foo=^<bar^>
echo %foo% > test.txt

This results in

> was unexpected at this time.
echo <bar> > test.txt


2) If I surround the echo statment variable with quotes: echo "%foo%" > test.txt, it writes successfully to the text file. However it obviously includes the quotes which I can't have.


3) Then I thought "Well, it must just be the angle brackets at the beginning and end..." So I added a character before and after the angle brackets:

set foo=a^<bar^>a
echo %foo% > test.txt

This resulted in some weird output which looks like my brackets are being numbered, and then it's looking for a file?

echo a 0<bar 1>test.txt
The system cannot find the file specified.


I've written elementary batch scripts before, but feel like I'm in over my head here... Any help is appreciated!


回答1:


Try this:

setlocal ENABLEDELAYEDEXPANSION

set foo=^<bar^>
echo !foo! > test.txt

endlocal

Using delayed expansion, and replacing the % with ! causes it to evaluate it differently.




回答2:


If using a pipe you need:

set foo=^^^<bar^^^>



回答3:


You need to account for double substitution:

set foo=^^^<bar^^^>
echo %foo% > test.txt



回答4:


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>


来源:https://stackoverflow.com/questions/27846423/batch-write-to-text-file-with-angle-brackets

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!