bat腳本設置系統環境變量即時生效


關於bat的資料多但零碎,記錄一下。

1.設置環境變量即時生效:通過重啟explorer來實現即時生效(親測有效)

  @echo off

  set curPath=%cd%

  wmic ENVIRONMENT where "name='path' and username='<system>'" set VariableValue="%curPath%tool\dig;%path%" 

  taskkill /im explorer.exe /f
  @echo ================================================
  @echo 下面開始重啟“explorer.exe”進程
  pause
  start explorer.exe

 

2.變量是否包含另一個變量判斷

  set mypath=%path%

  set curPath=%cd%

  set digPath=%curPath%tool\dig
  echo %mypath% | find /i "%digPath%">nul && goto A || goto B
  :A /////系統dig環境變量已經存在
  echo 當前環境變量包含%digPath%
  exit

  :B /////不存在

  echo 當前環境變量不包含%digPath%
  exit

  注意:變量中是否包含字符串,使用同樣的方法:echo %mypath% | find /i "ABC">nul && goto A || goto B

3.bat中打開exe程序:打開exe程序前需要先切換到exe所在的根目錄

  ::切換到exe所在目錄

  cd %curPath%tool
  
::啟動程序
  start "" "%curPath%tool\operateToolWPF.exe"

關於bat批處理,推薦一個網站,資料很全:http://www.bathome.net

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM