博客原文鏈接
歡迎來我的博客:http://jerkwisdom.github.io/developing/system/dos-current-path/
問題描述
假設我們要在批處理a.bat里調用執行批處理b.bat,b.bat需要知道b.bat的當前位置,并執行run.exe,如下:
// directory structure
// c:
// -a.bat
// -program
// -b.bat
// -run.exe
// a.bat
call "%cd%\program\b.bat"
// b.bat
"%cd%\run.exe"
那么現在能不能成功執行run.exe呢?
問題分析
%cd%和%~dp0都能用來表示當前目錄,但是他們在不同的使用場景下,功能卻不相同:
- %cd%代表的是當前工作目錄(current working directory),為變量;
- %~dp0代表的是當前批處理文件所在完整目錄(the batch file's directory),為常量。
我們來看看下面的例子:
// directory structure
// c:
// -c.bat
// -program
// -d.bat
// c.bat
call "%cd%\program\d.bat"
// d.bat
@echo off
echo cd = %cd%
echo dp0 = %~dp0
直接運行d.bat,結果為
cd = C:\program
dp0 = C:\program\
直接運行c.bat,結果為
cd = C:\
dp0 = C:\program\
從上面的結果可以看出:
- 執行d.bat時,當前工作目錄為d.bat所在目錄;
- 執行c.bat時,當前工作目錄為c.bat所在目錄,即使在調用d.bat后,該工作目錄依舊是c.bat所在目錄。
問題解決
讓我們再來看看問題描述中提及的問題——能不能成功執行run.exe呢?
答案是:不能?!?cd%\run.exe”表示的是“C:\run.exe”,并非“C:\program\run.exe”。那么如何更改呢?有兩種方案:
// plan A
// change the current working directory
// a.bat
cd "%~dp0"
call "%cd%\program\b.bat"
// b.bat
cd "%~dp0"
"%cd%\run.exe"
// plan B
// using %~dp0 directly
// a.bat
call "%~dp0program\b.bat"
// b.bat
"%~dp0run.exe"
問題延伸
上面的解決方案中plan A通過更改當前目錄來解決該問題,可以這里面也存在另外一個問題,讓我們看下面的例子:
// directory structure
// c:
// -program
// -f.bat
// d:
// -e.bat
// plan A
// change the current working directory
// e.bat
cd "%~dp0"
call "c:\program\f.bat"
// f.bat
cd "%~dp0"
"%cd%\run.exe"
現在e.bat和f.bat不在同一個盤符了,從e.bat切換當前工作目錄到f.bat直接使用cd是不行的,必須要使用:
cd /d "%~dp0"
這個地方容易疏忽,切記不要犯錯。
問題總結
我們來重申下%dp0和%cd%的區別,%cd%和%dp0都能用來表示當前目錄,但是他們在不同的使用場景下,功能卻不相同:
- %cd%代表的是當前工作目錄(current working directory,variable);
- %~dp0代表的是當前批處理文件所在完整目錄(the batch file's directory,fixed)。
從目前我們的使用情況來看,盡量使用%~dp0,不建議使用%cd%,有其他需求除外。