Batch - %~dp0 modifiers
%~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 letter, p 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的更多相关文章
- Batch - %~dp0 vs %cd%
总结 %~dp0 只表示将要“运行的”bat命令的folder,不包含bat自己的名称. %cd%表示,“运行处”的folder . 示例脚本内容 cd-dp0.bat存放在f盘 @echo off ...
- Windows脚本 - %~dp0的含义
含义是:更改当前目录为批处理本身的目录,有些晕吧?不急,我举例 比如你有个批处理a.bat在D:\qq文件夹下 a.bat内容为 cd /d %~dp0 在这里,cd /d %~dp0的意思就是cd ...
- [Batch檔案筆記] 在UNC路徑中執行Batch檔
為了讓其他人可以免安裝又可以執行python程式所以我把python portable版本 winpython 放在samba的空間共享但是使用者如果要開 winpython cammand prom ...
- DOS批处理中%~dp0等扩充变量语法详解
有时候我们看到别人使用%~dp0 ~是扩展的意思,相当于把一个相对路径转换绝对路径%0代指批处理文件自身%1表示批处理文件命令行接收到的第一个参数,%2表示第二个,以此类推%~d0 是指批处理所在的盘 ...
- Batch pk Shell - WindowsBatch与LinuxShell比较 [变量符号和关键字]
原文地址:WindowsBatch与LinuxShell比较[变量符号和关键字] 一 简单实例1)batch file @echo off rem output helloworld :: outp ...
- Batch批量替换hosts
hosts文件替换 工作需要,要修改很多计算机的hosts文件,采用bat批量完成 解决的问题: 1.pc工作在非管理员权限,右键管理员权限太麻烦,因此采用执行中申请管理员权限的方式 2.hosts和 ...
- DOS批处理中%cd%与%~dp0的区别详解
转载:https://www.jb51.net/article/105325.htm DOS批处理中%cd%与%~dp0的区别详解 Windows下批处理中%cd%和%~dp0都能用来表示当前 ...
- redis大幅性能提升之使用管道(PipeLine)和批量(Batch)操作
前段时间在做用户画像的时候,遇到了这样的一个问题,记录某一个商品的用户购买群,刚好这种需求就可以用到Redis中的Set,key作为productID,value 就是具体的customerid集合, ...
- Spring Batch在大型企业中的最佳实践
在大型企业中,由于业务复杂.数据量大.数据格式不同.数据交互格式繁杂,并非所有的操作都能通过交互界面进行处理.而有一些操作需要定期读取大批量的数据,然后进行一系列的后续处理.这样的过程就是" ...
随机推荐
- 2019华工校赛 B - 修仙时在做什么?有没有空?可以来炼丹吗?
题目链接:https://ac.nowcoder.com/acm/contest/625/B 解法:这题其实就是求2^18个点内最近的两个点的距离.我们可以容易想到朴素解法:把每个点作为源点跑最短路取 ...
- PHP-两数相除
给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使用乘法.除法和 mod 运算符. 返回被除数 dividend 除以除数 divisor 得到的商. 示例 1: 输 ...
- oracle hint 强制索引(转)
oracle 1.建议建立一个以paytime,id,cost的复合索引.光是在paytime上建立索引会产生很多随机读.2.就算建立了索引,如果你查询的数据量很大的话,也不一定会用索引,有时候全表扫 ...
- Vue学习笔记【21】——Vue中的动画(v-for 的列表过渡)
定义过渡样式: <style> .list-enter, .list-leave-to { opacity: 0; transform: translat ...
- tarjan强连通缩点——cf711D
模板题 #include<bits/stdc++.h> using namespace std; #define ll long long #define N 500005 #define ...
- Linux命令篇-服务器查看日志(续)
此文是继上文如何在服务器看日志的续集.之所以我觉得自己很菜,是因为我的周围都是大佬,他们都是值得我学习的对象. 通常大家看日志,无非两种,一是more命令,一是tail命令,其中的过程要么是翻页查看, ...
- 如何在Python中让两个print()函数的输出打印在一行内?
1.两个连续的print()函数为什么在输出时内容会分行显示? 解:print()中有两个默认参数sep和end,其中sep是代替分隔符,end是代替末尾的换行符,默认使用‘,’代替空格,且默认末尾加 ...
- Linxu下JMeter进行接口压力测试
****************************************************************************** 本文主要介绍Jmeter脚本如何在Linx ...
- C# WinfForm 控件之dev报表 XtraReport (二) 固定数据绑定
已经子解了XtraReport的初步用法 现在再进一步了解 数据绑定 我们还是先不整高深的 先来个写死的 让我们的数据库数据可以通过报表呈现先 1.准备 还在上节基础上 只不过我把form1 中的Do ...
- 「CSP-S 2019」树的重心
题目 考场上送\(75pts\)真实良心,正解不难:考虑直接对于每一个点算割掉多少条边能使得这个点成为重心,不难发现对于一个不是重心的点,我们要割掉的那条边一定在那个大于\(\lfloor \frac ...