關於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