I've been thinking about it Steegy, but I'm not quite sure what to do.
While running those 70 minutes, it deleted about 4,000 unwanted album arts, renamed some thousand, added about 40,000 records to the Cover table. Exactly what I wanted, but..... there's a lot of deleting going on, and it feels a bit uncomfortable to share something like that.
Another problem is that I now I have like 5 different versions, and I believe this is one of those that I took that long. Have improved on the code, and maybe over some time something more 'public' may come out of it.
Dump of a totally unsupported script for 'viewing' only, and might not even be the right one. I'll dump a couple just to make it confusing... hehe.
UNSUPPORTED, for viewing only
Code: Select all
Dim dbT,dbS,dbA,dbI,HDDHeight,MP3Height,LastLooser
Set SDB=CreateObject("SongsDB.SDBApplication")
Set dbT=SDB.Database.QuerySongs("And SongPath like '%.mp3'")
Set fso = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
LastLooser=""
While Not dbT.EOF
Set dbS=dbT.Item
Set dbA=dbS.AlbumArt
HDDHeight=0
MP3Height=0
'First find what is biggest of them all
For j=0 To dbA.Count-1
Set dbI=dbA.Item(j)
'If HDD, remember size
If dbI.ItemStorage=1 Then
If dbI.ItemType=3 Or dbI.ItemType=0 Then
If dbI.Image.Height > HDDHeight Then HDDHeight=dbI.Image.Height
End If
End If
'If MP3, remember size
If dbI.ItemStorage=0 Then
If dbI.Image.Height > MP3Height Then MP3Height=dbI.Image.Height
End If
Next
'MP3 Exist, but lost the height test to the HDD image
If MP3Height > 0 And MP3Height <= HDDHeight Then
For j=0 To dbA.Count-1
Set dbI=dbA.Item(j)
If dbI.ItemStorage=0 Then
dbA.Delete(j)
dbA.UpdateDB
MP3Height=0
End If
Next
End If
'MP3 Won the fight, replace the existing 'Folder.jpg'
If MP3Height > HDDHeight Then
For j=0 To dbA.Count-1
Set dbI=dbA.Item(j)
If dbI.ItemStorage=0 Then
'Find what folder To write the 'Folder.jpg'
LooserImage=fso.GetParentFolderName(dbS.Path) & "\Folder.jpg"
If LastLooser<>LooserImage Then
LooserImage=fso.GetParentFolderName(dbS.Path) & "\Folder.jpg"
Set output=SDB.Tools.FileSystem.CreateTextFile(LooserImage, True)
output.WriteData dbI.Image.ImageData,dbI.Image.ImageDataLen
output.Close
LastLooser=LooserImage
Set output=Nothing
End If
dbA.Delete(j)
dbA.UpdateDB
End If
Next
End If
dbT.Next
Wend
Set dbA=Nothing
Set dbS=Nothing
Set dbT=Nothing
Set SDB=Nothing
UNSUPPORTED, for viewing only
Code: Select all
Const adOpenStatic = 3
Const adLockOptimistic = 3
Const adCmdText = &H0001
Const ForReading = 1
Dim con,rec,dic,tmd,fso
Set con = CreateObject("ADODB.Connection")
Set rec = CreateObject("ADODB.Recordset")
Set dic = CreateObject("Scripting.Dictionary")
Set tmd = CreateObject("Scripting.Dictionary")
Set fso = CreateObject("Scripting.FileSystemObject")
Set FileType = New RegExp
FileType.Global = True
FileType.IgnoreCase = True
FileType.Pattern = "\.jpeg$|\.jpg$"
'Connect to the database
dbF = "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=D:\Docs\MediaMonkey\MediaMonkey.mdb"
con.Open dbF
'Open a query containing all the song paths
SQL = "SELECT SongPath FROM Songs ORDER BY SongPath"
rec.Open SQL, con, adOpenStatic, adLockOptimistic, adCmdText
'Read unique partent folders into a dictionary as keys
While Not rec.EOF
n = "M" & rec.Fields.Item(0)
If InStrRev(n,"\")>0 Then
n=Left(n,InStrRev(n,"\")-1)
If dic.Exists(n)=False Then dic.Add n,"none"
End If
rec.MoveNext
Wend
rec.Close
For Each AlbumFolder in dic.Keys
SeeIf=fso.FolderExists(AlbumFolder)
If SeeIf Then
Set Folder = fso.GetFolder(AlbumFolder)
Set Files = Folder.Files
FirstFile=""
Counter=0
For Each file In files
If FileType.Test(file.Name) Then
tmd.Add file.Name, file.Size
End If
Next
If tmd.Count=1 Then
For Each Tmp In tmd.Keys
If RenameWinningFile(Folder, Tmp, "Folder.jpg")=True Then
UpdateTheCoverTable Folder,"Folder.jpg",3,0
WScript.Echo Folder
WScript.Echo "Old: " & Tmp
WScript.Echo "New: " & "Folder.jpg"
End If
Next
End If
'Done the single file in folder already.
' ElseIf tmd.Count=2 Then
' WScript.Echo Folder
' WinningFolderSize=0
' WinningFolderFile=""
' LoosingFolderFile=""
' BackFolderFile=""
' For Each Tmp In tmd.Keys
' lowtmp=LCase(Tmp)
' If InStr(lowtmp,"back") =0 And _
' InStr(lowtmp,"inlay") =0 And _
' InStr(lowtmp," cd") =0 And _
' InStr(lowtmp,"inside") =0 And _
' InStr(lowtmp,"_cd") =0 And _
' InStr(lowtmp,"-cd") =0 And _
' InStr(lowtmp,"(cd") =0 And _
' InStr(lowtmp,"whole") =0 Then
' If WinningFolderSize <= tmd.Item(Tmp) Then
' WinningFolderSize = tmd.Item(Tmp)
' If WinningFolderFile <> "" Then
' LoosingFolderFile = WinningFolderFile
' WinningFolderFile = Tmp
' Else
' WinningFolderFile = Tmp
' End If
' Else
' LoosingFolderFile = Tmp
' End If
' Else
' BackFolderFile = Tmp
' End If
' Next
' ElseIf tmd.Count>2 Then
'Do nothing at this point.
' End If
' If tmd.Count=2 Then
' If LoosingFolderFile = "" Then
' If RenameWinningFile(Folder, WinningFolderFile, "Folder.jpg")=True Then
' UpdateTheCoverTable Folder, "Folder.jpg", 3, 0
' End If
' If BackFolderFile <> "" Then
' If RenameWinningFile(Folder, BackFolderFile, "Back.jpg")=True Then
' UpdateTheCoverTable Folder, "Back.jpg", 4, 1
' End If
' End If
' Else
' DeleteLooserFile Folder, LoosingFolderFile
' If RenameWinningFile(Folder, WinningFolderFile, "Folder.jpg")=True Then
' UpdateTheCoverTable Folder, "Folder.jpg", 3, 0
' End If
' End If
' End If
tmd.RemoveAll
End If
Next
Sub DeleteLooserFile(XFolder,XFile)
XFF=XFolder & "\" & XFile
If fso.FileExists(XFF) Then fso.DeleteFile(XFF)
End Sub
Function RenameWinningFile(XFolder,XFile,NFile)
RenameWinningFile=False
O=XFolder & "\" & XFile
T=XFolder & "\" & "XX__XX.jpg"
N=XFolder & "\" & NFile
If fso.FileExists(O) Then fso.MoveFile O, T
If fso.FileExists(T) Then fso.MoveFile T, N
If fso.FileExists(N) Then RenameWinningFile=True
End Function
Sub UpdateTheCoverTable(xFolder,nFile,xType,xOrder)
SQLName=Replace(xFolder & "\" & nFile,"'", "''")
SQL="SELECT Count(*) FROM Covers Where CoverPath='" & SQLName & "'"
rec.Open SQL, con, adOpenStatic, adLockOptimistic, adCmdText
AlreadyThere = CInt(rec.Fields.Item(0))
rec.Close
If AlreadyThere > 0 Then
SQL="DELETE * FROM Covers WHERE CoverPath='" & SQLName & "'"
Set NRec=con.Execute(SQL)
End If
SQL="INSERT INTO Covers ( IDSong, CoverOrder, CoverPath, CoverStorage, CoverDescription, CoverType, PictureType ) "&_
"SELECT Songs.ID, " & _
xOrder &_
" AS A, '" &_
SQLName &_
"' AS B, 1 AS C, Null AS D, " &_
xType &_
" AS E, 'image/jpeg' AS F " &_
"FROM Songs " &_
"WHERE (((Songs.SongPath) Like '" &_
Replace(Mid(xFolder,2),"'", "''") & "\%'))"
Set NRec=con.Execute(SQL)
End Sub
Set dic=Nothing
Set fso=Nothing
Set NRec=Nothing
Set rec=Nothing
con.Close
Set con=Nothing