bat脚本中%~dp0含义解释

在Windows脚本中,%i类似于shell脚本中的$i,%0表示脚本本身,%1表示脚本的第一个参数,以此类推到%9,在%和i之间可以有“修饰符”(完整列表可通过"for /?"指令查看),表示对%i进行各种处理,其中d表示盘符,p表示除盘符外的路径,n表示文件名,x表示扩展名,f表示全路径(路径+文件名),s表示8.3格式的全路径,修饰符可以组合,所以%~dp0的意思是:脚本本身(%0)的路径(盘符d+路径p)。下面是实例演示,在F:\ghost\tmpFiles\下新建aa.bat文件,内容如下:

@echo off echo %%0 = %0 echo %%1 = %1 echo %%2 = %2 echo %%~0 = %~0 echo %%~1 = %~1 echo %%~2 = %~2 echo %%~d0 = %~d0 echo %%~p0 = %~p0 echo %%~dp0 = %~dp0 echo %%~n0 = %~n0 echo %%~x0 = %~x0 echo %%~nx0 = %~nx0 echo %%~f0 = %~f0 echo %%~s0 = %~s0 echo %%~fs0 = %~fs0 echo %%~dp1 = %~dp1 echo %%~nx1 = %~nx1 echo %%~fs1 = %~fs1 echo %%~dp2 = %~dp2 echo %%~nx2 = %~nx2 echo %%~f2 = %~f2 echo %%~s2 = %~s2 echo %%~fs2 = %~fs2

然后加两个附加参数运行这个文件: "aa.bat f:\CMDTest.zip f:\warez\Develop\C\codeblocks-12.11mingw-setup.zip",运行结果如下:

%0 = aa.bat %1 = f:\CMDTest.zip %2 = f:\warez\Develop\C\codeblocks-12.11mingw-setup.zip %~0 = aa.bat %~1 = f:\CMDTest.zip %~2 = f:\warez\Develop\C\codeblocks-12.11mingw-setup.zip %~d0 = F: %~p0 = \ghost\tmpFiles\ %~dp0 = F:\ghost\tmpFiles\ %~n0 = aa %~x0 = .bat %~nx0 = aa.bat %~f0 = F:\ghost\tmpFiles\aa.bat %~s0 = F:\ghost\tmpFiles\aa.bat %~fs0 = F:\ghost\tmpFiles\aa.bat %~dp1 = f:\ %~nx1 = CMDTest.zip %~fs1 = f:\CMDTest.zip %~dp2 = f:\warez\Develop\C\ %~nx2 = codeblocks-12.11mingw-setup.zip %~f2 = f:\warez\Develop\C\codeblocks-12.11mingw-setup.zip %~s2 = f:\warez\Develop\C\CODEBL~1.ZIP %~fs2 = f:\warez\Develop\C\CODEBL~1.ZIP

参考What does %~dp0 mean, and how does it work?

posted on 2014-03-17 18:16  leechau  阅读(2734)  评论(0编辑  收藏  举报

导航