Friday, April 20, 2007

Renaming Script

I have a ton of mp3 files in different folders which are in the format below;

Track # - Song Name (Artist Name).mp3
ex. 01 - La Fille De Pekin (Frederick Rousseau).mp3


I wanted to rename all the files so I could import them easily using the following format;

Track # - Artist Name - Song Name.mp3
ex. 01 - Frederick Rousseau - La Fille De Pekin.mp3

This format helps me to adjust the meta data while importing. I wrote the following VBScript to rename the files in a directory :D



Option Explicit
On Error Resume Next

Dim a: a = WScript.Arguments.item(0)
Dim fso: Set fso=CreateObject("Scripting.FileSystemObject")
Dim lFolder: Set lFolder = fso.GetFolder(a)
Dim lFiles: Set lFiles = lFolder.Files
Dim lFile, numFiles
numFiles = 0
For each lFile in lFiles
RenameFile(lFile)
Next

Function RenameFile(fileObject)
Dim oldName, newName, fullnewName
Dim posDash, posParOpen, posParClose
Dim artistName, songName, trackNum

oldName = fileObject.Name
'get location of the dash
posDash = InStr(oldName, "-")
'get location of the first (
posParOpen = InStrRev(oldName,"(")
'get location of last )
posParClose = InStrRev(oldName, ")")

'get the track number
trackNum = Mid(oldName,1,posDash)
trackNum = Replace(trackNum,"-","")
trackNum = trim(trackNum)

'get the song name
songName = Mid(oldName,posDash,Cint(posParOpen - PosDash))
songName = Replace(songName,"-","")
songName = Trim(songName)

'get the artist name
artistName = Mid(oldName,posParOpen,Cint(posParClose - PosParOpen))
artistName = Replace(artistName,"(","")
artistName = Trim(artistName)

'new name formatted
newName = trackNum & " - " & artistName & " - " & songName & ".mp3"
fullNewName = fileObject.ParentFolder & "\" & newName

WSCript.Echo
WScript.Echo "Renaming.. " & fileObject.Name
WScript.Echo "To........ " & newName
'comment the following line to test it only.
fileObject.Move fullNewName
WScript.Echo
numFiles = numFiles + 1
End Function


WScript.Echo "Total Files Modified: " & numFiles
Set fso = Nothing
Set lFolder = Nothing
Set lFiles = Nothing

All you have to do to use it is copy paste into a vbs file "reformat.vbs" for example. Then run it using the command line:

C:\> cscript reformat.vbs "C:\Music\Some Messed up Album\"

Here is a link to the script, if you wish to download it. Just rename the file.
Link: Reformat.txt

7 comments:

Anonymous said...

pretty sweet
i think this is the first time i see VB Script put into good use :p

Transparently said...

lol well, in the right hands, VB Script can be a powerful tool!

*halo appears*

Anonymous said...

is that possible on a mac ?

Transparently said...

As far as I know, there is no way to run VB Scripts on a mac.

Anonymous said...

hehe thats a really handy script, i\ve been wanting to do something like that for a while (too lazy)
thanx for doing all the work for me man ;p now to start fixin my 200g+ music library /shudders

Transparently said...

Nomad,
Sure no problem bro :P The only problem with VBS as stated by BB is that it is not portable. I think I'll try to switch to JS. Just incase I too, wanted to run them on my mac :P Good luck with the 200gb+

Anonymous said...

Nice dispatch and this enter helped me alot in my college assignement. Say thank you you on your information.