Search code examples
batch-filedos

How to write a batch file to permanently append a list of directories without redundancies even though we invoke the batch multiple times?


Assume I want to permanently append the following list of directories (in a trimmed version for the sake of simplicity) to my system (as opposed to my local) PATH environment variable.

c:\company\dept\users
d:\school\teachers
e:\school\students

I have to do this by clicking a batch file (with admin privilege set on). If I am not sure I have already done it, I can do clicking the batch file again but without appending redundancies.

Shortly speaking, how can we write such a batch file? I guess we have to make a test for each item in the list whether or not it has been in the PATH. But how to do this?

What I have done so far is

setx PATH "%PATH%;c:\company\dept\users" /m
setx PATH "%PATH%;d:\school\teachers" /m
setx PATH "%PATH%;e:\school\students" /m

but it is not intelligent to prevent redundancies.

I have no knowledge to implement the redundancy test.


Solution

  • As a general approach, I would suggest looking at for with a delims=; to split %PATH% on semicolons, then compare each part to the path you're considering adding.

    If you have specific problems along the way, post them as new questions (or search--they may already be answered) and we'll be happy to answer more.

    You can get documentation on the for loop by typing help for at the command line.

    EDIT: Here's a better answer, from the SuperUser forum: Append to path without duplicating it