envvar.bat 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. @echo off
  2. REM
  3. REM This Batch-Script helps managing User-Environment Variables.
  4. REM Initial version by Paul-Dieter Klumpp, 2013-02-16
  5. REM
  6. REM It will set environment variables according to some configuration files.
  7. REM The configuration files are simple. If you want to set the PATH variable,
  8. REM you create file called "PATH.env". In that file, each line specifies a path.
  9. REM It will search all .01env and .02env files in the current directory.
  10. REM The files with the extension .01env gets evaluated before the files with
  11. REM the extension .02env.
  12. REM
  13. REM Some history
  14. REM 2013-10-18: pkl, checking if those 01env-files exist..
  15. REM 2013-08-05: pkl, now there is an order in evaluation of files.
  16. REM now, search all files with extension ".env" and get the filename without '.env'
  17. goto :main
  18. :addToPath
  19. set TEMPPA=%TEMPPA%%*
  20. goto :eof
  21. :setEnvVarByFile
  22. set ENVSET=%TEMP%\tmp%USER%_envset.bat
  23. echo echo. > %ENVSET%
  24. set FILES=%*
  25. set TMPFLIST=%TEMP%\tmp%USER%_files
  26. dir /b %FILES% > %TMPFLIST%
  27. echo Evaluating the following environment files now:
  28. type %TMPFLIST%
  29. for /F %%f in (%TMPFLIST%) do (
  30. @echo.
  31. echo Now environment file: %%f
  32. for /F "tokens=1 delims=." %%n in ("%%f") do (
  33. echo Environment %%n currently: !%%n!
  34. setx OLD_%%n "!%%n!"
  35. set i=0
  36. set TEMPPA=
  37. for /f "tokens=*" %%a in (%%f) do (
  38. set /A i+=1
  39. REM echo "wat: %%a"
  40. call :addToPath %%a
  41. REM echo "wot: !TEMPPA! "
  42. )
  43. setx %%n "!TEMPPA!"
  44. echo SET %%n=!TEMPPA!>> %ENVSET%
  45. echo.
  46. echo "!i! Eintraege gesetzt: %%n = !TEMPPA!"
  47. echo.
  48. echo.
  49. )
  50. )
  51. call %ENVSET%
  52. del %ENVSET%
  53. del %TMPFLIST%
  54. goto :eof
  55. :main
  56. SETLOCAL EnableDelayedExpansion
  57. REM http://stackoverflow.com/questions/659647/how-to-get-folder-path-from-file-path-with-cmd
  58. set WD="%~dp0"
  59. cd "!WD!"
  60. REM first .01env, then .02env ... useful, when 02env uses variables which have been set in 01env. This keeps the correct order.
  61. IF EXIST *.01env (
  62. echo Found .01env-files. Evaluating them now...
  63. call :setEnvVarByFile *.01env
  64. )
  65. IF EXIST *.02env (
  66. echo Found .02env-files. Evaluating them now...
  67. call :setEnvVarByFile *.02env
  68. )
  69. echo Shells neu starten, damit die nun neuen Einstellungen geladen und aktiv werden.
  70. echo.
  71. pause
  72. ENDLOCAL
  73. :eof