【问题标题】:Making directories in windows batch script在 Windows 批处理脚本中创建目录
【发布时间】:2016-07-19 20:43:15
【问题描述】:

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

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

当我运行它时,它说一个子目录或文件已经存在。即使已经存在同名文件,如何让它创建一个目录?

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

【问题讨论】:

  • 不能在同一个地方有同名的文件和目录,这在Windows中根本不可能!假设有一个文件image01.jpg,相关目录应该叫image01(没有.jpg后缀)吗?
  • do MD %%~dpi 请参阅for /? 的帮助结尾。
  • @aschipfl,你是对的!我不想在目录名称上加上 .jpg 后缀。

标签: windows batch-file for-loop directory


【解决方案1】:

你可以试试这样的:

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

编辑于 20/07/2016 @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 是批处理文件的完整名称,就像您假设的那样)。

【讨论】:

  • 这完全符合我的需要!谢谢你。你能解释一下%%~ni 是做什么的吗?
  • @Idpelegr 检查我的编辑以了解%%~ni 是什么,如果您觉得它有帮助,请不要忘记接受这个答案并点赞!祝你好运!
猜你喜欢
  • 2011-07-26
  • 1970-01-01
  • 1970-01-01
  • 2013-04-11
  • 1970-01-01
  • 2013-07-27
  • 2010-10-06
  • 2014-04-14
相关资源
最近更新 更多