Overcoming the 1024 character limit with setx
I am trying to set environment variables using the setx
command, such as follows
setx PATH "f:\common tools\git\bin;f:\common tools\python\app;f:\common tools\python\app\scripts;f:\common tools\ruby\bin;f:\masm32\bin;F:\Borland\BCC55\Bin;%PATH%"
However, I get the following error if the value is more then 1024 characters long:
WARNING: The data being saved is truncated to 1024 characters.
SUCCESS: Specified value was saved.
But some of the paths in the end are not saved in variable, I guess due to character limit as the error suggests.
Solution 1:
Your best bet is to edit the registry directly.
Navigate to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
and edit the Path
value (then reboot to activate the new value).
Note however that while you can enter a very long path, (up to the maximum environment variable length; 2,048 or 32,768 bytes depending on the source), not all software will be able to read and handle it correctly if it is too long.
Solution 2:
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"
mklink /d C:\pf86 "C:\Program Files (x86)"
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.
(I know it's not related to setx but it is useful for people which are searching overcomming on 1024 char limit)
Solution 3:
You could use a PowerShell script similar to the following:
$newPath = 'F:\common tools\git\bin;F:\common tools\python\app;F:\common tools\python\app\scripts;F:\common tools\ruby\bin;F:\masm32\bin;F:\Borland\BCC55\Bin'
$oldPath = [Environment]::GetEnvironmentVariable('PATH', 'Machine');
[Environment]::SetEnvironmentVariable('PATH', "$newPath;$oldPath",'Machine');
The Environment.SetEnvironmentVariable()
API call will broadcast WM_SETTINGCHANGE
so you do not need to reboot.
Solution 4:
This open-source SetEnv command-line tool is good to edit the PATH and other environment variables without limitations. It uses a dynamic buffer so no static limitations like 1024.
http://www.codeproject.com/Articles/12153/SetEnv
The choice of a % as a prefix to append to a variable could have been better though, as makes the syntax difficult sometimes if used with other batch local variables...