github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/test/integration/testdata/tools/refreshenv/refreshenv.bat (about)

     1  @echo off
     2  ::
     3  :: RefreshEnv.cmd
     4  ::
     5  :: Batch file to read environment variables from registry and
     6  :: set session variables to these values.
     7  ::
     8  :: With this batch file, there should be no need to reload command
     9  :: environment every time you want environment changes to propagate
    10  
    11  ::echo "RefreshEnv.cmd only works from cmd.exe, please install the Chocolatey Profile to take advantage of refreshenv from PowerShell"
    12  echo | set /p dummy="Refreshing environment variables from registry for cmd.exe. Please wait..."
    13  
    14  goto main
    15  
    16  :: Set one environment variable from registry key
    17  :SetFromReg
    18      "%WinDir%\System32\Reg" QUERY "%~1" /v "%~2" > "%TEMP%\_envset.tmp" 2>NUL
    19      for /f "usebackq skip=2 tokens=2,*" %%A IN ("%TEMP%\_envset.tmp") do (
    20          echo/set "%~3=%%B"
    21      )
    22      goto :EOF
    23  
    24  :: Get a list of environment variables from registry
    25  :GetRegEnv
    26      "%WinDir%\System32\Reg" QUERY "%~1" > "%TEMP%\_envget.tmp"
    27      for /f "usebackq skip=2" %%A IN ("%TEMP%\_envget.tmp") do (
    28          if /I not "%%~A"=="Path" (
    29              call :SetFromReg "%~1" "%%~A" "%%~A"
    30          )
    31      )
    32      goto :EOF
    33  
    34  :main
    35      echo/@echo off >"%TEMP%\_env.cmd"
    36  
    37      :: Slowly generating final file
    38      call :GetRegEnv "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" >> "%TEMP%\_env.cmd"
    39      call :GetRegEnv "HKCU\Environment">>"%TEMP%\_env.cmd" >> "%TEMP%\_env.cmd"
    40  
    41      :: Special handling for PATH - mix both User and System
    42      call :SetFromReg "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" Path Path_HKLM >> "%TEMP%\_env.cmd"
    43      call :SetFromReg "HKCU\Environment" Path Path_HKCU >> "%TEMP%\_env.cmd"
    44  
    45      :: Caution: do not insert space-chars before >> redirection sign
    46      echo/set "Path=%%Path_HKLM%%;%%Path_HKCU%%" >> "%TEMP%\_env.cmd"
    47  
    48      :: Cleanup
    49      del /f /q "%TEMP%\_envset.tmp" 2>nul
    50      del /f /q "%TEMP%\_envget.tmp" 2>nul
    51  
    52      :: capture user / architecture
    53      SET "OriginalUserName=%USERNAME%"
    54      SET "OriginalArchitecture=%PROCESSOR_ARCHITECTURE%"
    55  
    56      :: Set these variables
    57      call "%TEMP%\_env.cmd"
    58  
    59      :: Cleanup
    60      del /f /q "%TEMP%\_env.cmd" 2>nul
    61  
    62      :: reset user / architecture
    63      SET "USERNAME=%OriginalUserName%"
    64      SET "PROCESSOR_ARCHITECTURE=%OriginalArchitecture%"
    65  
    66      echo | set /p dummy="Finished."
    67      echo .