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 am in the process of cleaning a huge library of mangas (mostly -but not only- image files ). Each manga has a main folder containing subfolders, one for each volume.

But sometimes, volume folders contain only image files, and other times they contain subfolders.

I'd like a script that would, for the selected manga folder:

  • check which volume folders have subfolders
  • flatten the directory structure of the volume folders with subfolders. Rather than putting all images in the parent folder and erasing the subfolders, I'd like it to create a new folder on the desktop, and copy all the subfolders' images in it.
  • rename the folder created on the desktop after its original parent folder

Before :

manga_folder

volume1_folder

    subfolder1 / image1, image2, image3
    subfolder2 / image4, image5, image6
    subfolder3 / image7, image8, image9

volume2_folder

    image1, image2, image3, image4, image5, image6, image7, image8, image9

volume3_folder

    subfolder1 / image1, image2, image3
    subfolder2 / image4, image5, image6
    subfolder3 / image7, image8, image9

After:

Desktop:

volume1_folder

    image1, image2, image3, image4, image5, image6, image7, image8, image9

volume3_folder

    image1, image2, image3, image4, image5, image6, image7, image8, image9

The original folders should stay untouched so I can check for misplaced files etc.

Right now, I use an AppleScript that flatten the directory structure for each volume folder that I select. It creates a new folder inside, named after its parent folder with all the copied images inside.

What it does not :

  • check which volume folders have subfolders (I have to select each volume folder individually and launch the script each time)
  • move the created folder with flattenned directory structure to the Desktop

I did try to implement changes myself, but I could either move the new folder to the desktop or rename it after its parent folder. When I put the two instructions (whatever the one being put first), I would have an error saying that it couldn't find the folder after executing the first instruction. Here is the script :

tell application "Finder"
    set theTopFolder to (selection as alias)
    set theFiles to a reference to every file of (entire contents of folder theTopFolder)
    set theNewFolder to make new folder at theTopFolder with properties {name:"Flattened Files"}
    duplicate theFiles to theNewFolder
    set name of theNewFolder to name of theTopFolder


    --move theNewFolder to "Macintosh HD:Users:xx:Desktop:"
end tell

Thanks in advance

share|improve this question
add comment

1 Answer

tell application "Finder"
    repeat with f in (get folders of (POSIX file "/Users/username/manga_folder" as alias))
        make new folder at desktop with properties {name:(get name of f)}
        duplicate files of entire contents of f to result
    end repeat
end tell

You could also use a shell command like this:

cd ~/manga_folder;for d in *;do mkdir ~/Desktop/"$d";find "$d" -type f|while read f;do cp "$f" ~/Desktop/"$d";done;done

Or if you have GNU cp:

cd ~/manga_folder;for d in *;do mkdir ~/Desktop/"$d";find "$d" -type f -exec gcp -t ~/Desktop/"$d" {} +;done

share|improve this answer
    
Thank You. The AppleScript code did work, but it unecessarily copied all the folders on the desktop. Can it choose to copy only the folders that need to be flattened? I'd like the script to work on the currently selected folder as well if possible, like in my previous script. It's easier to manage that way. –  maqueiouseur Jan 14 at 1:25
add comment

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.