Copy folders that contain files with specific extensions
2015-10-15
Recently I used a tool to re-tag and move my music library. This left me a neatly organized music library and also with a music folder that contained a lot of folders that only contained e.g. the album cover (in various formats). But some songs were not picked up by the auto-tagging mechanism, so some music files still persisted in that folder.
That is why I created a script that traverses the folder structure and copies all folders that (directly) contain at least one file out of a list of desired file types (e.g. '.mp3').
Situation
If I would run the script on this folder:
C:\tagged
├─Der dicke Polizist
│ └─und die Hoffnung stirbt zuletzt!
│ folder.jpg
└─ZSK
├─[2004] From Protest to Resistance
│ ├─ folder.jpg
│ └─ Kein Mensch ist illegal.mp3
└─[2015] Herz für die Sache
folder.jpgI would expect this outcome:
C:\contains_music\tagged
└─ZSK
└─[2004] From Protest to Resistance
├─ folder.jpg
└─ Kein Mensch ist illegal.mp3Solution
Criteria
- Each album that still contains music should be copied.
- But if an album contains no more music should not be copied.
- If an artist's folder does not contain any music (also not in subdirectories), it should not be copied
Reasoning
Write a recursive function that walks the directory tree and copies a folder if it contains at least one file with one of the desired file extensions.
If it does not contain any of those files, don't copy it - this will also suffice for criteria 3.