%%~dpa 的含义? [英] Meaning of %%~dpa?

查看:74
本文介绍了%%~dpa 的含义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被要求维护一些批处理文件,我在每个批处理文件的开头反复看到这一行..

FOR/f "usebackq tokens=*" %%a IN ('%0') DO SET this_cmds_dir=%%~dpaCD/d "%this_cmds_dir%"

有谁知道第一行是做什么的?%%~dpa 是什么?%0 是什么?什么是usebackq?

解决方案

%~dpa 为您提供%a (使用双 %,因为您在脚本中运行,当然).在 cmd.exefor/? 帮助的底部:

此外,FOR 变量引用的替换已得到增强.您现在可以使用以下可选语法:%~I - 扩展 %I 删除任何周围的引号 (")%~fI - 将 %I 扩展为完全限定的路径名%~dI - 仅将 %I 扩展为驱动器号%~pI - 仅将 %I 扩展为路径%~nI - 仅将 %I 扩展为文件名%~xI - 仅将 %I 扩展为文件扩展名%~sI - 扩展路径只包含短名称%~aI - 将 %I 扩展为文件的文件属性%~tI - 将 %I 扩展到文件的日期/时间%~zI - 将 %I 扩展到文件大小%~$PATH:I - 搜索 PATH 中列出的目录环境变量并将 %I 扩展为找到的第一个的完全限定名称.如果环境变量名称不是已定义或未找到该文件搜索,然后此修饰符扩展为空字符串可以组合修饰符以获得复合结果:%~dpI - 仅将 %I 扩展为驱动器号和路径%~nxI - 仅将 %I 扩展为文件名和扩展名%~fsI - 将 %I 扩展为仅包含短名称的完整路径名%~dp$PATH:I - 搜索 PATH 中列出的目录%I 的环境变量并扩展为找到的第一个驱动器号和路径.%~ftzaI - 将 %I 扩展为类似 DIR 的输出行在上面的例子中 %I 和 PATH 可以替换为其他有效的值.%~ 语法以有效的 FOR 变量名结束.选择像 %I 这样的大写变量名使其更具可读性和避免与不区分大小写的修饰符混淆.

%0 是运行的批处理文件的名称,usebackqtokens=for 命令.tokens= 可用于将单词分配给特定变量,在这种情况下,使用 * 将整个批次放入 %%a.

usebackq 更改了如何处理 %0 参数周围的引号.没有它,单引号将运行一个命令并使用该命令的输出而不是%0的值.

通过运行上述for/?可以找到更多细节.

顺便说一下,那个 PATH 技巧是一个 在您的路径上查找可执行文件的绝妙方法.

I am given to maintain some batch files and i repeatedly see this line in the beginning of every batch file..

FOR /f "usebackq tokens=*" %%a IN ('%0') DO SET this_cmds_dir=%%~dpa
CD /d "%this_cmds_dir%"

Does anyone know what the first line does? What is %%~dpa? What is %0 ? What is usebackq?

解决方案

%~dpa gives you the drive and the path of the file pointed to by %a (using double % since you're running within a script, of course). From the bottom of the for /? help in cmd.exe:

In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:

%~I         - expands %I removing any surrounding quotes (")
%~fI        - expands %I to a fully qualified path name
%~dI        - expands %I to a drive letter only
%~pI        - expands %I to a path only
%~nI        - expands %I to a file name only
%~xI        - expands %I to a file extension only
%~sI        - expanded path contains short names only
%~aI        - expands %I to file attributes of file
%~tI        - expands %I to date/time of file
%~zI        - expands %I to size of file
%~$PATH:I   - searches the directories listed in the PATH
              environment variable and expands %I to the
              fully qualified name of the first one found.
              If the environment variable name is not
              defined or the file is not found by the
              search, then this modifier expands to the
              empty string

The modifiers can be combined to get compound results:

%~dpI       - expands %I to a drive letter and path only
%~nxI       - expands %I to a file name and extension only
%~fsI       - expands %I to a full path name with short names only
%~dp$PATH:I - searches the directories listed in the PATH
              environment variable for %I and expands to the
              drive letter and path of the first one found.
%~ftzaI     - expands %I to a DIR like output line

In the above examples %I and PATH can be replaced by other valid
values.  The %~ syntax is terminated by a valid FOR variable name.
Picking upper case variable names like %I makes it more readable and
avoids confusion with the modifiers, which are not case sensitive.

%0 is the name of the batch file running and usebackq and tokens= are options for the for command. tokens= can be used to assign words to specific variables and in this case, using * puts the whole lot into %%a.

usebackq changes how the quotes are treated around the %0 argument. Without it, single quotes would run a command and use the output of that command rather than the value of %0.

More details can be found by running the aforementioned for /?.

That PATH trick, by the way, is a nifty way to find executables on your path.

这篇关于%%~dpa 的含义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆