这个 for 循环(简化的最小示例);
@echo off
for %%a in (help -help --help /help ? /?) do (
echo %%a
)
用 '?' 扼杀 2 个元素特点。它输出
C:\Temp>test.bat
help
-help
--help
/help
C:\Temp>
所以它只是在遇到第一个 '?' 时退出循环。
这个集合的正确转义序列是什么?尝试了一堆东西,双引号、插入符、反斜杠等,但似乎没有任何效果。
最佳答案
另一种选择是在 FOR/F 字符串中使用换行符。 FOR/F 会将每一行视为一个独立的字符串。下面我展示了四种方法来做同样的事情。
@echo off
setlocal enableDelayedExpansion
:: Define LF to contain a linefeed character
set ^"LF=^
^" The above empty line is critical. DO NOT REMOVE
:: Option 1
:: Embed linefeeds directly in the string literal
for /f %%A in ("help!LF!-help!LF!--help!LF!/help!LF!?!LF!/?") do (
echo(%%A
)
echo(
:: Option 2
:: Define a variable with spaces and use search and replace
:: to substitue linefeeds
set "help=help -help --help /help ? /?"
for %%L in ("!LF!") do for /f %%A in ("!help: =%%~L!") do (
echo(%%A
)
echo(
:: Option 3
:: Embed linefeed directly in string without LF variable
for /f %%A in (^"help^
-help^
--help^
/help^
?^
/?^") do (
echo(%%A
)
echo(
:: Option 4
:: Embed linefeed directly in search and replace without LF variable
for /f %%A in (^"!help:^ ^=^
!^") do (
echo(%%A
)
我更喜欢选项 2。我发现它最容易阅读,但仍然很紧凑。
请注意,MC ND 和我都使用 echo(%%A
。这是防止 echo/?
显示 ECHO 命令的帮助所必需的。
关于windows - 批处理文件 : Escape questionmark in for loop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23852631/