使用enableDelayedExpansion无法在批处理文件中的引号内转义分号(Can't escape semicolon inside quotes in batch file us

系统教程 行业动态 更新时间:2024-06-14 17:03:54
使用enableDelayedExpansion无法在批处理文件中的引号内转义分号(Can't escape semicolon inside quotes in batch file using enableDelayedExpansion)

我正在通过批处理操作一些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.html

file.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.html

file.html contains:

<tag1> <tag"bla;2">

After running the script, output.html contains:

<tag1> <tag"bla

I'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 :eof

Within 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.

更多推荐

本文发布于:2023-04-24 14:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/d67c4db7f100462076197de2fdce9a83.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:分号   引号   批处理文件   enableDelayedExpansion   escape

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!