使用批处理脚本创建环境变量的做法

2025-09-28 10:32:44

1、1. 新建一个文本文件:SetEnvironmentVariable.bat,输入如下脚本代码:

rem 作者:explorer101

rem 请注意用管理员权限运行该批处理文件,否则会出现find命令无法识别的错误

@echo off

ver | find "4.0." > NUL &&  goto win_xp    

ver | find "4.10." > NUL &&  goto win_xp   

ver | find "4.90." > NUL &&  goto win_xp   

ver | find "3.51." > NUL &&  goto win_xp   

ver | find "5.0." > NUL &&  goto win_xp    慎矿爱

ver | find "5.1." > NUL &&  goto win_xp    

ver | find "5.2." > NUL &&  goto win_xp    

ver | find "6.0." > NUL &&  goto win7   

ver | find "6.1." > NUL &&  goto win7    

ver | find "6.2." > NUL &&  goto win8   

ver | find "6.3." > NUL &&  goto win8  

:win_xp 

rem 设置第三方库环境变量THIRD_PARTY

wmic ENVIRONMENT where "name='BOOST_LIB'" delete

wmic ENVIRONMENT create name="BOOST_LIB",username="<system>",VariableValue="D:\work\boost"

echo %BOOST_LIB%

goto end 

:win7

@setx BOOST_LIB "D:\work\boost" -m

echo %BOOST_LIB%

goto end 

:win8

@setx BOOST_LIB "D:\work\boost" -m

echo %BOOST_LIB%

goto end

:end

pause

   然后把文件保祝返存为bat文件。

2、以管理员身份运行SetEnvironmentVariable.bat,如下图购暗:

使用批处理脚本创建环境变量的做法

使用批处理脚本创建环境变量的做法

3、通过点击“我的电脑”,选择属性,找到并打开“高级系统配置”。在接下来的页面中点击“高级系统配置”,打开“系统属性”窗口,找到“环境变量”按钮并点击,可以看到环境变量已经设置上了,如下图:

使用批处理脚本创建环境变量的做法

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
猜你喜欢