Batch file 批处理脚本命令的工作方式?

Batch file 批处理脚本命令的工作方式?,batch-file,Batch File,“%%~xi”在批处理脚本命令中是什么意思 for %%i in (%1) do set ext="%%~xi" 我知道它是一个for循环,使用1个命令行参数,“~”不是,而“I”是变量,而是“x”? 这个命令做什么?%%~xi提供文件扩展名。如果%%i是“somefile.txt”,则%%xi将从返回.txt,以获取/?: %~I - expands %I removing any surrounding quotes (") %~fI - ex

“%%~xi”
在批处理脚本命令中是什么意思

for %%i in (%1) do set ext="%%~xi"
我知道它是一个for循环,使用1个命令行参数,“~”不是,而“I”是变量,而是“x”?
这个命令做什么?

%%~xi
提供文件扩展名。如果
%%i
“somefile.txt”
,则
%%xi
将从
返回
.txt

,以获取/?

    %~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.

在批处理文件中,与cmd.exe窗口相反,您可以将
%
字符加倍。

参考:


批处理中的
~
表示%%i
变量的
,以及
cmd行%1
参数
修饰符
请参阅
帮助以获取
帮助呼叫
请参阅。。。
Parameter Extensions

    When an argument is used to supply a filename then the following extended syntax can be applied:

    we are using the variable %1 (but this works for any parameter)

    %~f1 Expand %1 to a Fully qualified path name - C:\utils\MyFile.txt

    %~d1 Expand %1 to a Drive letter only - C:

    %~p1 Expand %1 to a Path only e.g. \utils\ this includes a trailing \ which will be interpreted as an escape character by some commands.

    %~n1 Expand %1 to a file Name without file extension C:\utils\MyFile or if only a path is present (with no trailing backslash\) - the last folder in that path.

    %~x1 Expand %1 to a file eXtension only - .txt

    %~s1 Change the meaning of f, n, s and x to reference the Short 8.3 name (if it exists.)

    %~1   Expand %1 removing any surrounding quotes (")

    %~a1 Display the file attributes of %1

    %~t1 Display the date/time of %1

    %~z1 Display the file size of %1

    %~$PATH:1 Search the PATH environment variable and expand %1 to the fully qualified name of the first match found.

The modifiers above can be combined:

    %~dp1 Expand %1 to a drive letter and path only

    %~sp1 Expand %1 to a path shortened to 8.3 characters

    %~nx2 Expand %2 to a file name and extension only