当前位置:网站首页>通过bat脚本配置系统环境变量

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

2022-07-06 09:04:00 vv1025

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

@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 %add_path%

SET toAdd=C:\Program Files\Go\bin
SET MYPATHCOPY=%PATH%
call :search2
echo %add_path%

echo add the path: %add_path%
call set xx=%Path%;%add_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%;%add_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%;%add_path%
goto :EOF

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

原网站

版权声明
本文为[vv1025]所创,转载请带上原文链接,感谢
https://blog.csdn.net/vv1025/article/details/125382010