I use a image processing tool which can batch output different sizes of the same image into the directory form:

<specific directory>\<image size name>
e.g. SuperBowl\Fullsize JPEG

I would like to routinely invert this and move any files within the subfolder to the new location, i.e.:

<image size name>\<specific directory>
e.g. Fullsize JPEG\SuperBowl

I would envisage running the operation against the Superbowl folder. Is there a simple tool available that can do this, or a Windows .bat or Linux bash script?


I think something like this should work:

for /d %x in (*) do for /d %y in ("%x\*") do md "%~ny"
for /d %x in (*) do for /d %y in ("%x\*") do md "%~ny\%x"
for /d %x in (*) do for /d %y in ("%x\*") do move "%x\%~ny\*" "%~ny\%x"

The first two lines create the inverted directory set, the third moves everything. If you put this in a batch file, you need to double the % characters.

Pretty sure you don't need the first line as MKDIR seems to create intermediate directories automatically, but I don't since how long.

  • On linux, mkdir -p creates intermediate directories. – Abhinav Jul 15 '09 at 12:07
  • 2
    On Windows, mkdir will make intermediate directories if you have "Command Extensions" enabled, which have been default since Windows NT. – crb Jul 15 '09 at 13:12
  • Nice! A word of caution, though — watch out for name collisions between folders and subfolders (e.g. "foo\bar" and "bar\baz"). You can get around this by changing "%~ny\%x" to e.g. "C:\target\%~ny\%x" (and omit the first line as previously discussed). – Ben Blank Jul 22 '09 at 0:06

Hafthor's 2nd and 3rd lines near enough do the trick, but I wanted to be able to specify a directory so modified them to be:

for /d %%x in ("%1") do for /d %%y in ("%%x\*") do md "%%~ny\%%x"
for /d %%x in ("%1") do for /d %%y in ("%%x\*") do move "%%x\%%~ny\*" "%%~ny\%%x"

Actually, I went a fair bit further and develop the following script - I take no responsibility whatsoever for any weirdness / unwanted deletion of files that may occur; run at your own risk:

@echo off
if [%1]==[] goto noparameter
if "%1"=="*" goto nowildcards
if "%1"=="/?" goto help
if not exist %1 (
  echo The directory does not exist.
  goto exit

echo Creating new directories...
for /d %%x in ("%1") do for /d %%y in ("%%x\*") do md "%%~ny\%%x"
echo Moving files...
for /d %%x in ("%1") do for /d %%y in ("%%x\*") do move "%%x\%%~ny\*" "%%~ny\%%x"
echo Files were flipped for directory %1; this directory now contains (should be empty):
dir %1 /S /B
echo =======================================================================
echo The previous (sure it's empty?) directory structure will now be deleted

if "%2"=="/F" (
  echo Deletion forced
  rmdir %1 /S /Q
) else (
  rmdir %1 /S

goto exit

echo You must specify a directory to flip.
goto exit

echo You cannot use a wildcard (asterisk) with this script.
goto exit

echo Takes directory structure a/b and converts it to b/a, moving any files within.
echo Works only with a single directory when you are within its parent.
echo FLIP [directory] [/F]
echo.  /F  Force deletion of directory structure without prompt
goto exit

  • Ahhhh, don't try to be clever after a long day at work and run a folder-creating script within a for .. do loop which iterates over every folder! :) – Alistair Knock Jul 21 '09 at 22:33

I don't know batch scripting, but this is probably the general procedure you'd want to use:

  1. Create an external folder for each size folder.
  2. Move each size folder to the external directory, renaming it to the name of its parent (the specific directory name).

And, uh... that's it, I guess. Not too complex for now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.