Pergunta

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?

Foi útil?

Solução

@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%"
)

Outras dicas

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top