Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

up vote 2 down vote accepted

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.

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

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

goto exit

:noparameter
echo You must specify a directory to flip.
goto exit

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

:help
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.
echo FLIP [directory] [/F]
echo.
echo.  /F  Force deletion of directory structure without prompt
goto exit

:exit
share|improve this answer
    
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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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