bat设置环境变量

转自 https://blog.csdn.net/ITBread/article/details/119787985

 setx /m 变量名 变量值设置永久环境变量(如果 去掉 /m 则是 临时设置,cmd窗口关闭后就失效),其他选项 自行了解熟悉。

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
@echo off
echo .
echo  ------------------用管理员权限运行 -----------------
echo  ----------------------- 准备 -----------------------
echo.
 
rem  tarDir 是  python 安装目录
set tarDir=d:\dev\python
rem  pyLibDir 是  python Lib 目录
set pyLibDir=d:\dev\python\Lib
 
rem "打印一下 目录信息"
echo "---python_Dir   ---- %tarDir%"
echo "---python_Lib_Dir---- %pyLibDir%"
 
set remain=%path%
 
set finded=false
set findedLib=false
rem "以下目的是查找环境变量里面是否已经设置了,如果没有设置才设置"
:loop
for /f "tokens=1* delims=;" %%a in ("%remain%") do (
   if "%tarDir%"=="%%a" (
      set finded=true
      echo "path find 1============== %tarDir%"
   )
 
   if "%pyLibDir%"=="%%a" (
      set findedLib=true
      echo "path find 2============== %pyLibDir%"
   )
 
   set remain=%%b
)
 
if defined remain goto :loop
 
if "%finded%"=="false" (
    if "%findedLib%"=="false" (
       echo "设置环境变量"
       setx /m "path" "%pyLibDir%;%tarDir%;%path%"
    ) else (
       setx /m "path" "%tarDir%;%path%"
    )
)
 
if "%finded%"=="true" (
    if "%findedLib%"=="false" (
       echo "设置环境变量"
       setx /m "path" "%pyLibDir%;%path%"
    )
)
echo  ----------------------- 完成 -----------------------
pause

  

posted @   小毛编  阅读(683)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示