批处理脚本说明(循环)

jac*_*ack 1 batch-file

我正在编写一个批处理脚本来查找目录中的文件夹,并使其正常工作,这要归功于之前在stackoverflow上发现的问题,但是,即使它正常工作,我也没有什么问题.

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

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

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

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

什么是%%〜dpnxa吗?

据谷歌说,我无法找到任何解释,或官方的MS网站.

npo*_*aka 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
Run Code Online (Sandbox Code Playgroud)

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

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

for loop - http://ss64.com/nt/for.html