windows SETX 不会将路径附加到系统路径变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17240725/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SETX doesn't append path to system path variable
提问by Hamed Kamrava
I have tried below command to append some path to system path variable by batch-file :
我已尝试通过以下命令通过批处理文件将一些路径附加到系统路径变量:
setx PATH "%PATH%;C:\Program Files\MySQL\MySQL Server 5.5\bin"
I have checked system variable path after running above batch-file, above path isn't in there.
我在批处理文件上运行后检查了系统变量路径,上面的路径不在那里。
You can see all windows Variable value
content in below :
您可以Variable value
在下面看到所有窗口内容:
C:\Program Files (x86)\AMD APP\bin\x86_64;C:\Program Files (x86)\AMDAPP\bin\x86;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\ProgramFiles (x86)\ATI Technologies\ATI.ACE\Core-Static;
What am i doing wrong?
我究竟做错了什么?
回答by mojo
To piggy-back on @Endoro's answer (I lack the rep to comment):
附上@Endoro的回答(我没有评论的代表):
If you want to change the system-wide environment variables, you have to use /M, a la:
如果要更改系统范围的环境变量,则必须使用/M,a la:
setx PATH "%PATH%;C:\Program Files\MySQL\MySQL Server 5.5\bin" /M
setx.exe is picky about placement of the /M, BTW. It needs to be at the end.
setx.exe 对 /M 的位置很挑剔,顺便说一句。它必须在最后。
回答by Robert Fey
WARNING!
警告!
setx will truncate the value to 1024 characters.
setx 会将值截断为 1024 个字符。
If you use it to modify PATH you might mess up your system.
如果你用它来修改 PATH 你可能会搞砸你的系统。
You can use this PowerShell snippet to add something to your path:
您可以使用此 PowerShell 代码段向您的路径添加内容:
$new_entry = 'c:\blah'
$old_path = [Environment]::GetEnvironmentVariable('path', 'machine');
$new_path = $old_path + ';' + $new_entry
[Environment]::SetEnvironmentVariable('path', $new_path,'Machine');
In case you want to not re-add an already existing entry something like this will do (see for a better version further down):
如果你不想重新添加一个已经存在的条目,这样的事情会做(请参阅下面的更好版本):
$new_entry = 'c:\blah'
$search_pattern = ';' + $new_entry.Replace("\","\")
$old_path = [Environment]::GetEnvironmentVariable('path', 'machine');
$replace_string = ''
$without_entry_path = $old_path -replace $search_pattern, $replace_string
$new_path = $without_entry_path + ';' + $new_entry
[Environment]::SetEnvironmentVariable('path', $new_path,'Machine');
Here a newer version that I'm using now (2017-10-23). This version handles nested paths correctly. E.g. it handles the case of PATH containing "c:\tool\foo" and you want to add "c:\tool".
这是我现在使用的较新版本(2017-10-23)。此版本正确处理嵌套路径。例如,它处理 PATH 包含“c:\tool\foo”并且您想添加“c:\tool”的情况。
Note, that this expands values that are in path and saves them back expanded. If you want to avoid this, have a look at the comment of @ErykSun below.
请注意,这会扩展路径中的值并将它们保存回扩展状态。如果您想避免这种情况,请查看下面@ErykSun 的评论。
$desired_entry = 'C:\test'
$old_path = [Environment]::GetEnvironmentVariable('path', 'machine');
$old_path_entry_list = ($old_path).split(";")
$new_path_entry_list = new-object system.collections.arraylist
foreach($old_path_entry in $old_path_entry_list) {
if($old_path_entry -eq $desired_entry){
# ignore old entry
}else{
[void]$new_path_entry_list.Add($old_path_entry)
}
}
[void]$new_path_entry_list.Add($desired_entry)
$new_path = $new_path_entry_list -Join ";"
[Environment]::SetEnvironmentVariable('path', $new_path,'Machine');
回答by Endoro
you shouldn't look at the system environment variables but to your user environment variables:
您不应该查看系统环境变量,而应该查看用户环境变量:
回答by chiragchavda.ks
SETX /M Path "%PATH%;%ProgramFiles%\MySQL\MySQL Server 5.5\bin\
It will append your path to system variable
它会将您的路径附加到系统变量
回答by colin lamarre
Should never use setx for a path since it's limited to 1024 chars, as mentioned.
不应该使用 setx 作为路径,因为它被限制为 1024 个字符,如上所述。
Could use reg add:
可以使用 reg 添加:
set pathkey="HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment"
for /F "usebackq skip=2 tokens=2*" %%A IN (`reg query %pathkey% /v Path`) do (reg add %pathkey% /f /v Path /t REG_SZ /d "%%B;C:\Program Files\MySQL\MySQL Server 5.5\bin")
or set pathkey="HKEY_CURRENT_USER\Environment" for user path.
或设置 pathkey="HKEY_CURRENT_USER\Environment" 为用户路径。
Then to broadcast the change:
然后广播更改:
powershell -command "& {$md=\"[DllImport(`\"user32.dll\"\",SetLastError=true,CharSet=CharSet.Auto)]public static extern IntPtr SendMessageTimeout(IntPtr hWnd,uint Msg,UIntPtr wParam,string lParam,uint fuFlags,uint uTimeout,out UIntPtr lpdwResult);\"; $sm=Add-Type -MemberDefinition $md -Name NativeMethods -Namespace Win32 -PassThru;$result=[uintptr]::zero;$sm::SendMessageTimeout(0xffff,0x001A,[uintptr]::Zero,\"Environment\",2,5000,[ref]$result)}"
回答by V SAI MAHIDHAR
I faced the same problem when i tried to add path variables related to fortran (Eclipse for c/c++/fortran)
当我尝试添加与 fortran 相关的路径变量时遇到了同样的问题(Eclipse for c/c++/fortran)
I tried SETX /M Path "%PATH%;C:\Users\mahidhai\cygwin64\bin" in command prompt as administrator .I got warning saying data was truncated to 1024 characters and stored .
我在命令提示符下以管理员身份尝试了 SETX /M Path "%PATH%;C:\Users\mahidhai\cygwin64\bin" 。我收到警告说数据被截断为 1024 个字符并存储。
SOlution: Go to registry file directly. Run->regedit Navigate to Environment
解决方法:直接进入注册表文件。运行->regedit 导航到环境
(Entire path : HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment )
(整个路径: HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment )
Click on path registry and add the path variable directly. Or remove any repeated path variables .
点击路径注册表,直接添加路径变量。或者删除任何重复的路径变量。
Now , open command prompt and then run the same command setx /M path "%path%, "
现在,打开命令提示符,然后运行相同的命令 setx /M path "%path%, "
Path variable could be related to C or C++ or fortran
路径变量可能与 C 或 C++ 或 fortran 相关
No worries in editing registry file , it will be saved permanently , don't be afraid as environment variables are in session manager.
编辑注册表文件不用担心,它会永久保存,不用担心环境变量在会话管理器中。