Batch writing to text file using <Angle Brackets>

I am trying to dynamically create a small XML file with a Script package, but I am having trouble writing lines that start and end with angle brackets.

1) If I do something like:

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

      

The result is

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

      


2) If I surround the stat echo variable with quotes:, echo "%foo%" > test.txt

it writes successfully to a text file. However, it obviously includes quotes, which I cannot have.


3) Then I thought, "Well, it should only be the angle brackets at the beginning and at the 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 that looks like my parentheses are numbered and then looking for a file?

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

      


I used to write starter scripts, but feel like I'm over my head here ... Any help is appreciated!

+3


source to share


4 answers


Try the following:

setlocal ENABLEDELAYEDEXPANSION

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

endlocal

      



Using slowed expansion and replacing %

with !

causes it to be evaluated differently.

+3


source


If you need a pipe:



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

      

+3


source


You need to consider double replacement:

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

      

+2


source


I am trying to dynamically create a small XML file with a Script package

Stop what you are doing, drop it. The script package is not a tool you should use to create XML, and even if you can lash out at it for a long time to get it to work in a particular case, it is still not the right tool for creating XML files.

If you want it everywhere, without any settings or special permissions, it is executed on every Windows machine XML creation, which can be executed in VBScript using the actual XML API (MSXML2).

For general use, some configuration and permissions are required, you can refer to PowerShell.

I'll provide some sample code if you can refine your desired output.


Following the OP's request in the comments, here's an example installation using VBScript. Of course, the key is not the tool to use, but the use of a tool that has a semantic understanding of XML.

Basic XML template eg. project_template.xml

:

<project outputDir="" baseDir="" xmlns="http://confuser.codeplex.com">
  <rule pattern="true" preset="maximum" inherit="false" />
</project> 

      

VBScript to fill it dynamically, eg. 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"

      

Using:

cscript /nologo project.vbs /outputDir:"xx1" /baseDir:"xx2" "xx3" "xx4" "xx5"

      

Exit (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>

      

+1


source







All Articles