问题描述
由于这个社区,我终于了解了如何在批处理的Delayed Expansion块中转义使用感叹号,以便立即使用.(使用两个逃脱插入符号,而不仅仅是一个,很棒)
Thanks to this community I have finally learned how to escape exlamation marks for immediate use in a batch delayedExpansion block.(use two escape carets not just one, awesome)
但是我似乎无法找到或弄清楚如何将包含感叹号作为参数的变量的内容传递给批处理子例程.
But I can't seem to find or figure out how to pass the contents of a variable containing an exclamation mark as parameter to a batch subroutine.
示例:
@echo off
setLocal EnableDelayedExpansion
set variable=Hello^^!
echo "!variable!"
call :subroutine "!variable:^^!=^^!!"
pause
exit
:subroutine
echo "%~1"
exit/b
输出:
"Hello!"
"Hello"
Press any key to continue . . .
我希望第二个"Hello"包括一个感叹号.我尝试了第5行的各种子串替换排列,但无济于事.
I want the second "Hello" to include an exclamation mark.I have tried various permutations of substring replacement on line 5 to no avail.
帮助
推荐答案
您需要一种不同的方式来替换变量,并且需要插入更多的符号.
You need a different way for the variable replacing, and much more carets.
@echo off
setLocal EnableDelayedExpansion
set variable=Hello^^!
echo "!variable!"
call :subroutine %variable:!=^^^^^^^^^^!%
exit /b
:subroutine
echo %~1
exit /b
或带有引号: 调用:subroutine%variable:!= ^^^!%"
Or with quotes: call :subroutine "%variable:!=^^^!%"
在您的函数中,您需要扩展%1
而不用任何引号,因为在CALL
参数中插入符号的数量始终为奇数.
In your function you need to expand %1
without any quotes, as the number of carets are always odd in a CALL
parameter.
但是尝试这样的事情绝对不是一个好主意.
我同意阿西尼(Aacini)的观点,您应该改为使用按引用传递.
这是处理任何可能内容的唯一方法.
But at all it's a bad idea to try such things.
I agree with Aacini, that you should use pass by reference instead.
This is the only way to handle any possible content.
@echo off
setLocal EnableDelayedExpansion
set variable=Hello^^!
echo "!variable!"
call :subroutine variable
exit /b
:subroutine
echo !%1!
exit /b
这篇关于在批处理子例程调用中将感叹号作为参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!