Partilhar via


Enumerating Folder Structures with VBScript to Extract ID3 Information from MP3 Files

Okay, so this has nothing to do with federated infrastructure. However, occasionally, I come across something worth preserving so I wanted to make sure I wrote it down. The hat tip here really goes to Antonin Foller (https://www.motobit.com/tips/detpg_list-id3-tags-script/), but I did make some modifications to his code to make it easier for my specific situation.

The goal of this script is to extract the ID3 meta data from my entire MP3 collection so that I can use it to ensure consistency (of artist name and genre mostly) as well as use the information for generating playlists that follow certain prescriptions like those used in formatted radio (ensuring that the same artist isn't played twice in the same hour, etc.). The script contains two sub-procedures and a very generic main procedure.

To run the script, I use the command prompt (cscript) and redirect the output to a txt file. 

Sub ListID3TagsFS(Root)
Dim SubFolder, File

'*** enumerate all files In the folder
For Each File In Root.Files
Select Case LCase(Right(File.Name, 4))
Case ".mp3"
ListID3TagsFile File
End Select
Next

'*** process all subfolders
For Each SubFolder In Root.SubFolders
ListID3TagsFS SubFolder
Next

End Sub

Sub ListID3TagsFile(File)
'*** Get id3 object To change data
Dim id3
Set id3 = CreateObject("CDDBControl.CddbID3Tag")

'*** load id3 data from a file, read only
id3.LoadFromFile File.Path, True

'*** print some of id3 tags
WScript.Echo File.Path & chr(9) & id3.LeadArtist & Chr(9) & id3.Title & Chr(9) & id3.Album & Chr(9) & id3.Year & Chr(9) & id3.Genre

End Sub

'******************************************************************
'*** Main Procedure ***
'******************************************************************

'*** Get Arguments
set oArgs = wscript.arguments
sFolder = oArgs(0)

'*** get FileSystemObject To enumerate files
Dim oFS
Set oFS = CreateObject("Scripting.FileSystemObject")

'*** call ListID3TagsFS Function with a folder object
ListID3TagsFS oFS.GetFolder(sFolder)

The output of the script is a single text file (tab delimited) that contains the entire MP3 collection. This can be edited in Excel or Access for consistency and used as an input file for the following script that will individually update each MP3 file (note however, that Excel will add quotes to some of these fields if saved as a text file - tab delimited. I found it better to actually copy the entire spreadsheet and paste it into notepad to get a "clean" text file for input. Once the output file has been cleaned up, the following script can be used to read the information from the output file back into the individual ID3 tags.

On Error Resume Next
set oArgs = wscript.arguments
sFolder = oArgs(0)

'*** get FileSystemObject To enumerate files
Dim oFS
Set oFS = CreateObject("Scripting.FileSystemObject")
Set iFile = oFS.OpenTextFile(sFolder, 1)

Do Until iFile.AtEndofStream

 LineA = iFile.Readline
Values = Split(LineA, Chr(9))
EditID3Tags Values(0), Values(1), Values(2), Values(3)
Loop

Sub EditID3Tags(File,Artist,SongTitle,Album)
'*** Get id3 object To change data
Dim id3
Set id3 = CreateObject("CDDBControl.CddbID3Tag")

'*** load id3 data from a file, read only
id3.LoadFromFile File, True

'*** Save the id3 tags
id3.Album = Album
id3.Title = SongTitle
id3.LeadArtist = Artist

id3.SaveToFile File
If Err <> 0 Then
wScript.Echo "Error: " & Artist & ": " & SongTitle & "[" & Album & "]"
Err.Clear
Else
wscript.echo "Completed " & Artist & ": " & SongTitle & "[" & Album & "]"
End If

End Sub

Comments

  • Anonymous
    March 13, 2007
    The comment has been removed
  • Anonymous
    August 18, 2007
    The comment has been removed
  • Anonymous
    August 18, 2007
    The comment has been removed
  • Anonymous
    December 17, 2007
    I believe the second attribute in the id3.LoadFromFile method controls whether or not you are opening the file as readonly. Try setting this to False or 0 ANTHONW SAYS: Dommer may be right, although I've not had any problems with the read-only flag set to true.  I have seen one problem with the existing code fairly consistently.  Whenever my file name, or any of the fields I am trying to modify, contains a comma, my script errs out.  Interestingly enough, however, the error handling doesn't kick in until the next iteration.  I haven't had a chance to run it through a script debugger to see where it might be having the problem.  My guess is that the CDDB control isn't very robust and is treating the comma as a separator.  Of course, the documentation seems hard to come by, so I cannot tell if there is anyway to escape the comma that is acceptable to the dll.