How to persistently set a variable in Windows 7 from a batch file?

前端 未结 7 1147
無奈伤痛
無奈伤痛 2020-12-02 08:38

I am trying to set the PATH environment variable in windows 7 using a bat-file; however it does not seem to work.

I am using this windows command:

se         


        
相关标签:
7条回答
  • 2020-12-02 09:36

    As wizlb noted, doing

    setx PATH "%cd%;%path%;" -m
    

    will copy local env to system env, and without -m it will copy system env to user env. Neither is desirable. In order to accurately edit only one part of registry (system or user, system in my below example) you need to do this:

    for /F "tokens=2* delims= " %%f IN ('reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path ^| findstr /i path') do set OLD_SYSTEM_PATH=%%g
    setx.exe PATH "%OLD_SYSTEM_PATH%;%OTHER_STUFF%;" -m
    

    Credit for the solution goes to http://www.robvanderwoude.com/ntregistry.php

    0 讨论(0)
提交回复
热议问题