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

前端 未结 12 879
慢半拍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.

    0 讨论(0)
  • 2020-11-30 17:12

    Another idea: Use DIR /X to determine the short names generated for non-8dot3 file names. Then use these in your %PATH%.

    For example, 'C:\Program Files' becomes 'C:\PROGRA~1'.

    0 讨论(0)
  • 2020-11-30 17:13

    Creating a folder c:\bin adding to your path and hardlinking like you said could shorten the string. Maybe add a variable pf to system vars with value c:\Program Files then replace c:\Program Files with %pf% in path.

    Edit:

    Create a virtual drive. subst p: "c:\program files"

    0 讨论(0)
  • 2020-11-30 17:18

    One way I can think of is to use other environment variables to store partial paths; for example, if you have

    C:\this_is_a\long_path\that_appears\in_multiple_places\subdir1;
    C:\this_is_a\long_path\that_appears\in_multiple_places\subdir2;
    

    then you can create a new environment variable such as

    SET P1=C:\this_is_a\long_path\that_appears\in_multiple_places
    

    after which your original paths become

    %P1%\subdir1;
    %P1%\subdir2;
    

    EDIT: Another option is to create a bin directory that holds .bat files that point to the appropriate .exe files.

    EDIT 2: Ben Voigt's comment to another answer mentions that using other environment variables as suggested might not reduce the length of %PATH% because they would be expanded prior to being stored. This may be true and I have not tested for it. Another option though is to use 8dot3 forms for longer directory names, for example C:\Program Files is typically equivalent to C:\PROGRA~1. You can use dir /x to see the shorter names.

    EDIT 3: This simple test leads me to believe Ben Voigt is right.

    set test1=hello
    set test2=%test1%hello
    set test1=bye
    echo %test2%
    

    At the end of this, you see output hellohello rather than byehello.

    EDIT 4: In case you decide to use batch files to eliminate certain paths from %PATH%, you might be concerned about how to pass on arguments from your batch file to your executable such that the process is transparent (i.e., you won't notice any difference between calling the batch file and calling the executable). I don't have a whole lot of experience writing batch files, but this seems to work fine.

    @echo off
    
    rem This batch file points to an executable of the same name
    rem that is located in another directory. Specify the directory
    rem here:
    
    set actualdir=c:\this_is\an_example_path
    
    rem You do not need to change anything that follows.
    
    set actualfile=%0
    set args=%1
    :beginloop
    if "%1" == "" goto endloop
    shift
    set args=%args% %1
    goto beginloop
    :endloop
    %actualdir%\%actualfile% %args%
    

    As a general rule, you should be careful about running batch files from the internet, since you can do all sorts of things with batch files such as formatting your hard drive. If you don't trust the code above (which I wrote), you can test it by replacing the line

    %actualdir%\%actualfile% %args%
    

    with

    echo %actualdir%\%actualfile% %args%
    

    Ideally you should know exactly what every line does before you run it.

    0 讨论(0)
  • 2020-11-30 17:18

    I generally don't have to worry about this (I haven't run into a path size limit - I don't even know what that is on modern Windows systems), but here's what I might do to avoid putting a program's directory in the path:

    • most command line utilities get thrown into a c:\util directory that's on the path
    • otherwise, I'll add a simple cmd/batch file to the c:\util directory that looks something like:

      @"c:\program files\whereever\foo.exe" %*
      

    which essentially creates an alias for the command. It's not necessarily perfect. Some programs really insist on being in the path (that's pretty rare nowadays), and other programs that try to invoke it might not find it properly. But for most uses it works well.

    But generally, I haven't had to worry about avoiding adding directories to the path.

    0 讨论(0)
  • 2020-11-30 17:24

    USe the App Path registry key instead of the path variable for application-specific paths:

    http://msdn.microsoft.com/en-us/library/windows/desktop/ee872121(v=vs.85).aspx

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