1

It's easy enough to add a directory to a variable and to an environment variable by commandline. It took a lot of struggle, but I was able to remove a substring from a variable as you can see down below (although it did leave pesky % sign for some reason). At any rate, as you can see below what works with set does not work with setx. Is there any way do remove directories/paths from the %PATH% environment variable by commandline?

For testing I did, echo %path%:

C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\WINDOWS\System32\LibreSSL\;C:\ProgramData\chocolatey\bin;C:\Program Files\PuTTY\;C:\Program Files\Git\cmd;C:\addtopath;C:\Users\thisguy\AppData\Local\Microsoft\WindowsApps;;C:\Program Files\Microsoft VS Code\bin;\\test\test

What I want to do here is remove the \\test\test at the very end by commandline. So,

set cut=\\test\test
echo %cut%
\\test\test

Next, I set %path% to oldname:

set oldname=%path%
echo %oldname%
C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\WINDOWS\System32\LibreSSL\;C:\ProgramData\chocolatey\bin;C:\Program Files\PuTTY\;C:\Program Files\Git\cmd;C:\addtopath;C:\Users\thisguy\AppData\Local\Microsoft\WindowsApps;;C:\Program Files\Microsoft VS Code\bin;\\test\test

It works, so far so good.

Now the trick to removing \\test\test is using call with some basic variable editing. Go ahead, try this some other way, it took me forever to figure this part out:

call set newname=%%oldname:%cut%=%%
echo %newname%
C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\WINDOWS\System32\LibreSSL\;C:\ProgramData\chocolatey\bin;C:\Program Files\PuTTY\;C:\Program Files\Git\cmd;C:\addtopath;C:\Users\thisguy\AppData\Local\Microsoft\WindowsApps;;C:\Program Files\Microsoft VS Code\bin;% **<--------(pesky percent sign lingers)**

Genius! I'm super happy right now. So, all is pretty good for the most part, right? Well check this out:

call setx path=%%path:%cut%=%%

And what do I get?

ERROR: Invalid syntax. Default option is not allowed more than '2' time(s).

It should work exactly the same, shouldn't it? I don't care how this gets solved, I just want to be able to delete a directory location from %PATH% from commandline.

Compo
  • 36,585
  • 5
  • 27
  • 39
Adrian Nielsen
  • 11
  • 1
  • 2
  • 4
  • Welcome! Please post your code and results as text in the question. – kismert May 11 '18 at 23:58
  • `setx` writes to `HKCU` hive by default if the `/M` argument is not used. So you are updating the value in the `PATH` key in the registry key of `HKCU\Environment`?. **Note**: `%PATH%` environmental variable is a merged value of `PATH` from the hives of `HKLM` and of `HKCU`. – michael_heath May 12 '18 at 01:35
  • Helps if you actually read the help file for the command before you try to use it and come asking for help. – Squashman May 12 '18 at 04:45
  • Just to note, _because you have several repeated entries_, when you've finished messing with `%PATH%`, it should look like this: `C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\WINDOWS\System32\OpenSSH\;C:\WINDOWS\System32\LibreSSL\;C:\ProgramData\chocolatey\bin;C:\Program Files\PuTTY\;C:\Program Files\Git\cmd;C:\Users\thisguy\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Microsoft VS Code\bin`. I assumed that `C:\addtopath` wasn't supposed to be there either, so I removed it, _please feel free to add it to the end if it's needed!_ – Compo May 12 '18 at 13:16
  • michael_heath, I'm not using the /m argument because I don't want it to affect the whole system but myself only. Good observation on how %PATH% merges HKLM and HKCU, which accounts for *some* duplicates when I echo %PATH%. Mofi, thanks for your input, I cleaned it up! – Adrian Nielsen May 13 '18 at 06:24

1 Answers1

1

I'd investigate why that terminal % persists - it doesn't on my machine.

Your problem appears to be that since the resultant path contains spaces, you need to quote the value to be assigned. It would also help if you were to use the correct syntax for setx - the = is at best superfluous.

call setx path "%%path:%cut%=%%"

should work happily, although I'd suggest you set a dummy variable for testing, just in case as path is a critical variable.

Magoo
  • 77,302
  • 8
  • 62
  • 84