Windows脚本 - %~dp0的含义
含义是:更改当前目录为批处理本身的目录,有些晕吧?不急,我举例
比如你有个批处理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:
echo this is %%cd%% %cd%
echo this is %%~dp0 %~dp0
Run it from C:\ and this is what you see
this is %cd% C:\
this is %~dp0 D:\
Windows脚本 - %~dp0的含义的更多相关文章
- VBScript: Windows脚本宿主介绍
Windows脚本宿主(Windows Script Host, WSH)是一个Windows管理工具.WSH创建了一个脚本运行的主环境,WSH使脚本能够使用对象和服务,并提供脚本执行的准则.WSH还 ...
- Windows脚本
最近项目测试比较麻烦,要启动好几个command窗口,手敲之类的有些繁琐,于是研究了一下windows脚本,实现以下功能: 懒人吗,参数意义顺序比较懒得记,所以需要友好参数提示: 根据参数数量决定启动 ...
- windows脚本(VBS)之cmd命令行的妙用
windows脚本(VBS)之cmd命令行的妙用 (2009-08-06 13:40:55) 转载▼ 标签: 脚本 cmd 命令行 vbs js 简单 公式 windows it 分类: 计算机 脚本 ...
- 修改host指定域名指向ip,Windows脚本与Linux脚本
修改host指定域名指向ip,Windows脚本与Linux脚本 一,Windows系统修改hosts文件 Windows系统下hosts文件位置:C:\Windows\System32\driver ...
- windows脚本-CMD和Batch
一.DOS,CMD和batch DOS是磁盘操作系统(英文:Disk Operating System)的缩写,是个人计算机上的一类操作系统.从1981年直到1995年的15年间,DOS在IBM PC ...
- windows脚本配置ip地址
背景:工作上经常涉及到要调试设备,每次都要手动配置静态ip地址,配置完之后还要重新改回来,有时候为了连续调试多台设备,来回手动更改ip,实在麻烦. 思考:想到windows有脚本,可以利用脚本文件达到 ...
- Windows 常用消息及含义
消息范围 说明 0 - WM_USER – 1 系统消息 WM_USER - 0x7FFF 自定义窗口类整数消息 WM_APP - 0xBFFF 应用程序自定义消息 0xC000 - 0xFFFF ...
- Windows脚本修改主机名-不重启
windows通过脚本方式修改主机名的方法有很多种,下面介绍修改注册表方式的脚本. 使用方法: 1 打开cmd,假如脚本名为ModifyHostname.bat 2 执行脚本,并加入脚本参数,其中第一 ...
- Windows脚本相关
1 获取IP地址 echo StartChangeIPFile echo 获取主机名 for /f %%i in ('hostname') do (set pcName=%%i) ::ping %pc ...
随机推荐
- (总结)工作中常用的js自定义函数——日期时间类
//设置时间类 var Wsdatatime = function(){ this.today = (new Date()).getTime(); //当前时间 } Wsdatatime.protot ...
- HDU_2014——计算平均分
Problem Description 青年歌手大奖赛中,评委会给参赛选手打分.选手得分规则为去掉一个最高分和一个最低分,然后计算平均得分,请编程输出某选手的得分. Input 输入数据有多组,每 ...
- zabbix linux被监控端部署
测试使用agentd监听获取数据. 服务端的安装可以查看http://blog.chinaunix.net/space.php?uid=25266990&do=blog&id=3380 ...
- hdu1166敌兵布阵
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- Java[1] Java学习书籍汇总(转)
原文:http://www.cnblogs.com/hyl8218/p/5067000.html 学习的最好途径就是看书“,这是我自己学习并且小有了一定的积累之后的第一体会.个人认为看书有两点好处: ...
- CSS常用操作-分类
- wpf下拉框不能多选的原因
<dxe:ComboBoxEdit Margin="0" Height="25" Width="65" VerticalAlignm ...
- 淘宝内部大量使用的开源系统监控工具--Tsar
Tsar是淘宝开发的一个非常好用的系统监控工具,在淘宝内部大量使用 它不仅可以监控CPU.IO.内存.TCP等系统状态,也可以监控Apache,Nginx/Tengine,Squid等服务器状态 ...
- Spring注解的使用和区别:@Component、@Service、@Repository、@Controller
Spring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释,它们分别是:@Repository.@Service 和 @Controller.在目前的 Spring ...
- Walle 瓦力 web部署系统
Walle 一个web部署系统工具,可能也是个持续发布工具,配置简单.功能完善.界面流畅.开箱即用! 安装步骤: 1. git clone 首先配置成功(去百度找答案) 打开git bash命令窗口执 ...