如何插入一个新的路径到系统路径变量,如果它已不存在 [英] How to insert a new path into system path variable if it is not already there

查看:196
本文介绍了如何插入一个新的路径到系统路径变量,如果它已不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的命令来追加到windows系统PATH变量的路径:

  SETX PATH%PATH%;的%ProgramFiles%\\ MySQL的\\ MySQL服务器5.5 \\ BIN

它工作正常。

我的问题是:

如何追加的路径(的%ProgramFiles%\\ MySQL的\\ MySQL服务器在这种情况下,5.5 \\ bin中)到系统PATH变量,同时检查它已不存在,如果它不将其添加两次?


解决方案

 关闭@echo
SETLOCAL EnableDelayedExpansion设置pathToInsert =的%ProgramFiles%\\ MySQL的\\ MySQL服务器5.5 \\ BINREM检查pathToInsert是不是已经在系统路径
如果路径:%!pathToInsert%=! EQU%PATH%(
   SETX PATH%PATH%;%pathToInsert%

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?

解决方案

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

这篇关于如何插入一个新的路径到系统路径变量,如果它已不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆