我正在尝试使用Windows批处理文件用HTML格式的文本替换HTML文档中的某些占位符标记。批处理工作正常,但它在替换文本的开头包含引号。
这是我到目前为止所做的:
@echo off
setlocal DisableDelayedExpansion
set INTEXTFILE=test.txt
set OUTTEXTFILE=test_out.txt
set SEARCHTEXT=PLACEHOLDER
set REPLACETEXT="This is the replaced <a href="http://www.website.com" target="window">text</a> at its head and tail%"~1,-1%
set OUTPUTLINE=
for /f "tokens=1,* delims=¶" %%A in ( 'type %INTEXTFILE%') do (
SET string=%%A
setlocal EnableDelayedExpansion
SET modified=!string:%SEARCHTEXT%=%REPLACETEXT%!
set variable=!variable:~1,-4!
)
>> %OUTTEXTFILE% echo(!modified!
endlocal
)
del %INTEXTFILE%
rename %OUTTEXTFILE% %INTEXTFILE%
输出如下:
&#34;这是头部和尾部的替换文本
有关如何在不影响HTML格式的情况下删除输出文本中的第一个引号的任何想法?谢谢!
答案 0 :(得分:1)
未经测试,但这是基本想法
@echo off
setlocal DisableDelayedExpansion
set "INTEXTFILE=test.txt"
set "OUTTEXTFILE=test_out.txt"
set "SEARCHTEXT=PLACEHOLDER"
set "REPLACETEXT=This is the replaced <a href="http://www.website.com" target="window">text</a> at its head and tail"
set OUTPUTLINE=
for /f "tokens=1,* delims=" %%A in ( 'type "%INTEXTFILE%"') do (
SET "string=%%A"
setlocal EnableDelayedExpansion
SET "modified=!string:%SEARCHTEXT%=%REPLACETEXT%!"
>> "%OUTTEXTFILE%" echo(!modified!
endlocal
)
del "%INTEXTFILE%"
rename "%OUTTEXTFILE%" "%INTEXTFILE%"