How do you avoid over-populating the PATH Environment Variable in Windows?

前端 未结 12 1046
慢半拍i
慢半拍i 2020-11-30 16:26

I would like to know what are the approaches that you use to manage the executables in your system. For example I have almost everything accessible through the command line,

12条回答
  •  心在旅途
    2020-11-30 17:08

    The solutions above only work if you can trim down your path. In my case, that wasn't really an option, and it was a hassle to have to run a script every time I opened a command prompt. So I wrote a simple script that runs automatically when opening the command prompt, and appends the contents of a text file to your path.

    There are also some contexts where having this script run breaks things (say, in a github or cygwin shell), so I also added a file that contains a list of paths that, if the command prompt is started in them, the path variable isn't changed via the startup script that normally updates the path.

    @echo off
    
    :: Modify these to the actual paths of these two files
    set dontSetupFile=C:\Users\Yams\Dontsetup.txt
    set pathFile=C:\Users\Yams\Path.txt
    
    :: Retrieve the current path (for determining whether or not we should append to our path)
    set curDir=%cd%
    
    :: Be done if the current path is listed in the dontSetupFile
    SetLocal EnableDelayedExpansion
    for /F "delims=" %%i in (%dontSetupFile%) do (
        if "%%i"=="%curDir%" GOTO AllDone
    )
    
    
    
    :: Append the pathFile to our current PATH
    set pathAppend=
    for /F "delims=" %%i in (%pathFile%) do (set pathAppend=!pathAppend!%%i)
    
    set PATH=%PATH%;%pathAppend%
    
    
    :: The only way to actually modify a command prompt's path via a batch file is by starting
    ::   up another command prompt window. So we will do this, however, if this script is
    ::   automatically called on startup of any command prompt window, it will infinately 
    ::   recurse and bad things will happen.
    
    :: If we already ran, we are done
    if "%yams%"=="onion" GOTO AllDone
    
    :: Otherwise, flag that we just ran, and then start up a new command prompt window
    ::   with this flag set
    set yams=onion
    
    cmd \K set PATH=%PATH%;
    
    :: When that command prompt exits, it will load back up this command prompt window, and
    ::   then the user will need to exit out of this as well. This causes this window to
    ::   automatically exit once the cmd it just spawned is closed.
    exit()
    
    :: Path is set up, we are done!
    :AllDone
    @echo on
    

    And Path.txt will look something like

    C:\Program Files (x86)\Google\google_appengine;
    C:\Program Files (x86)\ATI Technologies\ATI.ACE\Core-Static;
    C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;
    C:\Program Files\Microsoft SQL Server\110\Tools\Binn;
    C:\Program Files\Microsoft DNX\Dnvm;
    C:\Program Files (x86)\Windows Kits\8.0\Windows Performance Toolkit;
    

    While Dontsetup.txt will look something like

    C:\Program Files (x86)\Windows Kits\8.0\Windows Performance Toolkit
    C:\Program Files (x86)\Git\cmd
    C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin
    

    To make this run automatically on startup, open regedit, navigate to HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Command Processor, then right click on the right and press new -> Multi-String Value. Name it AutoRun. Set it's value to

    C:\Users\Yams\setUpPath.bat
    

    or wherever else you stored the batch file above.

提交回复
热议问题