我正在學習批量編程,我無法區分%〜2和%2。還請提供一個示例。批處理編程中%〜2和%2之間的區別?
1
A
回答
2
%2
在第二個參數中替代。 %~2
替代第二論據與但刪除任何引號:
C:\Temp>type t.cmd
@echo off
echo %%2 is: %2
echo %%~2 is: %~2
C:\Temp>t.cmd first second third
%2 is: second
%~2 is: second
C:\Temp>t.cmd first "second third"
%2 is: "second third"
%~2 is: second third
在命令提示符下,鍵入help for
擴大變量(即使不是其主要工作在for
命令)時找到的選項:
In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:
%~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
The modifiers can be combined to get compound results:
%~dpI - expands %I to a drive letter and path only
%~nxI - expands %I to a file name and extension only
%~fsI - expands %I to a full path name with short names only
%~dp$PATH:I - searches the directories listed in the PATH
environment variable for %I and expands to the
drive letter and path of the first one found.
%~ftzaI - expands %I to a DIR like output line
In the above examples %I and PATH can be replaced by other valid
values. The %~ syntax is terminated by a valid FOR variable name.
Picking upper case variable names like %I makes it more readable and
avoids confusion with the modifiers, which are not case sensitive.
0
%2是傳遞給批處理文件的第二個參數。
myfile.bat firstArg secondArg
因爲參數通常是文件路徑,所以有一些額外的語法來提取部分路徑。
%〜2刪除第二個參數上的任何「」。
%~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
0
線輪,這是簡單的。 %2
是傳遞給您的bat文件的第二個參數。添加~
將刪除變量中的引號。
考慮以下簡稱BAT文件(test.bat的):
@ECHO OFF
ECHO %2
ECHO %~2
德恩你打電話給你的BAT文件與test.bat p1 p2
它會輸出:
P2
P2
現在試試test.bat p1 "p2"
!這一次輸出將是:
「P2」
P2
所以,如果你沒有任何引號的輸出將是相同的,如果你有一些%2
會保持他們和%~2
將刪除它們。
相關問題
- 1. 2個工作和2個任務之間的JavaEE批處理
- 2. 批處理和守護進程之間的區別
- 3. Spring批處理中的步驟,Tasklet和塊之間的區別
- 4. Spark中的RDD和批處理之間的區別?
- 5. Long.toBinaryString()和Long.toString(num,2)之間的區別?
- 6. [Object,Object]和Array之間的區別(2)
- 7. C#中myArray.GetValue(2)和myArray [2]之間的區別是什麼?
- 8. SWI-Prolog中duplicate_term/2和copy_term/2之間的區別
- 9. %變量%和!變量之間的區別!批處理文件
- 10. (eval'(+ 1 2))和(eval(+ 1 2))之間的區別?
- 11. (int *)arr [2]和int * arr [2]之間的區別是什麼?
- 12. Zend Framework 2和Symfony 2之間的主要區別
- 13. SQL - SPACE(2)+'|'之間的區別+空格(2)和'| 「?
- 14. 編碼和整理之間的區別?
- 15. QuickCheck 2批處理
- 16. Akka中的羣集和遠程處理之間的區別
- 17. $ a == 2和2 == $ a之間有什麼區別?
- 18. 並行計算中處理器和進程之間的區別?
- 19. MassTransit中處理程序和消費者之間的區別
- 20. 2 jquery綁定之間的區別
- 21. 2小時之間的區別jquery
- 22. 2個solr查詢之間的區別
- 23. 2個JUnit Assert類之間的區別
- 24. 使2個按鈕之間的區別
- 25. 2個數據表之間的區別
- 26. 2個exe文件之間的區別
- 27. MS Excel:2列之間的區別。
- 28. 2個SQL表之間的區別
- 29. matrics編程和matlab和八度圖像處理之間的區別
- 30. 角度2中的渲染器和ElementRef之間的區別
+1很好的答案,雖然你確實有點過分引用整個幫助對話,只有第一個點的點是相關的 – Monacraft
@Monacraft它不是整個幫助屏幕,只有大塊關於變量替代,我覺得其餘的給出了一些背景。 – Duncan
你應該像你一樣引用相關的部分。教育,而不是魔法,需要你。 – Trigger