%~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%

%~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\

例子

a BAT file at the innermost level:

C:\
dir\
my files\
run.bat

The BAT file contains the following lines:

@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:

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

  

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代表第几个参数:

%~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也可以组合使用:

%~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).  

Batch - %~dp0 modifiers的更多相关文章

  1. Batch - %~dp0 vs %cd%

    总结 %~dp0 只表示将要“运行的”bat命令的folder,不包含bat自己的名称. %cd%表示,“运行处”的folder . 示例脚本内容 cd-dp0.bat存放在f盘 @echo off ...

  2. Windows脚本 - %~dp0的含义

    含义是:更改当前目录为批处理本身的目录,有些晕吧?不急,我举例 比如你有个批处理a.bat在D:\qq文件夹下  a.bat内容为 cd /d %~dp0 在这里,cd /d %~dp0的意思就是cd ...

  3. [Batch檔案筆記] 在UNC路徑中執行Batch檔

    為了讓其他人可以免安裝又可以執行python程式所以我把python portable版本 winpython 放在samba的空間共享但是使用者如果要開 winpython cammand prom ...

  4. DOS批处理中%~dp0等扩充变量语法详解

    有时候我们看到别人使用%~dp0 ~是扩展的意思,相当于把一个相对路径转换绝对路径%0代指批处理文件自身%1表示批处理文件命令行接收到的第一个参数,%2表示第二个,以此类推%~d0 是指批处理所在的盘 ...

  5. Batch pk Shell - WindowsBatch与LinuxShell比较 [变量符号和关键字]

    原文地址:WindowsBatch与LinuxShell比较[变量符号和关键字] 一 简单实例1)batch file @echo off rem output helloworld ::  outp ...

  6. Batch批量替换hosts

    hosts文件替换 工作需要,要修改很多计算机的hosts文件,采用bat批量完成 解决的问题: 1.pc工作在非管理员权限,右键管理员权限太麻烦,因此采用执行中申请管理员权限的方式 2.hosts和 ...

  7. DOS批处理中%cd%与%~dp0的区别详解

    转载:https://www.jb51.net/article/105325.htm DOS批处理中%cd%与%~dp0的区别详解     Windows下批处理中%cd%和%~dp0都能用来表示当前 ...

  8. redis大幅性能提升之使用管道(PipeLine)和批量(Batch)操作

    前段时间在做用户画像的时候,遇到了这样的一个问题,记录某一个商品的用户购买群,刚好这种需求就可以用到Redis中的Set,key作为productID,value 就是具体的customerid集合, ...

  9. Spring Batch在大型企业中的最佳实践

    在大型企业中,由于业务复杂.数据量大.数据格式不同.数据交互格式繁杂,并非所有的操作都能通过交互界面进行处理.而有一些操作需要定期读取大批量的数据,然后进行一系列的后续处理.这样的过程就是" ...

随机推荐

  1. MHA-Atlas-MySQL高可用(上)

    MHA-Atlas-MySQL高可用(上) 链接:https://pan.baidu.com/s/17Av92KQnJ81Gc0EmxSO7gA 提取码:a8mq 复制这段内容后打开百度网盘手机App ...

  2. 说一下HTML5与传统HTML的区别

    1.首先说一下什么是HTML5 HTML5 将成为 HTML.XHTML 以及 HTML DOM 的新标准. HTML 的上一个版本诞生于 1999 年.自从那以后,Web 世界已经经历了巨变. HT ...

  3. hdu 3450 后缀数组

    题目大意: 求多个字符串的最长公共子串 基本思路: 参加我的博客hdu2774 代码如下: #include<cstdio> #include<cstring> using n ...

  4. 多线程的设计模式:Future、Master-Worker

    一 简介 并行设计模式属于设计优化的一部分,它是对一些常用的多线程结构的总结和抽象.与串行程序相比,并行程序的结构通常更为复杂,因此合理的使用并行模式在多线程开发中更具有意义,在这里主要介绍==Fut ...

  5. C/C++ volatile

    { volatile和const关键很相似,都是修饰变量的,只是二者功能不一样. volatile在多线程当中经常使用,因为在某一线程多次调用某一个变量,编译器会进行优化,将该变量存放在在寄存器当中, ...

  6. luoguP2148 [SDOI2009]E&D [sg函数][组合游戏]

    题目描述 小E 与小W 进行一项名为“E&D”游戏. 游戏的规则如下: 桌子上有2n 堆石子,编号为1..2n.其中,为了方便起见,我们将第2k-1 堆与第2k 堆 (1 ≤ k ≤ n)视为 ...

  7. 向量vector 容器浅析

    一.什么是vector? 向量(Vector)是一个封装了动态大小数组的顺序容器(Sequence Container).跟任意其它类型容器一样,它能够存放各种类型的对象.可以简单的认为,向量是一个能 ...

  8. python 中的内置高级函数

    1.map(function,iterable) map是把迭代对象依次进行函数运算,并返回. 例子: map返回的十分map对象,需要list()函数转化. 2.exec()函数 执行储存在字符串或 ...

  9. 一个小BUG引发的思考。(论开发与测试之间的那点事)

    标题不是“一个馒头引发的血案”. 言归正传:今天上午测试的时候,发现了一个BUG,如图: 一个用肉眼就能发现的BUG.原因当然是因为开发同事没有自测试,流入到了测试人员这里了. 无非是开发同事不严谨造 ...

  10. 搭建hadoop集群 单机版

    二.在Ubuntu下创建hadoop用户组和用户         这里考虑的是以后涉及到hadoop应用时,专门用该用户操作.用户组名和用户名都设为:hadoop.可以理解为该hadoop用户是属于一 ...