如果尚未附加,如何附加到Path

时间:2014-04-02 01:56:03

标签: windows batch-file environment-variables

我想将%MYSQL_HOME%\bin%CATALINA_HOME%\bin附加到Windows上的Path系统环境变量中,但我想仅在附加时才附加这些追加。如何在命令提示符或批处理脚本中执行此操作?

4 个答案:

答案 0 :(得分:1)

@ECHO OFF
SETLOCAL
FOR %%a IN (skipsql skipcat) DO SET "%%a="
FOR /f "delims=" %%a IN ('"echo %path:;=&ECHO(%"') DO (
IF /i "%%a"=="%MYSQL_HOME%\bin" SET skipsql=Y
IF /i "%%a"=="%CATALINA_HOME%\bin" SET skipcat=Y
)
IF NOT DEFINED skipsql SET "path=%path%;%MYSQL_HOME%\bin"
IF NOT DEFINED skipcat SET "path=%path%;%CATALINA_HOME%\bin"
ECHO new path=%path%
GOTO :EOF

这对你有用。请记住,它仅适用于cmd的当前实例 - 它不会传输到现有或未来的cmd实例。

答案 1 :(得分:1)

这是另一个解决方案:

path|find /i "%MYSQL_HOME%\bin"    >nul || set path=%path%;%MYSQL_HOME%\bin
path|find /i "%CATALINA_HOME%\bin" >nul || set path=%path%;%CATALINA_HOME%\bin

答案 2 :(得分:0)

这是一个使用临时文件的错综复杂的方法:

set PATH_BACKUP=%PATH%
path | findstr "%MYSQL_HOME%\bin" > _tmp.txt
path=%PATH%;%MYSQL_HOME%\bin
for /f %%i in (_tmp.txt) do path=%PATH_BACKUP%
del _tmp.txt

set PATH_BACKUP=%PATH%
path | findstr "%CATALINA_HOME%\bin" > _tmp.txt
path=%PATH%;%CATALINA_HOME%\bin
for /f %%i in (_tmp.txt) do path=%PATH_BACKUP%
del _tmp.txt

如果需要,将_tmp.txt替换为其他一些丢弃文件名。

这是批处理文件。如果从命令行使用,请将%% i替换为%i。

答案 3 :(得分:0)

@echo off
setlocal EnableDelayedExpansion

for %%a in ("%MYSQL_HOME%\bin" "%CATALINA_HOME%\bin") do (
   if "!path:%%~a=!" equ "!path!" set "path=!path!;%%a"
)

ECHO New path=%path%