我有一系列存储字符串的变量。这些变量的索引号通过FOR命令递增。
我想列出所有这些变量的所有值,索引号是从FOR定义生成的(可以是下次自动+1或类似的子功能)。
setlocal EnableDelayedExpansion
set dir01a=text 1
set dir02a=text 2
set dir03a=text 3
for %G in (01 02 03) do (echo !dir%Ga!)
它输出:
!dir01a!
!dir02a!
!dir03a!
我想要这个:
text 1
text 2
text 3
我找不到任何解决方案如何让echo将!dir%Ga!
变量视为变量名而不是文本。
请问好吗?
如果批量使用,我想向%G添加1%以上。谢谢。
答案 0 :(得分:0)
您的批处理脚本有效:
@ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
set dir01a=text 1
set dir02a=text 2
set dir03a=text 3
for %%G in (01 02 03) do echo dir%%Ga =to= !dir%%Ga!
但是,SETLOCAL
命令不会影响当前cmd
实例,您需要创建新实例,请参阅下面的cmd /?
和更新。
SETLOCAL EnableDelayedExpansion
和ENDLOCAL
; CMD /V:ON /K
和EXIT
。 复制&将下一个代码段粘贴到打开的cmd
窗口:
ECHO OFF
set dir01a=text 1
set dir02a=text 2
set dir03a=text 3
for %G in (01 02 03) do echo dir%Ga =to= !dir%Ga!
cmd /E:ON /V:ON /C for %G in (01 02 03) do @echo dir%Ga =to= !dir%Ga!
ECHO ON >NUL
<强>输出强>:
==> ECHO OFF
set dir01a=text 1
set dir02a=text 2
set dir03a=text 3
for %G in (01 02 03) do echo dir%Ga =to= !dir%Ga!
dir01a =to= !dir01a!
dir02a =to= !dir02a!
dir03a =to= !dir03a!
cmd /E:ON /V:ON /C for %G in (01 02 03) do @echo dir%Ga =to= !dir%Ga!
dir01a =to= text 1
dir02a =to= text 2
dir03a =to= text 3
ECHO ON >NUL
更新:copy&amp;将下一个代码段粘贴到一个打开的cmd
窗口(注意“prompt
和===
命令前缀以获得更好的输出可读性”:
prompt $Q$Q$G$S
echo OFF
=== set "var=OLD_value_d"
=== SETLOCAL EnableDelayedExpansion
=== set "var=NEW_value_d" & echo default expansion %var% !var! & set var
=== echo default expansion %var% !var!
=== ENDLOCAL
=== echo default expansion %var% !var!
=== CMD /V:ON /K
echo OFF
=== set "var=OLD_value_E"
=== set "var=NEW_value_E" & echo Delayed expansion %var% !var! & set var
=== echo Delayed expansion %var% !var!
=== EXIT
=== echo default expansion %var% !var!
=== echo ON >NUL
prompt
<强>输出强>:
==> prompt $Q$Q$G$S
==> echo OFF
=== set "var=OLD_value_d"
=== SETLOCAL EnableDelayedExpansion
=== set "var=NEW_value_d" & echo default expansion %var% !var! & set var
default expansion OLD_value_d !var!
var=NEW_value_d
=== echo default expansion %var% !var!
default expansion NEW_value_d !var!
=== ENDLOCAL
=== echo default expansion %var% !var!
default expansion NEW_value_d !var!
=== CMD /V:ON /K
==> echo OFF
=== set "var=OLD_value_E"
=== set "var=NEW_value_E" & echo Delayed expansion %var% !var! & set var
Delayed expansion OLD_value_E NEW_value_E
var=NEW_value_E
=== echo Delayed expansion %var% !var!
Delayed expansion NEW_value_E NEW_value_E
=== EXIT
=== echo default expansion %var% !var!
default expansion NEW_value_d !var!
=== echo ON >NUL
==> prompt
答案 1 :(得分:0)
就个人而言,我会使用
for /f "tokens=1*delims==" %%F in ('set dir 2^>nul') do echo %%G
如果没有设置开始2^>nul
dir
会抑制生成的错误消息