1

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

Eduard Sukharev
  • 1,188
  • 2
  • 15
  • 37
jackdaw
  • 15
  • 7

1 Answers1

2

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
Eduard Sukharev
  • 1,188
  • 2
  • 15
  • 37
  • Thank you! That worked perfectly on my test data. Before running it on a copy of my actual data I'll read through the very helpful links you included to do my best understand why your solution works – jackdaw Mar 06 '20 at 00:20