This is a little project I have been trying to figure out ever since I read a line in the help for the "For" command:
%~$PATH:I - searches the directories listed in the PATH
environment variable and expands %I to the
fully qualified name of the first one found.
If the environment variable name is not
defined or the file is not found by the
search, then this modifier expands to the
empty string
Running multiple scripts that continually add to the path list can muck up the path list. I hoped I could design a function that searches to find out if a path exists in the path list and if it does it wont add the new one.
@Echo Off
PATH=C:\WINNT\system32\;C:\WINNT\;C:\Program Files\Test1\
Call :AddToPath "C:\Program Files\Test1\"
Call :AddToPath "C:\Program Files\Test2\LICENSE.txt"
Call :AddToPath "C:\Program Files\Test3\"
Call :AddToPath "C:\Program Files\Test4"
Echo %Path%
GoTo :EOF
:AddToPath
If Not ""=="%~x1" Call :SetToPath "%~dp1"
If "%~1"=="%~dp1" Call :SetToPath "%~1"
If "%~nx1"=="%~n1" If Not ""=="%~nx1" Call :SetToPath "%~1\"
GoTo :EOF
:SetToPath
Set t=%path%
@Rem Deleting the path if it exists
Call Set t=%%t:%~1=%%
If "%path%"=="%t%" Path %Path%;%~1
GoTo :EOF
This is as far I have been able to get the script. For the most part it works except if the new path is already part of the path of another path in the path list. There may be better ways to implement this.
One thing to note is that it is best that the path being added has already been created. Sometimes splitting out the different parts doesn't work if it doesn't exist.
No comments:
Post a Comment