Option Explicit
'____________________________________________________________
'
'Written by Andrew Fergus 10 September 2007
'Set a reference to 'Microsoft Scripting Runtime' under VBE
' menu option Tools > References
'____________________________________________________________
Private Type TagInformation 'assumes ID3 format
Tag As String * 3
SongName As String * 30
Artist As String * 30
Album As String * 30
Year As String * 4
Comment As String * 30
Genre As String * 1
End Type
Public RowCount As Long
Public ColumnCount As Long
'SET THE DRIVE / FOLDER TO SEARCH HERE
Const MyStartFolder As String = "E:\MyFiles\Music"
'SET THE WORKSHEET NAME TO HOLD THE RESULTS HERE
Const MyOutputSheet As String = "Sheet1"
Public Sub GetMyList()
'This is the macro you run from menu option Tools > Macros
RowCount = 1
ColumnCount = 1
Application.Cursor = xlWait
RetrieveSongs (MyStartFolder)
Application.Cursor = xlDefault
MsgBox "Finished compiling song list.", vbInformation, "Done!"
End Sub
Sub RetrieveSongs(Location As String)
On Error GoTo ErrorHandler
Dim fso As New FileSystemObject
Dim fsoFile As File
Dim fsoFolder As Folder
Dim FileTag As TagInformation
'Search this folder for files
For Each fsoFile In fso.GetFolder(Location).Files
If LCase(Right$(fsoFile.Name, 3)) = "mp3" Or _
LCase(Right$(fsoFile.Name, 3)) = "wma" Then
Open fsoFile.Path For Binary As #1
With FileTag
Get #1, FileLen(fsoFile.Path) - 127, .Tag
If UCase(.Tag) = "TAG" Then
Get #1, , .SongName
Get #1, , .Artist
Get #1, , .Album
Get #1, , .Year
Call WriteSong(fsoFile.Name, _
fsoFile.parentfolder, _
RTrim(.SongName), _
RTrim(.Artist), _
RTrim(.Album), _
RTrim(.Year))
Else
Call WriteSong(fsoFile.Name, _
fsoFile.parentfolder)
End If
End With
Close #1
End If
Next
'Search this folder for more folders
For Each fsoFolder In fso.GetFolder(Location).SubFolders
Call RetrieveSongs(fsoFolder.Path)
Next
Exit_Here:
Set fsoFile = Nothing
Set fsoFolder = Nothing
Set fso = Nothing
Exit Sub
ErrorHandler:
Application.Cursor = xlDefault
Close #1
MsgBox "There was an unexpected error." & vbCrLf & _
Err.Description, vbCritical, "Error# " & Err.Number
GoTo Exit_Here
End Sub
Sub WriteSong(filename As String, _
filefolder As String, _
Optional MySongName As String, _
Optional MySongArtist As String, _
Optional MySongAlbum As String, _
Optional MySongYear As String)
Dim tmpString As String
If RowCount = 65536 Then
RowCount = 2
ColumnCount = ColumnCount + 11
Else
RowCount = RowCount + 1
End If
With Sheets(MyOutputSheet)
.Cells(RowCount, ColumnCount).Value = filename 'file name
.Cells(RowCount, ColumnCount + 1).Value = filefolder 'file location
.Cells(RowCount, ColumnCount + 2).Value = "'" & MySongName
.Cells(RowCount, ColumnCount + 3).Value = "'" & MySongArtist
.Cells(RowCount, ColumnCount + 4).Value = "'" & MySongAlbum
.Cells(RowCount, ColumnCount + 5).Value = "'" & MySongYear
tmpString = "'" & LCase(Right$(filename, 3))
.Cells(RowCount, ColumnCount + 6).Value = tmpString 'type
If InStr(1, filefolder, "\", vbTextCompare) > 1 Then
tmpString = "'" & Right$(filefolder, InStr(1, StrReverse(filefolder), "\", vbTextCompare) - 1)
End If
.Cells(RowCount, ColumnCount + 7).Value = tmpString 'folder
If InStr(1 + InStr(1, filefolder, "\", vbTextCompare), filefolder, "\", vbTextCompare) > 1 Then
tmpString = Left$(filefolder, Len(filefolder) - InStr(1, StrReverse(filefolder), "\", vbTextCompare))
tmpString = "'" & Right$(tmpString, InStr(1, StrReverse(tmpString), "\", vbTextCompare) - 1)
End If
.Cells(RowCount, ColumnCount + 8).Value = tmpString 'parent folder
End With
End Sub