含义是:更改当前目录为批处理本身的目录,有些晕吧?不急,我举例 
比如你有个批处理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:\

Windows脚本 - %~dp0的含义的更多相关文章

  1. VBScript: Windows脚本宿主介绍

    Windows脚本宿主(Windows Script Host, WSH)是一个Windows管理工具.WSH创建了一个脚本运行的主环境,WSH使脚本能够使用对象和服务,并提供脚本执行的准则.WSH还 ...

  2. Windows脚本

    最近项目测试比较麻烦,要启动好几个command窗口,手敲之类的有些繁琐,于是研究了一下windows脚本,实现以下功能: 懒人吗,参数意义顺序比较懒得记,所以需要友好参数提示: 根据参数数量决定启动 ...

  3. windows脚本(VBS)之cmd命令行的妙用

    windows脚本(VBS)之cmd命令行的妙用 (2009-08-06 13:40:55) 转载▼ 标签: 脚本 cmd 命令行 vbs js 简单 公式 windows it 分类: 计算机 脚本 ...

  4. 修改host指定域名指向ip,Windows脚本与Linux脚本

    修改host指定域名指向ip,Windows脚本与Linux脚本 一,Windows系统修改hosts文件 Windows系统下hosts文件位置:C:\Windows\System32\driver ...

  5. windows脚本-CMD和Batch

    一.DOS,CMD和batch DOS是磁盘操作系统(英文:Disk Operating System)的缩写,是个人计算机上的一类操作系统.从1981年直到1995年的15年间,DOS在IBM PC ...

  6. windows脚本配置ip地址

    背景:工作上经常涉及到要调试设备,每次都要手动配置静态ip地址,配置完之后还要重新改回来,有时候为了连续调试多台设备,来回手动更改ip,实在麻烦. 思考:想到windows有脚本,可以利用脚本文件达到 ...

  7. Windows 常用消息及含义

      消息范围 说明 0 - WM_USER – 1 系统消息 WM_USER - 0x7FFF 自定义窗口类整数消息 WM_APP - 0xBFFF 应用程序自定义消息 0xC000 - 0xFFFF ...

  8. Windows脚本修改主机名-不重启

    windows通过脚本方式修改主机名的方法有很多种,下面介绍修改注册表方式的脚本. 使用方法: 1 打开cmd,假如脚本名为ModifyHostname.bat 2 执行脚本,并加入脚本参数,其中第一 ...

  9. Windows脚本相关

    1 获取IP地址 echo StartChangeIPFile echo 获取主机名 for /f %%i in ('hostname') do (set pcName=%%i) ::ping %pc ...

随机推荐

  1. java调优随记-堆和栈

    基础知识: 关于堆和栈,堆和栈是程序运行的关键,关于堆和栈的定义和解释可自行搜索,我比较认可以程序运行过程中他们扮演的角色作为对比的点:堆是存储的单位,而栈是程序运行时的单位.栈解决的是程序的运行问题 ...

  2. redis 异常排查

    异常排查 redis-server redis.windows.conf D:\redis-2.8.17>redis-server.exe redis.windows.conf[4692] 27 ...

  3. D - Network - poj3694(LCA求桥)

    题意:有一个网络有一些边相互连接,现在有Q次操作,求每次操作后的桥的个数 分析:开始竟然不知道还有LCA这么个东西....... *********************************** ...

  4. js继承的三种实现

    概念:在有些面向对象语言中,可以使用一个类(子类)继承另一个类(父类),子类可以拥有父类的属性和方法,这个功能可以在js中进行模拟. 三种方法: 第一种:扩展Object方法 Object.proto ...

  5. 用SHELL与列表处理了件尴尬事

    与列表语法 command-1 && command-2 && command-3 && command-4 && ...command ...

  6. Linux系统调用system_call

    2016-03-25 张超的<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 我的虚拟环境和代码在http ...

  7. nginx安装(正式)

    一.安装说明 系统环境:CentOS Linux release 7.2.1511 (Core) 系统内核:3.10.0-327.el7.x86_64软件:nginx-1.10.1.tar.gz其他所 ...

  8. Jqgrid的用法总结与分页功能的拓展

    这是本人写的第一个与技术相关的博客,但是非挑战技术的,而是对工作的总结,另外加一点点拓展. Jqgrid的功能十分强大,强大到可以做到与数据grid相关的任何功能,同时由于在用的过程中总是不能够一气呵 ...

  9. 随机获取oracle数据库中的任意一行数据(rownum)

    最近看oracle资料的时候,了解rownum的概念,以前只知道对数据库表进行简单的增删改查: 看到了rownum的概念后,突然想到了好多业务场景应该都可以适用的,比如在进行随机发奖的时候, 我们就可 ...

  10. 解决Linux下Oracle中文乱码的一些心得体会 ,转自

    以下转自 http://blog.itpub.net/29151695/viewspace-1173238/ 最近在linux上安装完oracle 10gR2后,又遇到了字符集乱码的问题,之前在网上找 ...