Permanently altering a user's %PATH% environment variable via batch or Python

前端 未结 4 1604
梦毁少年i
梦毁少年i 2021-02-09 00:53

I\'ve been having difficulty with getting my users to set the PATH environment variable manually, I\'m looking for a way to do this automatically. A batch file would be prefera

相关标签:
4条回答
  • 2021-02-09 01:10

    So, since I've been having difficulty with getting my users to set the PATH manually, I'm looking for a way to do this automatically.

    The HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths (as well as HKEY_CURRENT_USER\...) registry key allows you to attach an application specific path to your executable name.

    Whenever an executable of the given name is started, the application specific path is added to that executable's PATH environment variable.

    0 讨论(0)
  • 2021-02-09 01:23

    As David said, there is the SETX tool that you can get from the Windows Resource Kit.

    However, I have found that SETX has trouble (like crashing) sometimes. I have not figured out exactly what the problem is, but I suspect it is a size issue (for example if you try to set a variable—in my case it was PATH—to a value that is too big, eg >1024 some odd characters).

    I have found two other executables that can do the same thing. My favorite in particular is SetEnv by Jonathan “Darka” Wilkes over at CodeProject. He has made it quite useful, with good functionality, and it is compatible with all Windows systems—I suggested some features too. :)

    Another option, if you are up to it, is to do it manually (actually adding the item to the registry and then either broadcasting a WM_SETTINGCHANGE to top-level windows, or restarting the shell/rebooting). However I think that SetEnv in a BATCH file is your best bet. ;)

    0 讨论(0)
  • 2021-02-09 01:25

    I just ran across this question and didn't like any of the available options so I decided to write my own solution.

    (SetEnv would've been good, but I didn't like the non-libre license and I always prefer not having to call a subprocess... I wouldn't mind calling SetEnv as a subprocess but, according to Wikipedia, the license it uses is non-libre because it has some kind of "do no evil" clause and that kind of legally-ambiguous restriction is always a ticking time-bomb in my opinion.)

    Here's a little MIT-licensed Python class to hide away the work of modifying the registry directly and sending the WM_SETTINGCHANGE. (Good for use in setup.py)

    0 讨论(0)
  • 2021-02-09 01:36

    From this website:

    Using the add-on tool Setx.exe

    It is not part of the standard Windows XP setup but a command-line tool called setx.exe is included in the Windows XP Service Pack 2 Support Tools. This tool extends the set command so that permanent changes in the environment variables can be made. For example, to add a folder C:\New Folder to the path, the command would be

    setx path "%PATH%;C:\New Folder"
    

    This sounds like it'll work for what you're wanting to do.

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