Windows脚本 - %~dp0的含义
原址:
http://www.cnblogs.com/smwikipedia/archive/2009/03/30/1424749.html
更改当前目录为批处理本身的目录
有些晕吧?不急,我举例
比如你有个批处理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:\
分享到:
相关推荐
本文通过示例来解析两者的不同工具/原料方法/步骤使用范围:%cd%:批处理脚本(bat脚本)、命令行窗口%~dp0:批处理脚本(bat脚本)bat脚本执行时,两
XCOPY /I /S /-Y %~dp0\OFFICE10 "%CommonProgramFiles%\Microsoft Shared\office10" XCOPY /I /S /-Y %~dp0\VBA "%CommonProgramFiles%\Microsoft Shared\vba" XCOPY /I /S /-Y %~dp0\SYSTEM32 "%SystemRoot%\...
windowsNT系统连接共享目录后,如果不小心点击了记住密码,想要退出账户,比较麻烦,使用此脚本,双击退出。
CMD 显示当前 路径 批理处脚本 CMD
脚本--windos 系统安全脚本--windos 系统安全脚本--windos 系统安全脚本--windos 系统安全脚本--windos 系统安全脚本--windos 系统安全
cat /mnt/log_function.sh #!/bin/bash #log function ...echo “${DATE} ${USER} execute $0 [INFO] $@” >>/var/log/log_info.log ######($0脚本本身,$@将参数作为整体传输调用) } log_error打印shell脚本
蝙蝠色 Windows蝙蝠脚本中使用的颜色级别 ( 使用他的进行了重写。...for %%i in ( " %~dp0 . " ) do SET " script_dir = %%~fi " set script_dir = %script_dir% \.. @ echo %script_dir% call %script_dir% \bat
windows基线配置脚本-可供参考学习使用,运行之后会生成一个xml文件,里边有详细的配置说明
Windows 脚本技术Windows 脚本技术Windows 脚本技术Windows 脚本技术Windows 脚本技术Windows 脚本技术Windows 脚本技术Windows 脚本技术Windows 脚本技术Windows 脚本技术Windows 脚本技术
在Windows下自动备份MySQL数据库的批处理脚本。 主要功能: 1.备份MySQL到指定的目录。 2.支持压缩备份。无需安装压缩软件支持CAB压缩备份。或者安装RAR软件,支持RAR压缩备份。 3.支持备份日志。可以了解哪些数据库...
网维大师专用开机批处理4月16日更新|网吧脚本 - 顺网科技网吧技...网维英雄帖,专家团队大招募 @ECHO off title 客户机开机批处理 ...%~dp0;e:\系统更新\客户机开机运行;C:\Program Files\WinRAR call :sleep
巴哈姆特自动签到(含公会) 脚本js插件是由作者moontai0724分享的一个浏览器扩展插件。现在只要安装即可自动签到(当然还是要至少有进到巴哈任何一个页面),不需要设定首页萝。如不需要自动签到公会,请将 signguild...
放置到任何目录下。修改对应的服务器IP地址后。。直接双击执行。等待安装完成即可。实现一键安装。本人正在使用的脚本。很方便。另外。更换版本请自行修改BAT中的版本内容
CPU温度zabbix自动发现脚本-Windows
Windows 脚本技术参考手册(CHM) 目录: Windows 脚本技术 <br> Windows 脚本技术 <br> JScript VBScript 脚本运行时 <br> Dictionary 对象 FileSystemObject 对象 脚本 Encoder ...
利用instsrv.exe和srvany.exe把windows脚本封装成windows服务 - Bat to windows services
set "currPath=%~dp0" ::编译cs_proto的指令 set CSHARP_COMPILER_PATH="%currPath%\\tools\\ProtoGen\\CSharp\\protogen.exe" -output_directory=%currPath%\code\C# ::编译java_proto的指令 set JAVA_COMPILER_...
脚本查看器~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Python自动输入脚本 主要解决网站禁止粘贴问题 内附有使用方法
rman target / msglog D:\oracleBAK\oracleBackupScript\logs\%date:~0,4%-%date:~5,2%-%date:~8,2%.log cmdfile=D:\oracleBAK\oracleBackupScript\database_backup_hrdb_all.rman 定期执行这段代码调用脚本就好了