设为首页 收藏本站
查看: 842|回复: 0

[经验分享] Windows脚本 - %~dp0的含义

[复制链接]
累计签到:2 天
连续签到:1 天
发表于 2018-6-20 08:21:24 | 显示全部楼层 |阅读模式
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:
  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:\
  转载自:
  http://www.cnblogs.com/smwikipedia/archive/2009/03/30/1424749.html

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-526099-1-1.html 上篇帖子: windows下读取Linux分区软件 下篇帖子: windows下apache+多tomcat集群
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表