Wednesday, 2 June 2010

Moving files out of sub-folders

When you rip a CD in Windows Media Player, it creates a folder with the name of the artist, creates a sub-folder of that with the name of the album, then puts the individual ripped tracks into that.


So far so good. [Yes I know other CD rippers are available, that's not the point].

However our Brennan displays the folder names as the album name, which means that the artist name is not visible (or, at best, buried in the individual MP3 file names). I wanted to also display the artist names at top level, to aid in browsing through albums.

Some album names are instantly recognisable - 'Underneath The Stars', 'Kind Of Blue', 'Achtung Baby' and 'Lusignac' for example - but how many 'Best Of' and 'Greatest Hits' are there, let alone the great Scandinavian CDs I was buying a few years ago (Hoven Droven, Den Fule etc.) and can't even pronounce, let alone recognise by title alone.

The slow dull manual way would be

1) Rip the CD
2) Rename the album folder to 'artist - album'
3) Copy the album folder to the top-level directory
4) Delete the now-empty artist folder
5) Repeat from 1

I couldn't find any way of altering the folder structure that WMP creates at rip time. However, faced with a slow, dull, repetitive task? I feel a script coming on ....

This is in VbScript - javascript, powershell, PHP, perl etc would all also work of course. Now I just run this after ripping a bunch of CDs: I can't automate opening the jewel case, putting the CD in the drive, waiting for the album database to recognise the CD, and click the button to start ripping, sorry.

Save it as a .vbs file, run it from the directory the MP3s are in, and all the renaming and moving is done. When I get around to it I'll add a log file, so I can know which CDs have been ripped when, and therefore which box in the attic any particular CD is going to be in.

Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

Dim folder

Dim mainFolder
Set mainFolder = fso.GetFolder("c:\temp\") ' this is the MP3s directory

Dim TLsf
Set TLsf = mainFolder.SubFolders

For Each folder in TLsf

Dim strArtist
 strArtist = folder.Name

 Dim sf
 set sf = folder.subFolders

 ' This count stops the script processing CDs that have already been processed
 If sf.Count > 0 then
  Dim strAlbum
  For Each strAlbum IN sf
   Dim strCdSub
   strCdSub = strArtist & " - " & strAlbum.name
   fso.movefolder strAlbum, strCdSub
  Next
  fso.deletefolder(folder)
  End If
Next
Of course it's your file system and you run this script against it entirely at your own risk ... but the same principle would work whenever you have a whole load of stuff in sub-folders that you want to bring higher up the directory chain. If you take out the If sf.Count > 0 Then check and make the script recursive, it will eventually bring every file to the top level directory (although then you need to check for duplicate file names etc.).

0 comments: