通过bat脚本配置系统环境变量

2023-07-08 14:19:33 浏览数 (1)

通过bat脚本配置系统环境变量

代码语言:javascript复制
@echo off

echo ---------------------------------------
set pan=%~d0
set filePath=%~p0 
set filePath=%pan%%filePath%
echo current path: %filePath%

REM 添加PATH环境变量:如果已经存在则不添加
echo ---------------------------------------
SET add_path=

SET toAdd=%~dp0
SET MYPATHCOPY=%PATH%
call :search1
echo �d_path%

SET toAdd=C:Program FilesGobin
SET MYPATHCOPY=%PATH%
call :search2
echo �d_path%

echo add the path: �d_path%
call set xx=%Path%;�d_path%
wmic ENVIRONMENT where "name='Path' and username='<system>'" set VariableValue="%xx%"

pause
REM TIMEOUT /T 10

REM 方法1
:search1
for /f "tokens=1* delims=;" %%a in ("%MYPATHCOPY%") do (
	if "%toAdd%"=="%%a" (
		goto :isFinded
	)
	set MYPATHCOPY=%%b
    goto :search1
)
set add_path=%toAdd%;�d_path%
goto :EOF

REM 方法2
:search2
for /f "delims=; tokens=1,2*" %%p in ("%MYPATHCOPY%") do (
   REM @echo %%~p
   SET MYPATHCOPY=%%~q;%%~r
   if "%toAdd%"=="%%p" (
        REM echo %%p
	    goto :isFinded
    )
	goto :search2
)
set add_path=%toAdd%;�d_path%
goto :EOF

:isFinded
echo The path already exists: %toAdd%
goto :EOF

0 人点赞