github.com/ActiveState/cli@v0.0.0-20240508170324-6801f60cd051/internal/assets/contents/scripts/setenv.bat (about)

     1  :: Copyright 2019 Chocolatey github@chocolatey.
     2  
     3  :: Licensed under the Apache License, Version 2.0 (the "License");
     4  :: you may not use this file except in compliance with the License.
     5  :: You may obtain a copy of the License at
     6  
     7  ::    http://www.apache.org/licenses/LICENSE-2.0
     8  
     9  :: Unless required by applicable law or agreed to in writing, software
    10  :: distributed under the License is distributed on an "AS IS" BASIS,
    11  :: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  :: See the License for the specific language governing permissions and
    13  :: limitations under the License.
    14  
    15  @echo off
    16  ::
    17  :: RefreshEnv.bat
    18  ::
    19  :: Batch file to read environment variables from registry and
    20  :: set session variables to these values.
    21  ::
    22  :: With this batch file, there should be no need to reload command
    23  :: environment every time you want environment changes to propagate
    24  
    25  echo | set /p dummy="Refreshing environment variables from registry for cmd.exe. Please wait..."
    26  
    27  goto main
    28  
    29  :: Set one environment variable from registry key
    30  :SetFromReg
    31      "%WinDir%\System32\Reg" QUERY "%~1" /v "%~2" > "%TEMP%\_envset.tmp" 2>NUL
    32      for /f "usebackq skip=2 tokens=2,*" %%A IN ("%TEMP%\_envset.tmp") do (
    33          echo/set "%~3=%%B"
    34      )
    35      goto :EOF
    36  
    37  :: Get a list of environment variables from registry
    38  :GetRegEnv
    39      "%WinDir%\System32\Reg" QUERY "%~1" > "%TEMP%\_envget.tmp"
    40      for /f "usebackq skip=2" %%A IN ("%TEMP%\_envget.tmp") do (
    41          if /I not "%%~A"=="Path" (
    42              call :SetFromReg "%~1" "%%~A" "%%~A"
    43          )
    44      )
    45      goto :EOF
    46  
    47  :main
    48      echo/@echo off >"%TEMP%\_env.cmd"
    49  
    50      :: Slowly generating final file
    51      call :GetRegEnv "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" >> "%TEMP%\_env.cmd"
    52      call :GetRegEnv "HKCU\Environment">>"%TEMP%\_env.cmd" >> "%TEMP%\_env.cmd"
    53  
    54      :: Special handling for PATH - mix both User and System
    55      call :SetFromReg "HKLM\System\CurrentControlSet\Control\Session Manager\Environment" Path Path_HKLM >> "%TEMP%\_env.cmd"
    56      call :SetFromReg "HKCU\Environment" Path Path_HKCU >> "%TEMP%\_env.cmd"
    57  
    58      :: Caution: do not insert space-chars before >> redirection sign
    59      echo/set "Path=%%Path_HKLM%%;%%Path_HKCU%%" >> "%TEMP%\_env.cmd"
    60  
    61      :: Cleanup
    62      del /f /q "%TEMP%\_envset.tmp" 2>nul
    63      del /f /q "%TEMP%\_envget.tmp" 2>nul
    64  
    65      :: capture user / architecture
    66      SET "OriginalUserName=%USERNAME%"
    67      SET "OriginalArchitecture=%PROCESSOR_ARCHITECTURE%"
    68  
    69      :: Set these variables
    70      call "%TEMP%\_env.cmd"
    71  
    72      :: Cleanup
    73      del /f /q "%TEMP%\_env.cmd" 2>nul
    74  
    75      :: reset user / architecture
    76      SET "USERNAME=%OriginalUserName%"
    77      SET "PROCESSOR_ARCHITECTURE=%OriginalArchitecture%"
    78  
    79      echo | set /p dummy="Finished."
    80      echo .