0

I have a big problem when i use the batch command:

setlocal enabledelayedexpansion

because it dosen't keep the variables that i set in it when i use:

endlocal enabledelayedexpansion

Here is my code:

: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

Edit: i changed the code because i managed to fix the problem with that part. Here it is the solution for the previous code i posted:

: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
  • NOTE: Also when i try to use the call set var1name=%v%%var1%%% it does not work, because it was a v before it. –  Apr 27 '16 at 17:07
  • `endlocal` has no argument, it restores the previous delayed expansion state (dis-/enabled)... – aschipfl Apr 28 '16 at 15:39
  • Please do not post solutions in question posts, use answers instead! The question title is not quite clear, but I suppose you mean something like: "[Make an environment variable survive ENDLOCAL](http://stackoverflow.com/questions/3262287/make-an-environment-variable-survive-endlocal)" -- is that correct? – aschipfl Apr 28 '16 at 15:43
  • No i ment how to get a variable's value if the variable's name is in another variable without setlocal –  Apr 28 '16 at 15:55

1 Answers1

0

I figured it out, sorry for the shitty post.

Here it is how i did it, when you use the call command in a batch file you can get out of the problem easily without using setlocal that deletes your set variables. First you need to create a temporary variable, in my case tmp and set it to the variable that contains the variable name:

set tmp=prefix%var%

Now in tmp you have the full variable name, next we need to extract the value of the variable to a new variable, that we will call tmp:

call set tmp2=%%%tmp%%%

It was that simple, now we have the value of prefix%var2% in %tmp2%

First code fix:

: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

Second code fix:

: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
    This can be achieved in one line as follows: `call set "foo=%prefix%var%%"`. Also note that `tmp` is a standard Windows environment variable, so it's good to avoid using it. – paddy Sep 05 '22 at 06:07