CMD: Append to path without duplicating it?
Similar to MaddHackers answer, just more compact.echo %path%|find /i "%np%">nul || set path=%path%;%np%
%np%
is your new path, of course you can use literals instead.
What it does: echo %path%|find /i "%np%">nul
searches existing path for a string, discarding output. ||
means execute on failure, so it means:
Search path for string to be added, and if not found, add it.
Edit: Generally it's not required to quote paths, even those containig spaces, but if you do want to quote them, this version will work with double quoted paths:echo %path%|find /i "%np:"=%">nul || set path=%path%;%np%
Edit: changed findstr /i /c:
to find /i
as findstr
may misinterpret some sequences as noted by KubaOber in comments
I know it's dirty, but this should work:
SET PATH=$(echo $PATH | awk -F"%checkstr%" '{ print ($(NF-1) ~ 0) ? $PATH:%newpath% : $PATH; }')
where %checkstr%
is the string to check for in the PATH and %newpath%
is the new PATH to append to the current PATH
Hope that helps, and I'm sure there's an easier way, but that should do the trick for now.
EDIT
So if you don't want to add the UNIX tools to Windows, you can try this batch syntax:
echo %PATH% | find /C /I "<string to find>" > out.txt
set /p dne= < out.txt
if 0 EQU %dne% (set PATH=%PATH%;"<custom path>")
del out.txt
Ugh, not any prettier, and can be done as one line if you can get the pipe'ed find command to work as a subcommand in the if statement...