问题
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
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
However, it created the pre directory directly under c:\temp
回答1:
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
来源:https://stackoverflow.com/questions/60540370/create-subdirectory-under-each-directory-containing-a-file