Showing posts with label Scripting. Show all posts
Showing posts with label Scripting. Show all posts

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

Wednesday, April 11, 2007

Multi Variable Batch

This is a great example for reading lines through a regular batch file and processing the input at tokens.

Lets say for example you have a file with the following input:

user1 homedir1
user1 homedir2


etc

If you want to, you could add this to your batch file;

for /f "tokens=1*" %%i in (filename.txt) do @echo %%j\%%i


Notice that %%j was not defined, but is automatically assigned a value alphabetically. The output will look something like;

homedir1\user1
homedir2\user2