批處理,修改環境變數path的方法(加環境變數)

來源:互聯網
上載者:User

標籤:

方法一:批處理中,修改環境變數,一次性有效(也就是在當前的指令碼中有效)

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

批處理,修改環境變數path的方法(加環境變數)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.