在Windows脚本中,%i类似于shell脚本中的$i,%0表示脚本本身,%1表示脚本的第一个参数,以此类推到%9,在%和i之间可以有“修饰符”(完整列表可通过"for /?"指令查看),表示对%i进行各种处理,其中d表示盘符,p表示除盘符外的路径,n表示文件名,x表示扩展名,f表示全路径(路径+文件名),s表示8.3格式的全路径,修饰符可以组合,所以%~dp0的意思是:脚本本身(%0)的路径(盘符d+路径p)。下面是实例演示,在F:ghost mpFiles下新建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:warezDevelopCcodeblocks-12.11mingw-setup.zip",运行结果如下:
%0 = aa.bat %1 = f:CMDTest.zip %2 = f:warezDevelopCcodeblocks-12.11mingw-setup.zip %~0 = aa.bat %~1 = f:CMDTest.zip %~2 = f:warezDevelopCcodeblocks-12.11mingw-setup.zip %~d0 = F: %~p0 = ghost mpFiles %~dp0 = F:ghost mpFiles %~n0 = aa %~x0 = .bat %~nx0 = aa.bat %~f0 = F:ghost mpFilesaa.bat %~s0 = F:ghost mpFilesaa.bat %~fs0 = F:ghost mpFilesaa.bat %~dp1 = f: %~nx1 = CMDTest.zip %~fs1 = f:CMDTest.zip %~dp2 = f:warezDevelopC %~nx2 = codeblocks-12.11mingw-setup.zip %~f2 = f:warezDevelopCcodeblocks-12.11mingw-setup.zip %~s2 = f:warezDevelopCCODEBL~1.ZIP %~fs2 = f:warezDevelopCCODEBL~1.ZIP