简体   繁体   English

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

[英]Making directories in windows batch script

I have about 2000 jpg files in a folder, and I need to make a folder for each of them with the same name. 我在一个文件夹中有大约2000个jpg文件,我需要为每个文件夹创建一个具有相同名称的文件夹。 The jpg doesn't necessarily have to be in the same folder. jpg不一定必须在同一个文件夹中。 I'm new to batch script, but it seemed like a pretty simple operation. 我是批处理脚本的新手,但它似乎是一个非常简单的操作。 This is what I have so far: 这是我到目前为止:

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

When I run it, it says a subdirectory or file already exists. 当我运行它时,它表示已存在子目录或文件。 How do I get it to make a directory even though a file with the same name already exists? 即使已经存在具有相同名称的文件,如何才能创建目录?

Also, it appears to only loop for the last hundred files. 此外,它似乎只循环最后一百个文件。 How do I get it to run for all ~2000? 如何让它运行〜2000?

You can try something like that : 你可以试试这样的东西:

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

Edit on 20/07/2016 @20:25 编辑于2016年7月20日@20:25

Calling 调用

for /?

in the command-line gives help about this syntax (which can be used outside FOR, too, this is just the place where help can be found). 在命令行中提供有关此语法的帮助(也可以在FOR之外使用,这只是可以找到帮助的地方)。

In addition, substitution of FOR variable references has been enhanced. 此外,FOR变量引用的替换已得到增强。 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. 在上面的例子中,%I和PATH可以被其他有效值替换。 The %~ syntax is terminated by a valid FOR variable name. %〜语法由有效的FOR变量名终止。 Picking upper case variable names like %I makes it more readable and avoids confusion with the modifiers, which are not case sensitive. 选择大写变量名称(如%I)使其更具可读性并避免与修饰符混淆,修饰符不区分大小写。

There are different letters you can use like f for "full path name", d for drive letter, p for path, and they can be combined. 您可以使用不同的字母,例如f表示“完整路径名称”, d表示驱动器号, p表示路径,它们可以组合使用。 %~ is the beginning for each of those sequences and a number I denotes it works on the parameter %I (where %0 is the complete name of the batch file, just like you assumed). %~是每个序列的开头,数字I表示它适用于参数%I (其中%0是批处理文件的完整名称,就像您假设的那样)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM