问题
In a .bat script, I want to add a specific list of directory in PATH user variable.
Initially, path is like this :
C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Novell\iPrint;C:\Program Files\WindowsPowerShell\Scripts\HP.ClientScriptLibrary;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\dotnet\;C:\Program Files (x86)\Yarn\bin\;C:\Program Files\Docker\Docker\resources\bin;C:\ProgramData\DockerDesktop\version-bin;C:\Users\myuser\AppData\Local\Microsoft\WindowsApps;C:\Program Files (x86)\Graphviz2.38\bin;
I use this command :
setx WAC_PATH "c:\mydir1;c:\mydir2"
setx Path "%Path%;%%WAC_PATH%%"
I use "%%" because I want to group my new directories in a single variable (WAC_PATH) and use it directly into PATH variable.
As result, The WAC_PATH is correctly define in the user environment variable. But, PATH is altered : many occurrence of initial configuration are duplicated and I have several occurrences of C:\WINDOWS and C:\WINDOWS/SYSTEM32
C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Novell\iPrint;C:\Program Files\WindowsPowerShell\Scripts\HP.ClientScriptLibrary;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\dotnet\;C:\Program Files (x86)\Yarn\bin\;C:\Program Files\Docker\Docker\resources\bin;C:\ProgramData\DockerDesktop\version-bin;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files\Novell\iPrint;C:\Program Files\WindowsPowerShell\Scripts\HP.ClientScriptLibrary;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files\dotnet\;C:\Program Files (x86)\Yarn\bin\;C:\Program Files\Docker\Docker\resources\bin;C:\ProgramData\DockerDesktop\version-bin;C:\Users\myuser\AppData\Local\Microsoft\WindowsApps;C:\Program Files (x86)\Graphviz2.38\bin;;C:\WINDOWS;C:\WINDOWS\System32;C:\WINDOWS;C:\WINDOWS\System32;C:\WINDOWS;C:\WINDOWS\System32;C:\WINDO
I think that variable WAC_PATH is added at the end of PATH but the length exceed 1024.
Why all these values are duplicated (I should have the initial path value with only the reference to WAC_PATH at the end ) ?
来源:https://stackoverflow.com/questions/64837312/add-new-directory-in-path-user-variable-with-setx-dont-work