Related:
How to list the elements of the path in a batch file?
How does FOR work?
How would you write a batch file or CMD file to remove an element from the path? It should handle gracefully:
- differences in case
- shortnames and long names
I've done this using tr.exe but it's slow and complicated and uses temporary files, which makes it even more complicated.
I think the answer is something like this:
setlocal
set tpath=""
set _path="%PATH:;=" "%"
for %%p in (%_path%) do (
call :KeepIfNotEqual %%p %elementToRemove%
)
endlocal & set path=%tpath%
...where %elementToRemove% is the path element to remove. KeepIfUnique would have to be a subroutine that takes two arguments - directory names, normalizes them, and appends the first argument to tpath if it is not equal to the 2nd argument (elementToRemove).
As I said, I can do this with tr.exe, but can I do it with just built-in commands in the windows cmd.exe shell?
EDIT: I guess when you get right down to it, the question is, how to do case-conversion in cmd.exe?
I want to add something to Cheeso's answer... but I don't have enough rep to add a comment.
If you want the script he provided to work even after the batch file is finished, then change the last line like this this:
This employs a neat trick: the 'tpath' variable is available when that line is read-in and parsed by cmd.exe and expanded prior to getting deleted by 'endlocal'.
This allows the changes to the 'PATH' variable to persist even after the batch file exits.
if
command has ignore case optionSee
if /?
for more help on using it. I generally found these commands helpful in determining how to do some cmd line trickery:This is what I came up with, using Igor's hint.