As I went to append my PATH environment variable on my Windows 7 x64 machine I was surprised by, well perhaps more confused by, not being able to type into the end of the value textbox.
As a reminder, one can edit the PATH variable quickly by
- Pressing 'Windows Key' + 'Pause Break'
- 'Advanced system settings' on the left
- 'Environment Variables' on the lower right corner
- Selecting 'PATH' under 'System variables'
- Clicking 'Edit'
Normally I simply click into 'Variable value:' and press the end key to get to the end. Then tack on a semicolon followed by the new path.
This time, however, I could not add more text at the end. I had hit the length limit on the PATH variable. Seriously? Yea…
Apparently you can only key in 2,048 characters. My PATH was already at 2,410, which is oddly larger than 2,048. So perhaps the archaic 'Environment variables' interface is just too strict on the length limits.
Define PATH using other 'System variables'.
You can modify PATH from the command line with
> SET PATH=%PATH%;C:\My New\Path\
So, perhaps I can define PATH in pieces using other 'System variables'. It turns out you can, and I tried it. I took all of the paths related to development tools and put them into 'DEVPATH'.
I should mention at this point, do the following carefully and at your own risk.
- Copy the PATH variable value to notepad
- Keep a copy of the original PATH just in case
- Move paths in the PATH value to a second line which relate to development (or whatever grouping you like)
- Be sure that paths are separated by a semicolon
- Define a new 'System Variable', say 'DEVPATH', and put the extracted paths in the value
- Append ";%DEVPATH%" to the shortened PATH value
- Edit PATH and set its value to the new shorter value
- Click OK twice to apply the changes
Hoping that our PATH variable is now longer and not truncated, I open a new command prompt and echo PATH. Only to find it's truncated in the exact same way. The separation into DEVPATH simply helped to organize the paths… Great, but no fix.
Accepting the PATH is limited in length, what to do? Quite simply I had to make it shorter, but I didn't want to break any references.
It turns out a path in PATH includes the directories within the path (sort off). This way we can group and shorten the paths in PATH. I have conveniently separated paths to development applications, those which if broken won't break anything critical, into DEVPATH. Modifying these is then low risk (but again, do at your own risk).
Again editing DEVPATH in notepad, I grouped paths together into 'parent' folders. For example my DEVPATH included
E:\Program Files\Microsoft SQL Server\100\Tools\Binn\;E:\Program Files\Microsoft SQL Server\100\DTS\Binn\;
Which I combined to
E:\Program Files\Microsoft SQL Server;
Doing this enough times my total PATH length was eventually under the limit. Adding my new path to DEVPATH and echoing at the command line confirmed my new path was no longer getting truncated.
The short answer is to shorten deep paths to a parent path and remove duplicate paths since the PATH references look within the sub directories. Creating a DEVPATH variable was a convenient way to move the 'low risk' paths out of PATH for liberal modification.