nul(goto PATH_EXI。配置环境变量|配置环境变量 wmic。" />

配置环境变量|配置环境变量 wmic

  1. @echo off
  2. ::设定环境变量
  3. set "add_path1=D:\Oracle\product\10.2.0\db_1\bin"
  4. set "env_path1=ORACLE_HOME"
  5. ::检查PATH中是否已添加
  6. wmic ENVIRONMENT where "name='PATH'" get VariableValue|findstr /i /c:"%add_path1%">nul&&(goto PATH_EXIST)
  7. echo PATH环境变量中未添加: %add_path1%
  8. echo.
  9. echo 添加环境变量
  10. echo.
  11. set "set_val=%add_path1%; %PATH%"
  12. wmic ENVIRONMENT where "name='PATH' and username=''" set VariableValue="https://www.it610.com/article/%set_val%"
  13. :PATH_EXIST
  14. echo PATH环境变量中已添加: %add_path1%
  15. echo.
  16. ::检查是否已创建环境变量
  17. wmic ENVIRONMENT where "name='%env_path1%'" get VariableValue|findstr /i /c:"VariableValue">nul&&(goto ENV_CREATE)
  18. echo %env_path1%环境变量未创建
  19. echo.
  20. echo 创建环境变量
  21. echo.
  22. wmic ENVIRONMENT create name="%env_path1%",username="",VariableValue="https://www.it610.com/article/%add_path1%"
  23. :ENV_CREATE
  24. echo %env_path1%环境变量已创建
  25. echo.
  26. echo 配置完成,按任意键还原环境变量
  27. echo.
  28. pause>nul
  29. echo 还原环境变量
  30. echo.
  31. set set_val=
  32. :loop_findpath
  33. set /a n+=1
  34. for /f "tokens=%n% delims=; " %%a in ('wmic ENVIRONMENT where "name='PATH'" get VariableValue^|findstr /i /c:"%add_path1%"') do (
  35. if "%%a" neq "" (
  36. if "%%a" neq "%add_path1%" (
  37. if "%set_val%" == "" (
  38. set "set_val=%%a"
  39. ) else (
  40. set "set_val=%set_val%; %%a"
  41. )
  42. )
  43. goto loop_findpath
  44. )
  45. )
  46. wmic ENVIRONMENT where "name='PATH' and username=''" set VariableValue="https://www.it610.com/article/%set_val%"
  47. echo 删除环境变量
  48. echo.
  49. wmic ENVIRONMENT where "name='%env_path1%'" delete
  50. echo 环境变量已还原
  51. echo.
  52. pause>nul

    推荐阅读