存储在延迟变量中的变量名称 - 批处理

时间:2017-08-16 14:58:03

标签: batch-file delayedvariableexpansion

我有:

setlocal EnableDelayedExpansion
var inLoopVar=hey
var first=!inLoopVar!
echo %!first!%

这会输出ECHO OFF,因为%!first!%将返回为空。

我如何打印"嘿"

更新并澄清:

以下是我的完整代码以及我正在尝试做的评论

@ECHO OFF
setlocal EnableDelayedExpansion
set RESULT=TRUE
set INPUT[0]=+NAME=RESULT; VARB=Second; VARC=Second; VARD=Second; VARE=Second; VARF=Second; VARG=Second;
set length=1
set i=0
:loop
if %i% equ %length% goto :eof
for /f "usebackq delims=+ tokens=2" %%j in (`set INPUT[%i%]`) do (
    set y=%%j
    FOR /f "tokens=1-7 delims=; " %%a IN ("!y!") DO (
       set aaa=%%a
       set testVar=!aaa:~5!
       REM basically testVar resolves to RESULT
       echo !!testVar!!
       REM Above echo prints "RESULT"
       echo %!!testVar!!%
       REM Above echo prints "ECHO is off."
    )
)
set /a i=%i%+1
goto loop

而不是ECHO is off.我正在尝试输出TRUE

1 个答案:

答案 0 :(得分:0)

@ECHO OFF
setlocal EnableDelayedExpansion
set RESULT=TRUE
set INPUT[0]=+NAME=RESULT; VARB=Second; VARC=Second; VARD=Second; VARE=Second; VARF=Second; VARG=Second;
set length=1
set i=0
:loop
if %i% equ %length% goto :eof
for /f "usebackq delims=+ tokens=2" %%j in (`set INPUT[%i%]`) do (
    CALL :sub %%j
)
set /a i=%i%+1
goto loop
GOTO :EOF

:sub
SET "namepart="
FOR %%a IN (%*) DO IF DEFINED namepart (
 SET "Valuepart=!%%a!"
 ECHO !namepart! is !valuepart!
 SET "namepart="
) ELSE (
 SET "namepart=%%a"
)
GOTO :eof

如果您要解释您打算做什么而不是通过检查您未能做任何事情来确定您想要做什么,这将有所帮助。