CMD: Append to path without duplicating it?

12

3

For one CMD session I can easily set a new path: SET PATH=%PATH%;"insert custom path here"

Doing so in a batch file does not consider whether the custom path is already included. How do I avoid duplicating it (i.e. check whether it is already contained in the PATH "string").

Remarks:

  1. Related: How do I append user-defined environment variables to the system variable PATH in Windows 7?
  2. Related: How can I permanently append an entry into the system's PATH variable, via command line?
  3. Same question for UNIX: Add directory to $PATH if it's not already there
  4. Some "CMD" String ops explained: http://ss64.com/nt/syntax-replace.html

Horst Walter

Posted 2012-09-27T10:23:15.393

Reputation: 1 607

It should be pointed out that in this context, the duplication is harmless. It would probably be more sensible to allow the duplication to occur than to try to avoid it and introduce the risk of a false positive. – Harry Johnston – 2012-09-27T22:58:35.013

Answers

14

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

wmz

Posted 2012-09-27T10:23:15.393

Reputation: 6 132

findstr is the wrong utility to use. It will gladly interpret some of the backsplashes as escapes. find works great, though. – Reinstate Monica – 2015-09-04T18:05:27.013

@KubaOber please give an example of incorrect behaviour when used with /c option forcing literal match – wmz – 2015-09-04T19:29:35.950

1@wmz echo d\. | findstr /c:"d\." fails, but echo d\. | find "d\." succeeds – Reinstate Monica – 2015-09-04T19:53:36.280

@KubaOber You're right, another 'feature' of cmd tools :-( Although I tried various combinations and I think those [incorrectly interpreted] sequences will not actually happen in real paths... but just to be safe I will change it to find. Thanks for picking this up! – wmz – 2015-09-04T21:57:23.287

@wmz I only found about it because I ran into a path that triggered it :( It was a relative path with dots, like foo\..\bar – Reinstate Monica – 2015-09-04T22:02:00.247

Nice, I knew there was a cleaner way to do that... still prefer bash, but that's just me :D Thanks for the help! – MaddHacker – 2012-09-27T15:45:21.993

Nice, but does it work with spaces in the path? I have tested it with my particular paths, the one "quoted and with spaces" gets re-added. One just quoted but no spaces seems to work. – Horst Walter – 2012-09-27T16:06:42.780

1@HorstWalter Yes it should work with any path (I've just checked with some including spaces). What do you mean by "quoted"? You do not need to use quotes in paths, even those containing spaces. If your %np% contain quotes - yes, it will cause it to misbehave. – wmz – 2012-09-27T16:36:44.527

I have used "", e.g. "my path". OK, I'll try without quotes, and should have written "in quotes", sorry it was misleading. – Horst Walter – 2012-09-27T16:57:11.960

2@HorstWalter See my edit. This is only an issue if you use variable, using literals (as in echo %path%|findstr /i /c:"my path">nul || set path=%path%;"my path") should work fine. – wmz – 2012-09-27T17:15:20.273

Excellent, thanks to you and also MaddHacker, very nice! I appreciate your efforts. – Horst Walter – 2012-09-27T20:23:02.500

4

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...

MaddHacker

Posted 2012-09-27T10:23:15.393

Reputation: 301

2

Script based around the answer by "wmz" :

@ECHO off
ECHO java_home=%JAVA_HOME%
SET javapath=%JAVA_HOME%\bin
ECHO %path%|findstr /i /c:"%javapath:"=%">nul || set path=%path%;%javapath%
ECHO Path=%path%
PAUSE

djangofan

Posted 2012-09-27T10:23:15.393

Reputation: 2 459