0

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 ) ?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Broshet
  • 133
  • 12
  • Seems very strange. Your original `path` setting appears to be resolved to ~508 characters and there's no apparent appearance of `c:\mydir1;c:\mydir2` in your "after" version, which seems to have the first ~415 characters duplicated and `;C:\WINDOWS;C:\WINDOWS\System32` repeatedly appended. I'd suggest you use `regedit32` to read the actual definitions of `path` and `wac_path`and if the problem doesn't appear to be resolvable, edit your question to include those actual definitions. – Magoo Nov 14 '20 at 18:55
  • 4
    You should not use `setx.exe` to modify the value data for the `%Path%` environment variable. The reason for that is that unlike other variables, the `%Path%` value is special in that its value is the content of both the System `%Path%` variable value and the User `%Path%` variable value. Your command is therefore adding all of the content of the system variable, to your user variable, thus duplicating all of those. – Compo Nov 14 '20 at 19:09
  • Thank you, I understand better. Is there a method to update the PATH variable (user) in bat script? – Broshet Nov 14 '20 at 19:38
  • You could parse the expandable string value `Path` in the registry, `HKEY_CURRENT_USER\Environment`, and append your new value data to that. You could also leverage `powershell.exe` to append your data. – Compo Nov 14 '20 at 20:37
  • 1
    See [Why are other folder paths also added to system PATH with SetX and not only the specified folder path?](https://stackoverflow.com/a/25919222/3074564) and [Adding the current directory to Windows path permanently](https://stackoverflow.com/a/47080452/3074564) and [How to search and replace a string in environment variable PATH?](https://stackoverflow.com/a/24650324/3074564) and for the opposite [How can I use a .bat file to remove specific tokens from the PATH environment variable?](https://stackoverflow.com/a/38664286/3074564) – Mofi Nov 14 '20 at 23:18
  • 1
    Well, you have to repair first your __user__ `Path` environment variable according to [System cannot find path even though it exists in paths](https://stackoverflow.com/a/57442052/3074564). Note: The only reason for adding one or more folder paths directly or via a different environment variable to __user__ or __system__ `Path` is that an application or script installed with a batch file is mainly executed by the user from within a command prompt window with entering just its file name. No good coded application/script depends on which folders are in `Path` other than the Windows defaults. – Mofi Nov 15 '20 at 10:47

1 Answers1

0

Thank you Mofi,

I have check your link for cleaning the Path and it's OK for me.

Broshet
  • 133
  • 12