batch file write <> to text file

2019-05-11 21:08发布

问题:

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