windows指令碼(.bat): %~dp0 是什麼意思

來源:互聯網
上載者:User

下文轉自:

http://www.cnblogs.com/smwikipedia/archive/2009/03/30/1424749.html

 

 

更改目前的目錄為批處理本身的目錄
有些暈吧?不急,我舉例
比如你有個批處理a.bat在D:/qq檔案夾下  
a.bat內容為
cd /d %~dp0
在這裡
cd /d %~dp0的意思就是cd /d d:/qq
%0代表批處理本身 d:/qq/a.bat
~dp是變數擴充
d既是擴充到分區號 d:
p就是擴充到路徑 /qq
dp就是擴充到分區號路徑 d:/qq

擴充變數文法詳解:

:: 選項文法:
:: ~I - 刪除任何引號("),擴充 %I
:: %~fI - 將 %I 擴充到一個完全合格的路徑名
:: %~dI - 僅將 %I 擴充到一個磁碟機代號
:: %~pI - 僅將 %I 擴充到一個路徑
:: %~nI - 僅將 %I 擴充到一個檔案名稱
:: %~xI - 僅將 %I 擴充到一個副檔名
:: %~sI - 擴充的路徑只含有短名
:: %~aI - 將 %I 擴充到檔案的檔案屬性
:: %~tI - 將 %I 擴充到檔案的日期/時間
:: %~zI - 將 %I 擴充到檔案的大小
:: %~$PATH:I - 尋找列在路徑環境變數的目錄,並將 %I 擴充
:: 到找到的第一個完全合格的名稱。如果環境變數名
:: 未被定義,或者沒有找到檔案,此按鍵組合會擴充到
:: Null 字元串
:: 可以組合修飾符來得到多重結果:
:: %~dpI - 僅將 %I 擴充到一個磁碟機代號和路徑
:: %~nxI - 僅將 %I 擴充到一個檔案名稱和副檔名
:: %~fsI - 僅將 %I 擴充到一個帶有短名的完整路徑名
:: %~dp$PATH:i - 尋找列在路徑環境變數的目錄,並將 %I 擴充
:: 到找到的第一個磁碟機代號和路徑。
:: %~ftzaI - 將 %I 擴充到類似輸出線路的 DIR

----------------------------

http://www.911cd.net/forums/lofiversion/index.php/t3730.html

Look at this:
Manipulating variables in CMD shell
I'm not sure why this type of information isn't more prominent in the help files, but there you go. NT's command shell can manipulate variables but the operations you can perform are fairly limited. Still, better than nothing. If you want to do some really clever stuff then you're going to have to look elsewhere. Vbscript can be useful as it's got lots of string handling capabilities. There's also Perl and AWK which are Windows (windoze?) ports of some very powerful Unix commands.

%1 is your command line option.

Namely: mycommand.cmd myoption1.

%0 determins where the batch file is running from. I've created a demo batch file in my winnt system32 folder called x.cmd. Running this gives the results shown below

%~f1 expands %1 to the drive, path and file name. If you pass %1 from the current directory then this expands that variable to it's full path

echo f0 = %~f0 produces f0 = c:/WINNT/system32/x.cmd
%~d1 gets the drive letter from %1
echo d0 = %~d0 produces d0 = D
%~p1 extracts the path from variable %1
echo p0 = %~p0 produces /WINNT/system32/
%~dp1 pulls the drive letter and path
echo dp0 = %~dp0 produces C:/WINNT/system32/
%~sp1 creates a short path (but no drive letter)

echo sp0 = %~sp0 produces /WINNT/system32

If I set %1 to "c:/Program Files/Internet Explorer" then %~sp1 produces /PROGRA~1/INTER. Note you have to wrap the long path in quotes otherwise the truncation doesn't work.
%~x1 leaves only the command extension
echo x0 = %~x0 produces .cmd
%~nx1 extracts the file name and extension
echo nx0 = %~nx0 produces x.cmd
%~sx1 extracts the short extension from %0
echo sx0 = %~sx0 produces .cmd but a longer extension (.document?) would be cut down to .doc

----------------------------

%~dp0 VS %cd%

http://www.computerhope.com/forum/index.php?topic=54333.0

%cd% is available either to a batch file or at the command prompt and expands to the drive letter and path of the current directory (which can change e.g. by using the CD command)

%~dp0 is only available within a batch file and expands to the drive letter and path in which that batch file is located (which cannot change). It is obtained from %0 which is the batch file's name.

An experiment like the following shows the difference

Here is D:/dirshow.bat:

Code:@echo off
echo this is %%cd%%  %cd%
echo this is %%~dp0 %~dp0

Run it from C:/ and this is what you see

Code:C:/>D:/dirshow.bat
this is %cd%  C:/
this is %~dp0 D:/
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.