Batch - %~dp0 modifiers

回到顶部(go to top)

%~dp0 简易解释

The variable %0 in a batch script is set to the name of the executing batch file. The ~dp special syntax between the % and the 0 basically says to expand the variable %0 to show the drive letter and path, which gives you the current directory containing the batch file!

%~dp0 只表示将要“运行的”bat命令的folder,不包含bat名称自己。注意,不是“运行处”的folder (该功能用%cd%实现)。详细的区别,查看Batch - %~dp0 vs %cd%

 

回到顶部(go to top)

%~dp0 详细解释

The %~dp0 (that’s a zero) variable when referenced within a Windows batch file will expand to the drive letter and path of that batch file.

The variables %0-%9 refer to the command line parameters of the batch file. %1-%9 refer to command line arguments after the batch file name. %0 refers to the batch file itself.

%0-%9代表的是batch文件的参数。%1-%9 是batch名称之后的命令行参数,%0代表batch文件自己。

If you follow the percent character (%) with a tilde character (~), you can insert a modifier(s) before the parameter number to alter the way the variable is expanded. The d modifier expands to the drive letter and the p modifier expands to the path of the parameter.

Example: Let’s say you have a directory on C: called bat_files, and in that directory is a file called example.bat. In this case, %~dp0 (combining the d and p modifiers) will expand to C:\bat_files\

 

回到顶部(go to top)

例子

a BAT file at the innermost level:

1
2
3
4
C:\
  dir\
    my files\
      run.bat

The BAT file contains the following lines:

1
2
3
@pushd %~dp0
@echo %~dp0
@popd

If I run the BAT file from a command prompt whose current directory is C:\dir\my files, then I get a very reasonable result:

1
2
C:\dir\my files>run.bat
C:\dir\my files\

  

回到顶部(go to top)

Modifiers 语法

In addition, substitution of FOR variable references has been enhanced. You can now use the following optional syntax:

目前,对FOR 命令做了增强,可以加入单个/多个modifiers, 对%0~%9的参数进行增强。

下面是单个modifiers语法,大写的I代表第几个参数:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
%~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 

modifiers也可以组合使用:

1
2
3
4
5
6
7
%~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.

 

There are different letters you can use like f for "full path name"d for drive letterp for path, and they can be combined. %~ is the beginning for each of those sequences and a number I denotes it works on the parameter %I (where %0 is the complete name of the batch file, just like you assumed).  

 

posted on   frank_cui  阅读(385)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

levels of contents
点击右上角即可分享
微信分享提示