Windows脚本 - %~dp0的含义

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

 

%~dp0 VS %cd%
    %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:/

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值