问题描述
我有以下条纹:
@ECHO OFF
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
SET /A countArgs=1
...
SET /A countArgs+=1
CALL :error "!countArgs!. Argument ^-^> bla"
EXIT /B 1
...
:error
ECHO ERROR
ECHO %~1
EXIT /B 0
但是:error
例程echo
中的2. ECHO
行什么也没有.当我将CALL
参数字符串减小为"!countArgs!. Argument ^-^>"
时,我会遇到语法错误,当我将其减小为"!countArgs!. Argument ^-"
甚至是"!countArgs!. Argument -"
时,它将正常工作.
But the 2. ECHO
-line in the :error
routine echo
s nothing. When I reduce the CALL
argument string to "!countArgs!. Argument ^-^>"
i get a syntax error and when i reduce it to "!countArgs!. Argument ^-"
or even "!countArgs!. Argument -"
it works properly.
根据此帖子,如果添加^
的字符位于引号内,则该字符应转义因为在:error
例程中将字符串用作参数时,~
会删除包围的引号...
According to this post the character should be escaped when adding a ^
if it is inside quotes which makes sense because when using the string as a parameter in the :error
routine the ~
removes surrounded quotes...
我该如何解决?
感谢您的帮助.
推荐答案
无需使用call
转义>
.由于周围的引号是安全的.在子例程中回显该错误时,会发生该错误.您可以使用延迟扩展对其进行echo
there is no need to escape the >
with the call
. It's safe due to the surrounding quotes. The error occures when echoing it in the subroutine. You can use delayed expansion to echo
it:
@echo off
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
SET /A countArgs=1
...
SET /A countArgs+=1
CALL :error "!countArgs!. Argument -> bla"
EXIT /B 1
...
:error
ECHO ERROR
for /f "delims=" %%a in ("%~1") do echo for: %%a
echo quoted: "%~1"
set "x=%~1"
ECHO delayed: !x!
EXIT /B 0
由于周围的引号,因此call
行是安全的.
使用set
命令是安全的,也因为周围有引号.
使用echo
是安全的,因为使用了延迟扩展(echo %x%
会失败,但是echo "%x%"
会很好-尽管它会显示周围的引号).
It's safe with the call
line because of the surrounding quotes.
It's safe with the set
command, also because of the surrounding quotes.
It's safe with the echo
because of using delayed expansion (echo %x%
would fail, but echo "%x%"
would be fine - although it will show the surrounding quotes).
这篇关于批处理-尝试回显大号(>)时的语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!