在包含文件的每个目录下创建子目录 [英] Create subdirectory under each directory containing a file
问题描述
我在100多个目录中包含大量照片。这些都是安装前的照片。我想保留现有的目录结构,但在包含.jpg照片的任何目录中添加\pre和\post目录。如果给定目录仅包含其他目录但不包含文件,我不希望创建子目录
I have a large number of photos that are contained in over 100 directories. These are all pre-installation photos. I would like to keep the existing directory structure but add a \pre and \post directory in any directory that contains a .jpg photo. I dont want a subdirectory created if a given directory only contains other directories but not files
为了进行测试,我在c:\temp\one中有一个.jpg \二三。我在c:\temp中运行了此命令:
For testing , I have a single .jpg in c:\temp\one\two\three. I ran this command in c:\temp:
FOR /R c:\temp %G IN (*.jpg) DO mkdir pre
但是,它直接在c:\temp
However, it created the pre directory directly under c:\temp
推荐答案
因此,问题在于您尝试在当前目录中 mkdir
。对于您找到的每个JPG文件,您要获取目录名称,切换到该名称(使用 pushd
),创建 pre
目录 ,然后切换回开始的位置( popd
)。
So, the problem is that you try to mkdir
in a current dir. For each JPG file you find you want to get a directory name, switch to it (using pushd
), create a pre
directory if it does not exist yet, and then switch back to where you started (with popd
).
FOR /R c:\temp %G IN (*.JPG) DO pushd %~dpG && if not exist pre mkdir pre && popd
这篇关于在包含文件的每个目录下创建子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!