4

I'm using the below command to append a path to windows system PATH variable :

setx PATH "%PATH%;%ProgramFiles%\MySQL\MySQL Server 5.5\bin"

It works fine.

My question is:

How to append a path (%ProgramFiles%\MySQL\MySQL Server 5.5\bin in this case) into system PATH variable while also checking that it is not already there, and not adding it twice if it does?

Hamed Kamrava
  • 12,359
  • 34
  • 87
  • 125

2 Answers2

11
@echo off
setlocal EnableDelayedExpansion

set "pathToInsert=%ProgramFiles%\MySQL\MySQL Server 5.5\bin"

rem Check if pathToInsert is not already in system path
if "!path:%pathToInsert%=!" equ "%path%" (
   setx PATH "%PATH%;%pathToInsert%"
)
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
Aacini
  • 65,180
  • 12
  • 72
  • 108
  • 1
    In my point of view it is not advisable to use value of local **PATH** to update system **PATH**. For the reasons see my answer on [Why are other folder paths also added to system PATH with SetX and not only the specified folder path?](http://stackoverflow.com/a/25919222/3074564) – Mofi Sep 19 '14 at 13:48
  • 1
    @TrevorHickey: Although you are right in the reasons you had to edit my answer, you should realize that this topic is more than 2 and a half years old and that the deleted part is really insignificant. I suggest you to spend your time in more productive tasks. – Aacini Jan 14 '16 at 20:38
  • 1
    @Aacini I spent less time making the edit than you did commenting about it. – Trevor Hickey Jan 14 '16 at 22:21
  • @TrevorHickey: Yes, this means that your edit distracted both you and me from doing more productive tasks... **`:/`** – Aacini Jan 15 '16 at 00:11
  • @Aacini I totally agree with you, fed up about people trying to edit every post to get some little points. Maybe they think santa will get them more present I don't know – Antoine D Sep 14 '22 at 13:12
-1

I'd think the easiest way is to check if it exists, and then append to it if it does, or just directly write onto it if it doesn't. From your tags I assume you are trying to do this from a batch file. This page seems to contain an example that perfectly fits your need:

IF "%PATH%" == "" GOTO NOPATH
:YESPATH
@ECHO The PATH environment variable was detected.
PATH=C:\DOS;%PATH%
GOTO END
:NOPATH
@ECHO The PATH environment variable was NOT detected.
PATH=C:\DOS;
GOTO END
:END

This batch code would add C:\DOS to path, just substitute it with what you'd want to use. And of course you might want to remove the echo lines or disable echoing alltogether if you don't want the messages to appear.

Kryomaani
  • 152
  • 8
  • %PATH% has many application paths. When i'm trying echo `%PATH%` it show many paths. And `C:\Program Files\MySQL\MySQL Server 5.5\bin` is among %PATH% variable. Your code just checking if `%PATH%` is empty or it's not ,append `C:\DOS` then. I need to check if 'C:\DOS' not exist among `%PATH%` append it. – Hamed Kamrava Jun 13 '13 at 12:53
  • @HamedKamrava Sorry, I misunderstood your question. Well that explains a lot, I was already wondering why someone wanted to check if %PATH% existed, as if it usually didn't... – Kryomaani Jun 13 '13 at 13:01