首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >批量编程中%~2和%2的差异?

批量编程中%~2和%2的差异?
EN

Stack Overflow用户
提问于 2015-04-23 11:51:58
回答 3查看 6.9K关注 0票数 1

我正在学习批处理编程,我无法从字面上区分%~2和%2 .Also,请举一个例子。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-04-23 11:57:32

%2在第二个参数中替换。%~2替换了第二个参数,但删除了任何引号:

代码语言:javascript
复制
C:\Temp>type t.cmd
@echo off
echo %%2 is: %2
echo %%~2 is: %~2

C:\Temp>t.cmd first second third
%2 is: second
%~2 is: second

C:\Temp>t.cmd first "second third"
%2 is: "second third"
%~2 is: second third

在命令提示符下,键入help for以在展开变量(即使在for命令中不起作用)时查找选项:

代码语言:javascript
复制
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.
票数 3
EN

Stack Overflow用户

发布于 2015-04-23 12:03:26

嗯,这很简单。%2是传递给bat文件的第二个参数。添加~将从变量中删除引号。

考虑以下简短的bat文件(test.bat):

代码语言:javascript
复制
@ECHO OFF
ECHO %2
ECHO %~2

使用test.bat p1 p2调用bat文件,它将输出:

p2 p2

现在试试test.bat p1 "p2"!这一次的输出是:

"p2“ p2

因此,如果您没有任何引号,输出将是相同的,如果您有一些,%2将保留它们,%~2将删除它们。

票数 2
EN

Stack Overflow用户

发布于 2015-04-23 11:57:57

%2是传递给批处理文件的第二个参数。

代码语言:javascript
复制
myfile.bat firstArg secondArg

因为参数通常是文件路径,所以有一些额外的语法来提取路径的部分。

%~2移除第二个参数上的任何"“。

代码语言:javascript
复制
%~1         - expands %1 removing any surrounding quotes (")
%~f1        - expands %1 to a fully qualified path name
%~d1        - expands %1 to a drive letter only
%~p1        - expands %1 to a path only
%~n1        - expands %1 to a file name only
%~x1        - expands %1 to a file extension only
%~s1        - expanded path contains short names only
%~a1        - expands %1 to file attributes
%~t1        - expands %1 to date/time of file
%~z1        - expands %1 to size of file
%~$PATH:1   - searches the directories listed in the PATH
               environment variable and expands %1 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    

%~dp1       - expands %1 to a drive letter and path only
%~nx1       - expands %1 to a file name and extension only
%~dp$PATH:1 - searches the directories listed in the PATH
               environment variable for %1 and expands to the
               drive letter and path of the first one found.
%~ftza1     - expands %1 to a DIR like output line

可能重复:What does %~d0 mean in a Windows batch file?

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29822635

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档