Starting out my participation in this forum on a giving note. :)
WHO NEEDS THIS? (you do if...)
You've got all your CDS ripped into MP3s onto your huge hard disk,
nicely arranged by folder & filename. Their tags aren't all set,
though. So you want the tags to match the filenames. MusicMatch
Jukebox has a automatic renamer feature, that generates tags from
filenames -- great!... but it doesn't use folder names!
before: D:\MUSIC\WHO\HELP\01-love me do.MP3
after: D:\MUSIC\WHO\HELP\who - help - 01-love me do.MP3
Now the auto-renamer can get the album and artist and title, all from
the filename.
responses)
Yes, it's down and dirty. Yes, it definitely could be better (see
wishlist). But it did the trick for me!
-Josh
'--------------------------------------
'MP3 Renamer
'
' Free for personal use. For publishing, commercial use, or other
' rights, Email author. Terms will be reasonable. :)
'
' v1 - first release, May 2002
'
' There are many other free programs that do what this one does -
' the only advantage this offers is open source code.
'
'TODO / WISHLIST (in priority order)
' check for duplicate folder names (being smart about white space, and
"The")
' check for swapped album-artist order in names
' allow "skip this artist" button on dialog
' check for zero-length files
' allow caps editing - upper, lower, first-word capitalization
' trap for illegal (for CD burning, web) characters in filename
' check for filenames too long to be allowed
' try to detect existing subsets of album/artist names, using
versatile delimiters ("-" " - " "_" " " etc)
' somehow get ID3 tag info & sync with it.
' ideally it would first dig through the files, then build a
"before/after" list so user can see what's gonna happen before it
happens.
' probably should set read-only files back to readonly after we rename
'em. (see below)
' check for MP3 files in other folder depths - for example, in the
artist folder (but no album subfolder)
' check for stupid, nearly duplicate names ("Disc 1" vs "Disk 1")
' be a bit more flexible about unusually deep or shallow trees
' replace ABORT keywords with buttons (see belwo)
' (search for TODO for more minor fixes I've planned)
'
'
'MISC NOTES
'
'With VBScript there is no main() or equivalent - you simply start
coding.
' I've put the bulk of the program in a big Sub simply so I can use
the Exit Sub function.
' (I couldn't find a way to exit the entire script in mid-execution.)
'
'I didn't break it up into any more sub-functions for performance
reasons: VBScript is interpreted,
' so I'm assuming function calls would cost CPU time. Since this
program can run quite slowly on
' huge MP3 databases, I'm trying to keep it lean.
'--------------------------------Intro
defaultDirectory = "D:\MUSIC" ' this is the music library directory
default name
skip1 = "- UNKNOWN - UNTITLED -" ' these are the directory names to
ignore.
str = "Music Renamer v1 Josh White 2002" & vbcrlf & vbcrlf
str = str & "This crude program renames MP3s in your music
collection."& vbcrlf
str = str & vbcrlf & "before: D:\MUSIC\WHO\HELP\01-love me do.MP3"
str = str & vbcrlf & "after: D:\MUSIC\WHO\HELP\who - help - 01-love
me do.MP3" & vbcrlf
str = str & vbcrlf & "It ignores files buried deeper than 2 trees
since these are not likely to be the artist & album info you would
want in your filename:"
str = str & vbcrlf &
"example:D:\MUSIC\70s\WHO\HELP\beatles_help_01-love me do.MP3"
str = str & vbcrlf & "(you can work around by entering a longer root -
e.g. D:\MUSIC\70s)"
str = str & vbcrlf & "It's smart enough to prevent duplicating
existing path info in the filename. for example, it won't change the
following file:"
str = str & vbcrlf & "D:\MUSIC\WHO\HELP\thewho_help_01-love me do.MP3"
& vbcrlf
str = str & vbcrlf & "Also, it removes read-only attributes from all
your MP3 files." & vbcrlf
str = str & vbcrlf & "Also, it doesn't rename files in directories
named " & skip1 & vbcrlf
str = str & vbcrlf & "(click OK for more)"
MsgBox (str)
str = " Enter your music library path root or click Cancel to stop
it"
MusicPath = InputBox(str, "Music Renamer v1 - Josh White 2002",
defaultDirectory)
' If they clicked cancel, then the returning string is blank and the
program doesn't do anything.
' Othwerise, we call up the action!
If MusicPath <> "" then Call Actionn 'note - MusicPath doesn't need
to be a parameter
'--------------------------------------Guts
Sub Actionn
' On Error allows execution to continue despite a run-time error.
You can then build the error-handling routine inline within the
procedure.
'
' On Error Resume Next
Batch = False
Set FSO = CreateObject("Scripting.FileSystemObject")
' first, try to open the folder name
Set fFolder = FSO.GetFolder(MusicPath)
' if we catch an error, they entered an invalid path - just abort
this program. TODO itd be nice to loop 'em back.
If Err <> 0 Then
MsgBox ("Sorry, the path you entered wasn't found. Please check
the spelling (copy it out of Explorer) and run the program again.")
Exit Sub
End If
' first look in each subfolder. (These folders should be artist
names)
For Each fArtist In fFolder.SubFolders
'if the folder isnt a name we should ignore.
If InStr(Skip1, UCase(fArtist.Name)) = 0 Then
' capture the first subfolder name, which we're assuming is the
artist, not that it matters for this program
nArtist = CStr(fArtist.Name) & " - "
'if user didn't tell us to stop bugging them
If Batch = False Then
'check with user before renaming their files willy-nilly.
' NOTE - we're using a default answer so all they have to do is
push "enter" and they get the existing name.
answer = InputBox("Edit the first part of the name, or type in
'SKIP' to use all the defaults without being asked:", "Artist Name",
nArtist)
If answer = "" Then Exit Sub ' empty string means they clicked
cancel, so we should abort mission
' We're allowing the user to type in a keyword to suppress all
the interaction
' TODO - this would be better with a checkbox.
'
If answer = "SKIP" Then
Batch = True 'this flag turns off all dialogs
answer = nArtist 'if they skip the dialogs, we'll assume it's because
they like the defaults. so use the original default
End If
'set the artist name to their answer.
nArtist = answer
End If
For Each fAlbum In fArtist.SubFolders
'if it isnt a name we should ignore.
If InStr(Skip1, UCase(fAlbum.Name)) = 0 Then
' capture the second subfolder name, which we're assuming is the
album, not that it matters for this program
nAlbum = CStr(fAlbum.Name) & " - "
If Batch = False Then
answer = InputBox("Edit the album name if necessary (don't
forget the trailing dash):", "Album Name", nAlbum)
If answer = "" Then Exit Sub ' if they click cancel, then
abort mission
nAlbum = answer
End If
For Each fFile In fAlbum.Files
' copy the name out of the file
fn = fFile.Name
'MsgBox ("DEBUG found " & Cstr(fn) & " in " & nAlbum & " in " &
nArtist)
' start the new name same as the old one
fn2 = fn
' if its a MP3, continue
If UCase(Right(fn,4)) = ".MP3" Then
' if the album isn't already in the filename, then add it.
If InStr(fn, nAlbum) = 0 Then fn2 = nAlbum & fn2
' if the artist isn't already in the filename, then add it.
If InStr(fn, nArtist) = 0 Then fn2 = nArtist & fn2
'now, if the name has changed, save it.
If fn <> fn2 Then
' is it read-only? when the first bit of the attributes byte is
set to 1, the file is readonly.
If fFile.attributes and 1 Then
' try clearing it.
fFile.attributes = fFile.attributes - 1
' was there a problem clearing the bit?
If Err <> 0 Then
' if they are feeling chatty, tell them we failed to
clear the readonly bit.
If Batch = False Then MsgBox ("Sorry, the file "
& fn & " is set to read only and this program couldn't reset it. Try
renaming one file by hand to troubleshoot the problem.")
Err.Clear
End If
End If 'readonly
' TODO - We should set read-only files back to readonly.
'At this point we know the file is not read only, so we'll go ahead
and try to set the filename.
fFile.Name = fn2
If Err <> 0 Then
' can't rename, dunno why, but anyway it's not fatal. skip
it, tell them & keep truckin.
If Batch = False Then MsgBox ("Sorry, this program
couldn't set the new filename for the file " & fn & ". Try renaming
one file by hand to troubleshoot the problem.")
Err.Clear ' Clear the error.
End If
End If 'fn <> fn2
'MsgBox ("DEBUG Finished rename. filename is now " & CStr(fName.Name))
End If 'its a MP3
Next 'track
End If 'album skip
Next 'album
End If 'artist skip
Next 'artist
End Sub
'--------------------------------------