如果尚未存在,则如何将新路径插入到系统路径变量中

Ham*_*ava 4 windows batch-file setx

我正在使用以下命令追加到Windows系统PATH变量的路径:

setx PATH "%PATH%;%ProgramFiles%\MySQL\MySQL Server 5.5\bin"
Run Code Online (Sandbox Code Playgroud)

它工作正常.

我的问题是:

如何将路径(在这种情况下为%ProgramFiles%\ MySQL\MySQL Server 5.5\bin)附加到系统PATH变量中,同时还检查它是否已经存在,如果没有,则不添加两次?

Aac*_*ini 10

@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%"
)
Run Code Online (Sandbox Code Playgroud)