使用<尖括号>批量写入文本文件; [英] Batch Write to Text File with <Angle Brackets>
问题描述
我正在尝试使用批处理脚本动态创建一个小的XML文件,但是在编写以尖括号开头和结尾的行时遇到了问题.
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)如果我执行以下操作:
1) If I do something like:
set foo=^<bar^>
echo %foo% > test.txt
这将导致
> was unexpected at this time.
echo <bar> > test.txt
2)如果在echo mentment变量周围加上引号:echo "%foo%" > 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)然后我想:好吧,它必须只是尖括号在开头和结尾..."所以我在尖括号之前和之后添加了一个字符:
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!
推荐答案
尝试一下:
setlocal ENABLEDELAYEDEXPANSION
set foo=^<bar^>
echo !foo! > test.txt
endlocal
使用延迟扩展,然后将%
替换为!
会导致对它的评估有所不同.
Using delayed expansion, and replacing the %
with !
causes it to evaluate it differently.
这篇关于使用<尖括号>批量写入文本文件;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!