[http://www.cnblogs.com/smwikipedia/archive/2009/03/30/1424749.html]

[http://www.911cd.net/forums/lofiversion/index.php/t3730.html]

更改当前目录为批处理本身的目录 
有些晕吧?不急,我举例 
比如你有个批处理a.bat在D:\qq文件夹下  
a.bat内容为 
cd /d %~dp0 
在这里 
cd /d %~dp0的意思就是cd /d d:\qq 
%0代表批处理本身 d:\qq\a.bat 
~dp是变量扩充 
d既是扩充到分区号 d: 
p就是扩充到路径 \qq 
dp就是扩充到分区号路径 d:\qq

扩充变量语法详解:

:: 选项语法: 
:: ~I - 删除任何引号("),扩充 %I 
:: %~fI - 将 %I 扩充到一个完全合格的路径名 
:: %~dI - 仅将 %I 扩充到一个驱动器号 
:: %~pI - 仅将 %I 扩充到一个路径 
:: %~nI - 仅将 %I 扩充到一个文件名 
:: %~xI - 仅将 %I 扩充到一个文件扩展名 
:: %~sI - 扩充的路径只含有短名 
:: %~aI - 将 %I 扩充到文件的文件属性 
:: %~tI - 将 %I 扩充到文件的日期/时间 
:: %~zI - 将 %I 扩充到文件的大小 
:: %~$PATH:I - 查找列在路径环境变量的目录,并将 %I 扩充 
:: 到找到的第一个完全合格的名称。如果环境变量名 
:: 未被定义,或者没有找到文件,此组合键会扩充到 
:: 空字符串 
:: 可以组合修饰符来得到多重结果: 
:: %~dpI - 仅将 %I 扩充到一个驱动器号和路径 
:: %~nxI - 仅将 %I 扩充到一个文件名和扩展名 
:: %~fsI - 仅将 %I 扩充到一个带有短名的完整路径名 
:: %~dp$PATH:i - 查找列在路径环境变量的目录,并将 %I 扩充 
:: 到找到的第一个驱动器号和路径。 
:: %~ftzaI - 将 %I 扩充到类似输出线路的 DIR

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

http://www.911cd.net/forums/lofiversion/index.php/t3730.html

Look at this:
Manipulating variables in CMD shell
I'm not sure why this type of information isn't more prominent in the help files, but there you go. NT's command shell can manipulate variables but the operations you can perform are fairly limited. Still, better than nothing. :-) If you want to do some really clever stuff then you're going to have to look elsewhere. Vbscript can be useful as it's got lots of string handling capabilities. There's also Perl and AWK which are Windows (windoze?) ports of some very powerful Unix commands.

%1 is your command line option.

Namely: mycommand.cmd myoption1.

%0 determins where the batch file is running from. I've created a demo batch file in my winnt system32 folder called x.cmd. Running this gives the results shown below

%~f1 expands %1 to the drive, path and file name. If you pass %1 from the current directory then this expands that variable to it's full path

echo f0 = %~f0 produces f0 = c:\WINNT\system32\x.cmd 
%~d1 gets the drive letter from %1
echo d0 = %~d0 produces d0 = D 
%~p1 extracts the path from variable %1
echo p0 = %~p0 produces \WINNT\system32\ 
%~dp1 pulls the drive letter and path
echo dp0 = %~dp0 produces C:\WINNT\system32\ 
%~sp1 creates a short path (but no drive letter)

echo sp0 = %~sp0 produces \WINNT\system32

If I set %1 to "c:\Program Files\Internet Explorer" then %~sp1 produces \PROGRA~1\INTER. Note you have to wrap the long path in quotes otherwise the truncation doesn't work. 
%~x1 leaves only the command extension
echo x0 = %~x0 produces .cmd 
%~nx1 extracts the file name and extension
echo nx0 = %~nx0 produces x.cmd 
%~sx1 extracts the short extension from %0
echo sx0 = %~sx0 produces .cmd but a longer extension (.document?) would be cut down to .doc

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

%~dp0 VS %cd%

http://www.computerhope.com/forum/index.php?topic=54333.0

%cd% is available either to a batch file or at the command prompt and expands to the drive letter and path of the current directory (which can change e.g. by using the CD command)

%~dp0 is only available within a batch file and expands to the drive letter and path in which that batch file is located (which cannot change). It is obtained from %0 which is the batch file's name.

An experiment like the following shows the difference

Here is D:\dirshow.bat:

Code:
@echo off
echo this is %%cd%%  %cd%
echo this is %%~dp0 %~dp0

Run it from C:\ and this is what you see

Code:
C:\>D:\dirshow.bat
this is %cd%  C:\
this is %~dp0 D:\

转载于:https://www.cnblogs.com/taoxu0903/archive/2010/03/02/1676690.html

[Tip: bat] About %~dp0相关推荐

  1. Shell常用命令 和 bat常用命令

    linux和windows下的反斜杠\和正斜杠/区别 Windows:--"/"是表示参数,"\"是表示本地路径. xcopy /E /Y D:\DailyBu ...

  2. (九)Alian 的 Spring Cloud 公共 API 的(API核心starter)

    目录 一.背景 二.maven依赖 三.主要 3.1 自动配置类 3.2 属性配置类 3.3 swagger整合 四.优雅停服 4.1 优雅停服线程类 4.2 优雅停服调用 五.公共类 5.1.Map ...

  3. Win10家庭版安装docker(WSL与WSL2两种安装方式)

    Win10家庭版安装docker 背景 WSL方式安装(与WSL2可选其一) 系统要求 伪装成win10专业版 开启Containers 开启Hyper-V WSL2方式安装(与WSL可选其一) 系统 ...

  4. 潇洒郎: windows、centos学习Influxdb下载安装 重置用户、密码 influx语法 数据写入与读取、查询 修改默认启动端口

    下载: Install InfluxDB | InfluxDB OSS 2.1 Documentation 新建文件influxd.bat cd %~dp0 influxd.exe 或 cd %~dp ...

  5. NMAKE简要教程1:环境配置

    1. NMAKE初步了解 NMAKE和GNUMake大同小异,但是不能直接划等号,两者Makefile并不通用. NMAKE是Visual Studio的内置工具之一,只负责执行Makefile中描述 ...

  6. matebookego win11 arm 系统安装安卓子系统

    一般的x86电脑如果安装android子系统,运行安卓子系统,由于要将android arm代码转译为x86代码,所以效率不一定高,但是如果电脑是arm架构的,通过安卓子系统运行android的程序执 ...

  7. windows11\window10 开机自启动热点

    1.准备三个文件 文件1名:AutoStartWifiHotSport.ps1 Add-Type -AssemblyName System.Runtime.WindowsRuntime $asTask ...

  8. U8打包把dx替换为d8

    本文所需文件下载地址:https://download.csdn.net/download/wei1583812/21059961 建议看此文之前先把我上传的资源tool.zip下载下来看看目录结构, ...

  9. go leaf 从入坑到起飞

    之所以使用go leaf是因为其轻便,开发效率高不高,都是看个人的,好不好用,也是看个人的,咱们不予以置评,开始干活. 关于go leaf的下载 GitHub - name5566/leaf: A g ...

最新文章

  1. Struts2——学习(5):页面跳转
  2. go语言学习(3)面向对象,结构体和方法
  3. avalon $computed不起作用?
  4. java填充二维数组_Java用Arrays.fill()初始化二维数组的实现
  5. 从程序媛到微软全球 AKS 女掌门人,技术女神驾到!
  6. mongodb 字段出现次数_MongoDB数据库
  7. ubuntu14上安装ros教程
  8. lopatkin俄大神精简中文系统Windows 10 Enterprise 2016 LTSB 14393.479 x86-x64 ZH-CN PIP
  9. 计算机共享地址怎么设置到桌面,局域网共享在哪里设置
  10. reactjs安装并在脚手架中使用 material-ui/core
  11. android中实现一键加QQ群功能
  12. 选择困难症? 看看这几款主流的文档管理系统
  13. Python学习笔记之os模块
  14. 计算机专业保研面试备考:线性代数
  15. C++算法——字幕校对问题
  16. \t转义字符占几个字节?
  17. 害怕字体侵权?可以免费商用的字体库来了!
  18. 充分利用计算机研究GIS,GIS在水文水资源领域中的应用研究
  19. 汉堡式折叠html,3种超酷汉堡包菜单按钮变形动画特效
  20. PyTorch - GAN与WGAN及其实战

热门文章

  1. 把数据输出到Word (非插件形式)
  2. C# WinForm窗口最小化到系统托盘
  3. 作业调度框架 Quartz.NET 2.0 StepByStep
  4. hdu 2669 Romantic
  5. 诗与远方:无题(六十三)- 杂诗,然,矣
  6. vueCli3中使用代理,点击页面的刷新按钮时报错
  7. Java 面向对象:static的理解
  8. Linux:rsync error: remote command not found (code 127) at io.c(226) [sender=3.1.2
  9. linux挂载华为存储fc,linux 挂载存储设备
  10. 1909升级卡64_升级win10 1909后开机数分钟后出现卡死如何解决