英文:
How to escape / pass the % character to the Sub
问题
I have this batch file, where I am trying to extract a substring but in the end result the %
character is being removed. Eg some%value
becomes somevalue
. I have escaped the &
character but nothing works for the percent character.
setlocal enabledelayedexpansion
For /F "Delims=" %%A In ('FindStr /I "<start>.*</end>" "AllApiLogs.log"') Do (
Call :Sub "%%A"
)
GoTo :EOF
:Sub
Set "Line=%~1"
Set "Line=!Line:&amp;=^&!"
Set "Up2Sub=!Line:*<start>=!"
Set "SubStr=!Up2Sub:</end>=&:&!"
Echo '!SubStr!', >> ApiRegExExtract.log
Exit /B
How do I work around this?
Is there a way to escape all characters that need escaping in batch files? I am new to these.
英文:
I have this batch file, where I am trying to extract a substring but in the end result the %
character is being removed. Eg some%value
becomes somevalue
. I have escaped the &amp;
character but nothing works for the percent character.
setlocal enabledelayedexpansion
For /F "Delims=" %%A In ('FindStr /I "<start>.*</end>" "AllApiLogs.log"') Do (
Call :Sub "%%A"
)
GoTo :EOF
:Sub
Set "Line=%~1"
Set "Line=%Line:&amp;=^&%"
Set "Up2Sub=%Line:*<start>=%"
Set "SubStr=%Up2Sub:</end>="&:"%"
Echo '%SubStr%', >> ApiRegExExtract.log
Exit /B
How do I work around this?
Is there a way to escape all characters that need escaping in batch files? I am new to these.
答案1
得分: 1
你可以使用插入符号(caret)来转义百分号(%)。
通过将百分号(%)重复两次来转义它,因此要表示百分号(%),请使用%%来转义此字符。
英文:
You can not escape a % with a caret.
You can escape a % by doubling it, so for % use a % to escape this character.
答案2
得分: 1
在你的情况下,通过使用Call :Sub "%%A"
,你会失去百分号。
只需更改为:
For /F "Delims=" %%A In ('FindStr /I "<start>.*</end>" "AllApiLogs.log"') Do (
set "line=%%A"
Call :Sub
)
GoTo :EOF
:Sub
Set "Line=%Line:&=^&%..."
这是CALL
命令的效果,它解析参数两次。
英文:
In your case, you lose the percent sign by using Call :Sub "%%A"
.
Just change that to:
For /F "Delims=" %%A In ('FindStr /I "<start>.*</end>" "AllApiLogs.log"') Do (
set "line=%%A"
Call :Sub
)
GoTo :EOF
:Sub
Set "Line=%Line:&amp;=^&%"
...
It's an effect of the CALL
command, it parses the arguments two times
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论