在Windows批处理脚本中创建目录

时间:2016-07-19 20:43:16

标签: windows batch-file for-loop directory

我在一个文件夹中有大约2000个jpg文件,我需要为每个文件夹创建一个具有相同名称的文件夹。 jpg不一定必须在同一个文件夹中。我是批处理脚本的新手,但它似乎是一个非常简单的操作。这就是我到目前为止所做的:

for %%i in (*) do md %%i

当我运行它时,它表示已存在子目录或文件。即使已经存在具有相同名称的文件,如何才能创建目录?

此外,它似乎只循环最后一百个文件。我如何让它运行〜2000?

1 个答案:

答案 0 :(得分:0)

你可以尝试这样的事情:

@echo off
set "ext=*.jpg"
for %%i in ("%ext%") do ( 
    if not exist "%%~ni" MD "%%~ni" 
)
pause

2016年7月20日编辑@ 20:25

调用

for /?
命令行中的

提供了有关此语法的帮助(也可以在FOR之外使用,这只是可以找到帮助的地方)。

  

另外,替换FOR   变量引用已得到增强。   您现在可以使用以下可选项   语法:

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

修饰符可以合并得到   复合结果:

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

在上面的例子中,%I和PATH可以   被其他有效值替换。   %〜语法由有效终止   FOR变量名。采摘大写   像%I这样的变量名称使它更多   可读并避免与...混淆   修饰符,不是这种情况   敏感。

您可以使用不同的字母,例如f表示“完整路径名称”,d表示驱动器号,p表示路径,可以组合使用。 %~是每个序列的开头,数字I表示它适用于参数%I(其中%0是批处理文件的完整名称,就像你假设)。

相关问题