批量文件如何在没有Setlocal的变量中使用变量名

时间:2016-04-27 16:34:51

标签: windows batch-file cmd

当我使用批处理命令时,我遇到了一个大问题:

setlocal enabledelayedexpansion

因为当我使用时,它会保留我在其中设置的变量:

endlocal enabledelayedexpansion

这是我的代码:

:out
setlocal enabledelayedexpansion
if "%1"=="[" goto end
if "%1"=="]" goto end
set command=%~1
if not exist !cmd%command%! (
    setlocal disabledelayedexpansion
    set /a sError=%sError%+1
    echo At line %line%: {Library Error} Unknown command: "%~1"
    set lasterror=At line %line%: {Library Error} Unknown command: "%~1".
    goto end
)
call !cmd%command%! !ext%command%! %2 %3 %4 %5 %6 %7 %8 %9
endlocal disabledelayedexpansion
if exist rewrite.tmp (
    for /f %%A in (rewrite.tmp) do set %%A
    del rewrite.tmp
)
:end

编辑:我更改了代码,因为我设法修复了该部分的问题。 这是我发布的上一个代码的解决方案:

:compare
set ifcorrect=false
set var1=%~2
set comp=%~3
set var2=%~4
set iftrue=%~5
set else=%~6
set ifelse=%~7
call set var11=%%%var1%%%
call set var22=%%%var2%%%
if "%comp%"=="EQU" goto docomp
if "%comp%"=="NEQ" goto docomp
if "%comp%"=="LSS" goto docomp
if "%comp%"=="LEQ" goto docomp
if "%comp%"=="GTR" goto docomp
if "%comp%"=="GEQ" goto docomp
set /a sError=%sError%+1
set lasterror=At line %line%: {Command Error} Incorrect use of IF command.
echo At line %line%: {Command Error} Incorrect use of IF command.
goto end

:docomp
if "%var11%" %comp% "%var22%" set ifcorrect=true
if "%ifcorrect%"=="true" (
    call :execute execute "%iftrue%"
)
if "%ifcorrect%"=="false" (
    if "%else%"=="else" (
        call :execute execute "%ifelse%"
    )
)
goto end

1 个答案:

答案 0 :(得分:0)

我想通了,抱歉这个糟糕的帖子。

这就是我如何做到这一点,当您在批处理文件中使用call命令时,您可以轻松地解决问题而无需使用删除设置变量的setlocal。首先,您需要创建一个临时变量,在我的情况下tmp并将其设置为包含变量名称的变量:

set tmp=prefix%var%

现在在tmp中你有完整的变量名,接下来我们需要将变量的值提取到一个新变量,我们将调用tmp

call set tmp2=%%%tmp%%%

就是这么简单,现在我们在prefix%var2%

中的值为%tmp2%

第一个代码修复:

:out
if "%1"=="[" goto end
if "%1"=="]" goto end
set command=%~1
set tmp=cmd%command%
call set c1=%%%tmp%%%
if not exist %c1% (
    set /a sError=%sError%+1
    echo At line %line%: {Library Error} Unknown command: "%~1"
    set lasterror=At line %line%: {Library Error} Unknown command: "%~1".
    goto end
)
set tmp=ext%command%
call set c2=%%%tmp%%%
call %c1% %c2% %2 %3 %4 %5 %6 %7 %8 %9
if exist rewrite.tmp (
    for /f %%A in (rewrite.tmp) do set %%A
    del rewrite.tmp
)
:end

第二个代码修复:

:compare
set ifcorrect=false
set var1=%~2
set comp=%~3
set var2=%~4
set iftrue=%~5
set else=%~6
set ifelse=%~7
call set var11=%%%var1%%%
call set var22=%%%var2%%%
if "%comp%"=="EQU" goto docomp
if "%comp%"=="NEQ" goto docomp
if "%comp%"=="LSS" goto docomp
if "%comp%"=="LEQ" goto docomp
if "%comp%"=="GTR" goto docomp
if "%comp%"=="GEQ" goto docomp
set /a sError=%sError%+1
set lasterror=At line %line%: {Command Error} Incorrect use of IF command.
echo At line %line%: {Command Error} Incorrect use of IF command.
goto end

:docomp
if "%var11%" %comp% "%var22%" set ifcorrect=true
if "%ifcorrect%"=="true" (
    call :execute execute "%iftrue%"
)
if "%ifcorrect%"=="false" (
    if "%else%"=="else" (
        call :execute execute "%ifelse%"
    )
)
goto end