方法一:批處理中,修改環境變量,一次性有效(也就是在當前的腳本中有效)
CMD中運行
set path==%path%;d:/mypath
用 set path可以查看,當前的環境變量
方法二 :批處理中,修改環境變量,永久有效
::更改path環境變量值,新增e:\tools
wmic ENVIRONMENT where "name='path' and username='<system>'" set VariableValue="%path%;e:\tools"
另外介紹些關於wminc的用法(簡單又實用)
::獲取temp環境變量
wmic ENVIRONMENT where "name='temp'" get UserName,VariableValue
::新增系統環境變量home,值為%HOMEDRIVE%%HOMEPATH%
wmic ENVIRONMENT create name="home",username="<system>",VariableValue="%HOMEDRIVE%%HOMEPATH%"
::刪除home環境變量
wmic ENVIRONMENT where "name='home'" delete
::獲取temp環境變量
wmic ENVIRONMENT where "name='temp'" get UserName,VariableValue
::更改path環境變量值,新增e:\tools
wmic ENVIRONMENT where "name='path' and username='<system>'" set VariableValue="%path%;e:\tools"
::新增系統環境變量home,值為%HOMEDRIVE%%HOMEPATH%
wmic ENVIRONMENT create name="home",username="<system>",VariableValue="%HOMEDRIVE%%HOMEPATH%"
::刪除home環境變量
wmic ENVIRONMENT where "name='home'" delete
一般來說,wmic創建或修改的系統變量不用重啟windows生效,但如果你發現在DOS窗下檢測不生效的話,你試着關閉 DOS窗,再檢測一次.就生效了.
如果你的批處理想不關閉而直接讓新的變量給下級程序應用可以這樣寫
view plaincopy to clipboardprint?
::檢查path中有沒有e:\tools(有就跳到run,沒有就接着執行)
echo %path%|findstr /i "e:\tools"&&(goto run)
::先添加,防止沒有時修改出錯
wmic ENVIRONMENT create name="path",VariableValue="e:\tools;%path%"
::再修改,防止已有時添加出錯
wmic ENVIRONMENT where "name='path' and username='<system>'" set VariableValue="e:\tools;%path%"
::再即時應用
set "path=e:\tools;%path%"
:run
start 程序.exe
::檢查path中有沒有e:\tools(有就跳到run,沒有就接着執行)
echo %path%|findstr /i "e:\tools"&&(goto run)
::先添加,防止沒有時修改出錯
wmic ENVIRONMENT create name="path",VariableValue="e:\tools;%path%"
::再修改,防止已有時添加出錯
wmic ENVIRONMENT where "name='path' and username='<system>'" set VariableValue="e:\tools;%path%"
::再即時應用
set "path=e:\tools;%path%"
:run
start 程序.exe
來源: https://www.cnblogs.com/wzxblog/p/5731699.html