batch file write <> to text file

南笙酒味 提交于 2019-12-07 16:08:29

问题


This is my first time doing these things. Basically I am creating a batch file html maker for church songs. This is what I am stuck with:

echo <br> %var17% >>Runfiles\temporary2.txt

Basically var 17 is a line of lyric and I want to add a line break in front of it. I have seperated the template maker into three parts:

  • first half
  • lyrics
  • second half

Here is the code:

@echo off
copy "I:\Build\Files\*.txt"

ren *.txt temp.txt

setlocal ENABLEDELAYEDEXPANSION
set vidx=0
for /F "tokens=*" %%A in (temp.txt) do (
SET /A vidx=!vidx! + 1
set var!vidx!=%%A
)
set var

echo <br> %var17% >>Runfiles\temporary2.txt

TYPE Runfiles\temp1.txt > run.html  
TYPE temp.txt >> run.html
TYPE Runfiles\temp2.txt >> run.html
pause

Temp 1 is the beginning and temp2 is the end, so I have a lyric document and I want to insert a <br> element in front of all of them.

The reason is because I have somewhat 100 songs to process and it takes so long to do it manually.


回答1:


echo ^<br^>

Circumflex is the escape character in DOS/Win command line.




回答2:


SilverbackNet has the correct answer for escaping special characters like < and >. But it might be more convenient to establish a variable containing the HTML tag. Special characters do not need to be escaped if they are quoted or expanded using delayed expansion. The definition uses quotes, and the expansion uses delayed expansion.

Using the variable only becomes an advantage when you need to write the tag in multiple places.

set "br=<br>"
....
echo !br! %var17% >>Runfiles\temporary2.txt


来源:https://stackoverflow.com/questions/11407438/batch-file-write-to-text-file

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