Microsoft Windows batch file 中的%〜d0是什麼意思? batch file 可以寫小自動化安裝程式!

因為我們公司現在安裝 Microsoft Office 2019 是用 batch file 自動安裝。所以當你把 Office 2019 下載來到 C:\ 裡時,此時必須更改 " batch file 自動安裝 Office 2019 檔案路徑 " 和 " ProPlus2019.xml 檔案裡的路徑  " 。然後才能成功執行 batch file 自動安裝 。

Step 1: 更改 " batch file 自動安裝 Office 2019 檔案路徑 "


Step 2:  更改 "ProPlus2019.xml 檔案裡的路徑  "



關於 Windows batch file 簡單說明如下: 

是的,您可以使用下面列出的其他快捷方式。 在您的命令中,〜d0表示第0個參數的驅動器號。〜擴展給定變量

d僅獲取驅動器號 。0是您要引用的參數

腳本所在的路徑(無驅動器):〜p0   。  腳本所在的驅動器:〜d0


Run 2:

D:\Workbench>batch c:\123\a.exe e:\abc\b.exe

~dp0= D:\Workbench\
~dp1= c:\123\
~dp2= e:\abc\

%~dp0 批次檔所在路徑,例如 C:\Program Files\Mozilla Firefox\ 或 UNC 路徑,例如 \\Server\Share\Program Files\Mozilla Firefox\


%~d0 批次檔所在磁碟代號,例如 C: 或 UNC 路徑的雙反斜線 \\


%~p0 批次檔所在路徑,不含磁碟代號,例如 \Program Files\Mozilla Firefox\ 或開頭不帶雙反斜線的 UNC 路徑,例如 Server\Share\Program Files\Mozilla Firefox\


%cd% 目前工作路徑,非根路徑時後面不帶反斜線,例如 C:\Program Files\Mozilla Firefox

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

關於 %〜dp0變量

在Windows批處理文件中引用時,%〜dp0(為零)變量將擴展為該批處理文件的驅動器號和路徑。變量%0-%9引用批處理文件的命令行參數。 %1-%9在批處理文件名之後引用命令行參數。 %0是指批處理文件本身。

如果在百分號(%)後面加上波浪號(〜),則可以在參數編號之前插入修飾符,以更改變量的擴展方式。 d修飾符擴展為驅動器號,而p修飾符擴展為參數的路徑。

範例:假設您在C:上有一個名為bat_files的目錄,並且在該目錄中有一個名為example.bat的文件。 在這種情況下,%〜dp0(結合d和p修飾符)將擴展到C:\ bat_files。

查看這篇Microsoft文章以獲取完整說明。

另外,請查看此論壇主題。


從這裡獲得更清晰的參考:

%CmdCmdLine%將返回傳遞給CMD.EXE的整個命令行

%*將返回從第一個命令行參數開始的命令行的其餘部分(在Windows NT 4中,%*還包括所有前導空格)

如果%n是有效路徑或文件名(無UNC),則%〜dn將返回%n的驅動器號(n的範圍可以從0到9)。

如果%n是有效路徑或文件名(無UNC),則%〜pn將返回%n的目錄

如果%n是有效的文件名,%〜nn將僅返回%n的文件名

如果%n是有效的文件名,%〜xn將僅返回%n的文件擴展名

如果%n是有效的文件名或目錄,則%〜fn將返回%n的標準路徑。


Add-1

剛剛為神秘的〜波浪號運算符找到了一些很好的參考。

%〜字符串稱為百分比波浪號運算符。 您可以在以下情況下找到它:%〜0。

:〜字符串稱為冒號波浪號運算符。 您可以找到它,例如%SOME_VAR:〜0,-1%。


ADD 2 - 1:12 PM 7/6/2018

%1-%9是指命令行參數。 如果它們不是有效的路徑值,則%〜dp1-%〜dp9都將擴展為與%〜dp0相同的值。 但是,如果它們是有效的路徑值,它們將擴展為自己的驅動程序/路徑值。

For example: (batch.bat)

@echo off
@echo ~dp0= %~dp0
@echo ~dp1= %~dp1
@echo ~dp2= %~dp2
@echo on

Run 1:

D:\Workbench>batch arg1 arg2

~dp0= D:\Workbench\
~dp1= D:\Workbench\
~dp2= D:\Workbench\

Run 2:

D:\Workbench>batch c:\123\a.exe e:\abc\b.exe

~dp0= D:\Workbench\
~dp1= c:\123\
~dp2= e:\abc\
------------------------------------------------------------------------------------

它們是增強的變量替換。 它們修改批處理文件中使用的%N變量。 如果您要在Windows中進行批處理編程,那將非常有用。

%~I         - expands %I removing any surrounding quotes ("")
%~fI        - expands %I to a fully qualified path name
%~dI       - expands %I to a drive letter only
%~pI       - expands %I to a path only
%~nI       - expands %I to a file name only
%~xI       - expands %I to a file extension only
%~sI       - expanded path contains short names only
%~aI       - expands %I to file attributes of file
%~tI        - expands %I to date/time of file
%~zI        - expands %I to size of file
%~$PATH:I   - searches the directories listed in the PATH environment variable and expands %I to the
                       fully qualified name of the first one found. If the environment variable name is not
                       defined or the file is not found by the search, then this modifier expands to the
                       empty string

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

由於第0個參數是腳本路徑,它將為您獲取路徑的驅動器號。 您可以使用以下快捷方式
%~1         - expands %1 removing any surrounding quotes (")
%~f1        - expands %1 to a fully qualified path name
%~d1       - expands %1 to a drive letter only
%~p1       - expands %1 to a path only
%~n1       - expands %1 to a file name only
%~x1       - expands %1 to a file extension only
%~s1       - expanded path contains short names only
%~a1       - expands %1 to file attributes
%~t1        - expands %1 to date/time of file
%~z1        - expands %1 to size of file
%~$PATH:1   - searches the directories listed in the PATH environment variable and expands %1 to the fully
                         qualified name of the first one found.  If the environment variable name is not defined or the
                         file is not found by the search, then this modifier expands to the empty string    

%~dp1      - expands %1 to a drive letter and path only
%~nx1      - expands %1 to a file name and extension only
%~dp$PATH:1 - searches the directories listed in the PATH environment variable for %1 and expands to the
                          drive letter and path of the first one found.
%~ftza1     - expands %1 to a DIR like output line

留言

熱門文章