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 ...
随机推荐
- BZOJ3391: [Usaco2004 Dec]Tree Cutting网络破坏
3391: [Usaco2004 Dec]Tree Cutting网络破坏 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 47 Solved: 37[ ...
- v4l2采集视频和图片源码
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <assert.h&g ...
- CSS 代码是什么?(转)
转自:http://www.divcss5.com/rumen/r95.shtml CSS 代码是什么,什么是CSS代码? 目录 什么是CSS css代码样子(图) 作用 相关扩展阅读 一.了解什么是 ...
- Java中4种权限的理解
1. 包访问权限 (1)包的理解:将一组相关的.有意义的类文件组织在一起(即相应的.java文件放在一个文件夹下)就构成了包或者类库.(每个类文件的开头都包含一个所属包的声明“package pac ...
- JavaScript学习笔记(高级部分—01)
JavaScript的核心ECMAScript描述了该语言的语法和基本对象:DOM描述了处理网页内容的方法和接口:BOM描述了与浏览器进行交互的方法和接口. 简单说,ECMAScript描述了以下内容 ...
- 【C#基础】实现URL Unicode编码,编码、解码相关整理
1.Unicode编码 引用系统 System.Web using System.Web; string postdata = "SAMLRequest=" + HttpUtili ...
- int与Integer
int 是基本类型,直接存数值 integer是对象,用一个引用指向这个对象 1.Java 中的数据类型分为基本数据类型和复杂数据类型 int 是前者>>integer 是后者(也就是一个 ...
- xml的加密和解密
xml加密(XML Encryption)是w3c加密xml的标准.这个加密过程包括加密xml文档的元素及其子元素,通过加密,xml的初始内容将被替换,但其xml格式仍然被完好的保留. 介绍我们有3个 ...
- Oracle之事务
一,oracle的事务: 是指对数据操作的一系列动作的统称.即:事务的任务便是使数据库从一种状态变换成为另一种状态,这不同于文件系统,它是数据库所特用的. 事务有四大特性(ACID): 1,原子性(a ...
- Linux命令之必杀绝技Vi文本编辑的使用
vi 文本编辑器 语法:vi [参数] 文件 进入vi全屏幕编辑画面 按字母i进入[输入模式],按[ESC]转到命令行, 输入 :q可不保存退出vi :wq存盘退出vi :q!不存盘强制退出 :w ...