Windows脚本 - %~dp0的含义

更改当前目录为批处理本身的目录 
有些晕吧?不急,我举例 
比如你有个批处理a.batD:\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

这段代码是一个批处理脚本,用于将用户输入的條碼内容替换到指定的文本文件模板中,并将生成的结果打印到LPT1端口(打印机端口)。 下面是对每一行代码的解释: - `setlocal enabledelayedexpansion`:开启延迟环境变量扩展,用于在 FOR 循环中使用另一个 FOR 循环变量。 - `set "file=%~dp0\1.txt"`:定义变量 `file`,指定要替换的文本文件,`%~dp0` 表示取当前批处理脚本文件的绝对路径。 - `echo.`:输出一个空行。 - `set "replaced=1234"`:定义变量 `replaced`,表示要被替换的字符串。 - `echo QRCode>"%~dp0\2.txt"`:在一个新的文本文件中写入 "QRCode"。 - `echo.`:输出一个空行。 - `set /p "all=請輸入條碼: "`:提示用户输入條碼,将输入的值保存到变量 `all` 中。 - `for /f "usebackq delims=" %%i in ("%file%") do ( ... )`:循环读取文本文件的每一行,并对每一行执行一系列操作。 - `set "str=%%i"`:定义变量 `str`,将当前行的内容保存到其中。 - `set "str=!str:%replaced%={{QRCode:!all!}}!"`:将 `str` 中的 `replaced` 字符串替换成 `{{QRCode:!all!}}`,其中 `!all!` 表示用户输入的條碼内容。 - `echo !str!>>"%~dp0\2.txt"`:将替换后的 `str` 内容写入到新的文本文件中。 - `COPY "%~dp0\2.txt" LPT1`:将新的文本文件复制到打印机端口(LPT1)。 - `goto :1`:跳转到标签 `:1`,结束脚本。 因此,当用户运行这个批处理脚本时,它会提示用户输入條碼内容,然后将條碼内容替换到文本文件模板中,并将生成的结果打印到打印机端口。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值