我正在通过批处理操作一些HTML。 我正在使用延迟扩展但是当在双引号内遇到分号时,脚本无法复制除分号后的任何内容。
这可能是因为当我将变量传递给putLineInHTMLFile标签时我使用双引号(我需要在标签中保持事物分开)。
@echo off setlocal enableDelayedExpansion del output.html for /f "delims=" %%x in (file.html) do call :putLineInHTMLFile "%%x" goto :EOF :putLineInHTMLFile set "line=%~1" echo !line!>> output.htmlfile.html包含:
<tag1> <tag"bla;2">运行脚本后,output.html包含:
<tag1> <tag"bla我已经尝试用^或^^转义分号。 没工作。 我也试过逃避双引号。 那也行不通。
只要我可以在输出文件中包含分号,我就可以更改file.html的内容。
I'm manipulating some HTML via Batch. I'm using delayed expansion but when a semicolon is encountered inside double quotes, the script fails to copy anything after and including the semicolon.
It's probably due to the fact that I use double quotes when I pass the variable to the putLineInHTMLFile label (I need to keep things separated in labels).
@echo off setlocal enableDelayedExpansion del output.html for /f "delims=" %%x in (file.html) do call :putLineInHTMLFile "%%x" goto :EOF :putLineInHTMLFile set "line=%~1" echo !line!>> output.htmlfile.html contains:
<tag1> <tag"bla;2">After running the script, output.html contains:
<tag1> <tag"blaI've tried escaping the semicolon with ^ or ^^. Didn't work. I've tried escaping the double quotes too. That didn't work either.
I can change the contents of file.html anyway I please just as long as I can include that semicolon in the output file.
最满意答案
这似乎适用于给出的测试用例; 无法保证更广泛的使用:
@echo off setlocal enableDelayedExpansion del output.html for /f "delims=# tokens=*" %%x in (file.html) do ( set "safe=%%x" set "safe=!safe:"=""!" call :putLineInHTMLFile "!safe!" ) goto :eof :putLineInHTMLFile set "line=%~1" set "line=%line:""="%" echo !line!>> output.html :eof在for命令的“body”中, %%x尚未拆分,只有在call命令处理时才会发生这种情况。 为了保护它,我使用safe来加倍字符串中的所有双引号,然后在子例程中添加一行以再次将它们去掉。
如果双引号不匹配,这将无法正常工作,但在这些情况下,即使没有分号存在,也不会回显尾随> 。
This seems to work for the test-case given; no guarantees for wider use:
@echo off setlocal enableDelayedExpansion del output.html for /f "delims=# tokens=*" %%x in (file.html) do ( set "safe=%%x" set "safe=!safe:"=""!" call :putLineInHTMLFile "!safe!" ) goto :eof :putLineInHTMLFile set "line=%~1" set "line=%line:""="%" echo !line!>> output.html :eofWithin the "body" of the for command, the %%x has not been split, it's only when processed by the call command that this happens. To protect that, I've used safe to double-up all double-quotes in the string, and then added a line in the subroutine to strip them out again.
This doesn't work properly if the double-quotes aren't matched, but in those cases, neither does the echoing of the trailing >, even when there are no semi-colons present.
更多推荐
发布评论