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.