批处理脚本说明(循环)

时间:2015-05-31 18:13:43

标签: batch-file

我正在编写一个Batch脚本来查找目录中的文件夹,并且由于之前在stackoverflow上找到了一些问题而得到了它,但是,即使它正在按预期工作,我也没有什么问题。< / p>

for /d /r "%directory%" %%a in (*) do if /i "%%~nxa"=="%foldername%" set "folderpath=%%a"
echo "%folderpath%"

%% a (*) %% - nxa 的行是什么?

同样在此代码中(搜索目录中的文件)

for /r "%directory%" %%a in (*) do if "%%~nxa"=="%filename%" set p=%%~dpnxa

%% ~dpnxa 做什么?

就google而言,我无法找到任何解释或官方MS网站。

1 个答案:

答案 0 :(得分:5)

摘自for帮助:

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

%%a是for循环中使用的标记,%%~nxa(每次迭代将对应于已处理的文件)是文件的名称和扩展名。 *是一张外卡,表示每个符号。

通配符 - http://ss64.com/nt/syntax-wildcards.html

for循环 - http://ss64.com/nt/for.html