Problem with setlocal enabledelayedexpansion

Discussion in 'Scripting' started by sacarias, Feb 11, 2020.

  1. sacarias

    sacarias MDL Junior Member

    Nov 21, 2018
    82
    1
    0
    #1 sacarias, Feb 11, 2020
    Last edited: Feb 11, 2020
    I have this code:

    Code:
    @echo off
    
    REM Detect either Windows 32 or 64 bit
    
    if /i %PROCESSOR_ARCHITECTURE%==x86 (
       if not defined PROCESSOR_ARCHITEW6432 (set xOS=Win32) else (set xOS=Win64)
    ) else set xOS=Win64
    
    REM Search for Java installation
    
    setlocal enabledelayedexpansion
    if /i %xOS%==win64 (
       if exist "%programfiles(x86)%"\Java\jre1.8* (
          for /f "delims=" %%j in ('dir /b /ad "%programfiles(x86)%"\Java\jre1.8*') do set javapath=%%j
          set javapathsec="%programfiles(x86)%"\Java\!javapath!\lib\security
       ) else (
          echo ERROR: Java 8 ^(x86^) not installed
          echo.
          pause
          goto :eof
       )
    ) else (
       if exist "%programfiles%"\Java\jre1.8* (
          for /f "delims=" %%j in ('dir /b /ad "%programfiles%"\Java\jre1.8*') do set javapath=%%j
          set javapathsec="%programfiles%"\Java\!javapath!\lib\security
       ) else (
          echo ERROR: Java 8 ^(x86^) not installed
          echo.
          pause
          goto :eof
       )
    )
    endlocal
    
    echo %javapath%
    echo %javapathsec%
    
    pause

    But the outcome of this is always "ECHO is off" instead of the actual values of the variables.
    The variables javapath and javapathsec are not being set!

    Why isn't setlocal/endlocal working?

    I want enabledelayedexpansion to be only for this code block
     
  2. abbodi1406

    abbodi1406 MDL KB0000001

    Feb 19, 2011
    16,211
    84,860
    340
    endlocal remove the variable set before it

    instead use setlocal disabeldelayedexpansion
    or this one line trick (not verified)
    Code:
    endlocal &set javapath=%javapath% &set javapathsec=%javapathsec%
     
  3. sacarias

    sacarias MDL Junior Member

    Nov 21, 2018
    82
    1
    0
    #3 sacarias, Feb 11, 2020
    Last edited: Feb 11, 2020
    (OP)
    Will using disabledelayedexpansion instead of endlocal have the same effect for the code block?
    In CMD what's the default behavior? Indeed delayed expansions disabled?