Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
share|improve this question
    
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 Sep 27 '12 at 22:58

3 Answers 3

up vote 5 down vote accepted

Similar to MaddHackers answer, just more compact.
echo %path%|findstr /i /c:"%np%">nul || set path=%path%;%np%

%np% is your new path, of course you can use literals instead. What it does: echo %path%|findstr /i /c:"%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%|findstr /i /c:"%np:"=%">nul || set path=%path%;%np%

share|improve this answer
    
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 Sep 27 '12 at 15:45
    
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 Sep 27 '12 at 16:06
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 Sep 27 '12 at 16:36
    
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 Sep 27 '12 at 16:57
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 Sep 27 '12 at 17:15

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

share|improve this answer
    
Thanks for all your effort so far! –  Horst Walter Sep 27 '12 at 16:25

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.