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

WindowsPathEnvironment VariablesExecutable

Windows Problem Overview


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, but now I come to the limit of the path string, so i can't add any more dir.

So what do you recommend? A long time ago, I tried to use softLinks of the executables in a Dir that belonged to the path, but that approach didn't work. Throw the "executable only" to a known Dir,has the problems that almost any application require a set of files, so this also is bad. Throw the executable and all his files to a known Dir, mmm this will work, but the possibility to get a conflict in the name of the files is very very high. Create a HardLink? i don't know. What do you think?

Windows Solutions


Solution 1 - Windows

This will parse your %PATH% environment variable and convert each directory to its shortname equivalent and then piece it all back together:

@echo off

SET MyPath=%PATH%
echo %MyPath%
echo --

setlocal EnableDelayedExpansion

SET TempPath="%MyPath:;=";"%"
SET var=
FOR %%a IN (%TempPath%) DO (
	IF exist %%~sa (
 		SET "var=!var!;%%~sa"
 	) ELSE (
 		echo %%a does not exist
 	)
)

echo --
echo !var:~1!

Take the output and update the PATH variable in environment variables.

Solution 2 - Windows

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.

Solution 3 - Windows

if you are using windows vista or higher, you can make a symbolic link to the folder. for example:

mklink /d C:\pf "C:\Program Files"

would make a link so c:\pf would be your program files folder. I shaved off 300 characters from my path by using this trick.

Solution 4 - Windows

In case anyone's interested...

I find I never really need all those paths at once, so I create a bunch of "initialization" batch files which modify the path accordingly.

For example, if I wanted to do some C++ development in Eclipse, I would do:

> initmingw
> initeclipse
> eclipse

This is also handy for avoiding conflicts between executables with the same name (such as the C++ and D compilers, which both have a make.exe).

My batch files typically look like this:

@echo off
set PATH=C:\Path\To\My\Stuff1;%PATH%
set PATH=C:\Path\To\My\Stuff2;%PATH%

I find this approach relatively clean and have yet to run into any problems with it.

Solution 5 - Windows

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.

Solution 6 - Windows

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'.

Solution 7 - Windows

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

Solution 8 - Windows

I wrote and use on a every-time basis a standard stream (stdin/stderr/stdout) & exit code PROXY program (called dispatcher https://github.com/131/dispatcher)

All CLI program i use (node, php, python, git, svn, rsync, plink ...) i'm using are actually the same exe file (around 10kb, that i just name differently), that i put in the same directory. A dummy static clear text file do the "proxy file name to real exe mapping".

Dispatcher use low level Process management win32 API to be absolutly transparent.

Using this software, i only do have ONE additionnal directory set in my PATH for all programs i might use.

Solution 9 - Windows

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"

Solution 10 - Windows

I follow these steps to make the entries manageable:

  1. Created different users for different combination of software packages usage. Example: (a) Created a user web for making available all the web development software; (b) Created a user database for making available all the database and data warehousing software packages. Remember some software may create more than one entry. Or sometime I break this into oracle specific and MSSQL specific and oracle specific users. I put MySQL/PostgreSQL, tomcat, wamp, xamp all into the user account webr.

  2. If possible install common packages like office, photoshop, .. as system specific available for all users and special packages as user specific. Of course I had to log into different users and install them. Not all software may provide this option. If "install for this user only" option is not available, install it for the whole system.

  3. I avoid installing programs in to the folder Program File (x86) or in to Program File. I always install into the base directory. For example MySQL 64 bit goes into "C:\mysql64" and MySQL 32 bit goes into "C:\mysql" folder. I always assume adding a suffix 64 only for 64bit software. If no suffix, then it is a 32 bit. I follow the same thing to Java and others. This way my path will be shorter, not including "C:\Program File (x86)". For some software the configuration file may need to be edited to show where exactly the .exe file is. Only program that demands to be installed into "C:\Program File (x86)" will be installed into that folder. Always I remember to shorten the names. I avoid version number like tomcat/release/version-2.5.0.3 such details. If I need to the know version, I create a file by name version and put it into the tomcat folder. In general shorten the link as much as possible.

  4. Include any batch to replace abbreviated link to the path, if all the above steps passed the Windows limit.

Then Log into usage specific (mobile application, or database/data warehousing or web-development.. ..) user and do the relevant tasks.

You can also create virtual windows within windows. As long as you have one licensed OS copy, creating multiple virtual windows with same key is possible. You can put packages specific for a particular task in that machine. You have to launch separate VM each time. Some memory intensive packages like 3D animation movie makers all should be put into the main machine, not into VM as VM will have only a part of the RAM available for its use. It is a pain to boot each VM though.

Solution 11 - Windows

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.

Solution 12 - Windows

Didn't try it, but will splitting PATH in parts work and joining them in final variable work?

Example initially let's say you have something like

PATH={LONGPATH1};{LONGPATH2};....{2048th char}....{LONGPATH_N-1};{LONGPATH_N}

Instead you create:

_PATH1 = {LONGPATH1};{LONGPATH2};....{2048 char}
_PATH2 = {2049th char}...{LONGPATH_N-1};{LONGPATH_N}
rem // may be more parts
PATH = %_PATH1%;%_PATH2%

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmjsrView Question on Stackoverflow
Solution 1 - WindowsTodd SmithView Answer on Stackoverflow
Solution 2 - WindowsMitch SchwartzView Answer on Stackoverflow
Solution 3 - Windowsbmg002View Answer on Stackoverflow
Solution 4 - WindowsYellPikaView Answer on Stackoverflow
Solution 5 - WindowsMichael BurrView Answer on Stackoverflow
Solution 6 - WindowsAndroid EveView Answer on Stackoverflow
Solution 7 - WindowsMad HatterView Answer on Stackoverflow
Solution 8 - Windows131View Answer on Stackoverflow
Solution 9 - WindowstroyntView Answer on Stackoverflow
Solution 10 - WindowsDr. A. AnukanthView Answer on Stackoverflow
Solution 11 - WindowsPhylliidaView Answer on Stackoverflow
Solution 12 - WindowsPhilipp MuninView Answer on Stackoverflow