Find if a file is in the library

This forum is for questions / discussions regarding development of addons / tweaks for MediaMonkey for Windows 4.

Moderators: Gurus, Addon Administrators

pgrimmer
Posts: 17
Joined: Thu Jun 26, 2008 12:19 am

Find if a file is in the library

Post by pgrimmer »

does anyone have a snippet of code to determine if a song (designated by a complete path on a disk) exists in the current library? I know I should use the Database and QuerySongs but I don't know how to prepare the query nor do I know the format of the return. I know this is a really fundamental thing to do but I've spent a couple of hours looking in the forums and can't find what I need.

Input: a complete path to an mp3 or flac file
Output: the ID of the song if it is in the library or 0 if not found

Thanks.
pgrimmer
Posts: 17
Joined: Thu Jun 26, 2008 12:19 am

Re: Find if a file is in the library

Post by pgrimmer »

It turns out that there is a real oddity in the way that MediaMonkey's QuerySong Function works. If you pass it a string of a file path on a network such as "\\myShare\myFolder\Songs\a Song.mp3" then everything works okay. On the other hand, if you have a path no a local drive then you first have to drop the drive letter before making the call. A remote possibility exists with the return and you can have multiple returns so you have to check the returns to find the one that has the drive letter you dropped off for the call.

I don't do scripting with MediaMonkey. However, I use it as a COM object and I work with MediaMonkey from VBA in Excel and Word and also sometime using Visual Basic 6. Bellow is a function that returns the song data in .SDBSongData form from a file path either no a local drive or a network drive.

Code: Select all

Function GetSongDataFromPath(FilePath As String, SongData As SDBSongData) As Long
[color=#40BF00]' Given a file path (e.g., D:\Songs\A Song.mp3"), this function will search through the
' MediaMonkey database and return the data for the file if it is in the database.
' Due to a quirk in the database query function for MediaMonkey, the drive letter must be dropped
'  off of the file path string in order for it to work. Since it is possible, although unlikely, to have
'  a song by the same name in the same path on a different drive, we use some extra code to cycle through
'  the found songs, looking for the drive letter the same as ours. this is not done or needed if
'  the specified FilePath is on a network (e.g., "\\Share\Folder\").
' At least in VBA, this function works with Unicode FilePath strings.
'Inputs
' FilePath - complete path to the song in question. Could be .mp3, .flac etc.
' SongData - MediaMonkey SDBSongData structure to hold return info if song is found in the database
'    Note- SongData contains all 0's if the song is not found int he database.
'Return
'  0 Success! FilePath has been found in the database. SongData will hold the databse information on the file.
' -1 Could not connect to MediaMonkey  (likely MediaMonkey is not running)
' -2 FilePath was not found in the MediaMonkey database
' -3 Got path match but no drive letter match.[/color]

Dim SDB As SongsDB.SDBApplication
Dim Cntr As Long
Dim tr As SDBSongIterator
Dim stDB As String
Dim IsUNC As Boolean
Dim Success As Boolean

On Error GoTo NoConnect
Set SongData = Nothing
Set SDB = New SongsDB.SDBApplication ' set COM connection to MediaMonkey
On Error GoTo 0
With SDB.Database
   .BeginTransaction
   ' if drive letter and not UNC network path, drop drive letter
   If AscW(FilePath) <> 92 Then
      stDB = Right$(FilePath, Len(FilePath) - 1)
      IsUNC = False
   Else
      stDB = FilePath
      IsUNC = True
      End If
   ' Query the MediaMonkey database for the complete path of the song
   Set tr = .QuerySongs("NOT STRICOMPW(SongPath," & ChrW$(34) & stDB & ChrW$(34) & ")")
   If tr.EOF Then
      ' did not find the song
      GetSongDataFromPath = -2
      Exit Function
      End If
   Do
      If IsUNC Then
         Success = True
      ElseIf AscW(tr.Item.Path) = AscW(FilePath) Then
         Success = True
         End If
      If Success Then
         Set SongData = tr.Item
         GetSongDataFromPath = 0
         Exit Do
         End If
      tr.Next
      Cntr = Cntr + 1
      Loop Until (tr.EOF) Or (Cntr > 100) ' give up if we get more than 100 duplicates
   If GetSongDataFromPath <> 0 Then GetSongDataFromPath = -3 ' match but not the first character if is not a network path (UNC)
   .Commit
   End With
Set SDB = Nothing
Exit Function
NoConnect:
GetSongDataFromPath = -1
End Function

Sub TestGetSongDataFromPath()
Dim Track As SDBSongData, i As Long, myStr As String

myStr = Imports.Cells(20, 2).Value
'myStr = "F:\Media\Music\MP3s\1960's Soul\Brenton Wood\Gimme Little Sign _ Brenton Wood.mp3"
'myStr = "F:\Media\Music\MP3s\Jazz\Al Hirt\Alley Cat _ Al Hirt.mp3"
'myStr = "F:\zz\WeatherGirls.mp3"
i = GetSongDataFromPath(myStr, Track)
Select Case i
   Case 0 ' success!
      MsgBox myStr & vbCrLf & vbCrLf & "found in MediaMonkey. ID: " & Format(Track.ID, "#,##0"), vbInformation, "Successful MediaMonkey Search"
   Case -1
      MsgBox "Could not connect to MediaMonkey  (likely MediaMonkey is not running)", vbCritical
   Case -2
      MsgBox myStr & vbCrLf & vbCrLf & "was not found in the MediaMonkey database", vbCritical
   Case -3
      MsgBox "Got path match but no drive letter match."
   Case Else
      MsgBox "Unknown error"
   End Select
Set Track = Nothing
End Sub
[/size]
Post Reply