Page 1 of 1

Import MPC as TXT 1.1 [MM2+3]

Posted: Wed Dec 21, 2005 8:18 am
by trixmoto
This script imports an "MP3 Collectorz" database that has been exported as a TXT. It is a custom script so will probably need modifying for your own export.

A log file (.log) is created with your .txt database file, this lists any lines which could not be imported.

Please read this before using: http://www.mediamonkey.com/forum/viewtopic.php?t=6994

An installer can be download from my website (as always) :)

Code: Select all

'
' MediaMonkey Script
'
' NAME: ImportMPCasTXT 1.1
'
' AUTHOR: trixmoto (http://trixmoto.net)
' DATE : 22/12/2005 
'
' INSTALL: Copy to Scripts directory and add the following to Scripts.ini 
'          Don't forget to remove comments (') and set the order appropriately
'
' FIXES: More stable when converting numeric strings
'
' [ImportMPCasTXT]
' FileName=ImportMPCasTXT.vbs
' ProcName=ImportMPCasTXT
' Order=16
' DisplayName=Import MPC as TXT
' Description=Import MPC as TXT
' ScriptType=0 
'

Sub ImportMPCasTXT

  res = InputBox ("Enter filename of TXT to import:","Import MPC as TXT")
  Set progress = SDB.Progress
  progress.Text = "Initialising..."&res
  Set filesys = CreateObject("Scripting.FileSystemObject")
  If filesys.FileExists(res) Then
    progress.Text = "Creating logfile..."
    path = Left(res,InStrRev(res,"."))&"log"
    Set logfile = filesys.CreateTextFile(path, True)
    logfile.WriteLine("Import MPC as TXT") 
    logfile.WriteLine("By Trixmoto")
    logfile.WriteLine("")
    
    progress.Text = "Opening file: "&res
    logfile.WriteLine("Opening file: "&res)
    Set file = filesys.OpenTextFile(res, 1, false)
    If not file.AtEndOfStream Then
      line = file.ReadLine
      head = Split(line,chr(9))
      progress.Text = "Reading "&UBound(head)+1&" headers..."
      logfile.WriteLine("Number of fields = "&(UBound(head)+1))
      For i = 0 to UBound(head)
        head(i) = Mid(head(i),2,Len(head(i))-2)
      Next
    End If
    
    progress.Text = "Counting rows..."
    curr = 0
    count = 0
    total = 0
    Do while not file.AtEndOfStream
      line = file.ReadLine
      total = total + 1
    Loop
    progress.MaxValue = total
    logfile.WriteLine("Number of lines  = "&total)
    logfile.WriteLine("")
    
    Set file = filesys.OpenTextFile(res, 1, false)
    line = file.ReadLine
    Do while not file.AtEndOfStream
      line = file.ReadLine
      curr = curr + 1
      progress.Text = "Reading row "&curr&" of "&total&"..."
      progress.Value = curr
      row = Split(line,chr(9))
      If UBound(row) = UBound(head) Then
        count = count + 1
        Set track = SDB.NewSongData
        For i = 0 to UBound(row)
          field = Mid(row(i),2,Len(row(i))-2)
          If field <> "" Then
            Select Case head(i)
              Case "Filepath"
                track.Path = field
              Case "Track Artist" 
                track.ArtistName = field
              Case "Title"
                track.Title = field
              Case "Album"
                track.AlbumName = field
              Case "Length"
                track.SongLength = getmilli(field)
              Case "Size"
                track.FileLength = getbytes(field)
              Case "Format"
                'ignore
              Case "Bitrate"
                track.Bitrate = getbitrate(field)
              Case "Genre"
                track.Genre = field
              Case "Location"
                track.Custom1 = field
              Case "Notes"
                track.Comment = field
              Case "Year"
                If isNumeric(field) Then track.Year = Clng(field)
            End Select
          End If
        Next 
        track.updateDB
        Set track = Nothing
      Else
        logfile.WriteLine("Error on line "&curr&": "&line)
      End If
      If progress.Terminate Then Exit Do
    Loop
    
    If count = total Then
      If count = 0 Then
        res = SDB.MessageBox("Database not imported", mtError, Array(mbOk)) 
      Else
        res = SDB.MessageBox("Database successfully imported", mtError, Array(mbOk))
      End If
    Else
      If count < total Then
        res = SDB.MessageBox((total-count)&" rows could not be imported", mtError, Array(mbOk))
      Else
        res = SDB.MessageBox((count-total)&" rows have been imported more than once", mtError, Array(mbOk))
      End If
    End If
    
    logfile.WriteLine("")
    logfile.WriteLine("Number of lines read     = "&curr)
    logfile.WriteLine("Number of lines imported = "&count)
    logfile.WriteLine("Number of errors         = "&(curr-count))
    
    file.Close
    logfile.Close
    Set file = Nothing 
    Set logfile = Nothing
  Else
    res = SDB.MessageBox("This database could not be found", mtError, Array(mbOk))
  End If
  
  Set progress = Nothing
  Set filesys = Nothing

End Sub 

Function getmilli(str)
  mil = Clng(0)
  pos = InStr(str,":")
  str1 = Left(str,pos-1)
  If isNumeric(str1) Then
    str2 = Mid(str,pos+1,Len(str)-pos)
    If isNumeric(str2) Then
      min = Clng(str1)
      sec = Clng(str2)
      mil = sec*1000 + min*60000
    End If
  End If
  getmilli = Clng(mil)
End Function

Function getbytes(str)
  bs = Clng(0)
  str = Replace(str," KB","")
  str = Replace(str,".","")
  If isNumeric(str) Then
    bs = Clng(str)*1024
  End If
  getbytes = Clng(bs)
End Function

Function getbitrate(str)
  bps = Clng(0)
  pos = InStr(str,"k")
  str = Left(str,pos-1)
  If isNumeric(str) Then
    bps = Clng(str)*1000
  End If
  getbitrate = Clng(bps)
End Function

Sub Install()
  Dim inip : inip = SDB.ApplicationPath&"Scripts\Scripts.ini"
  Dim inif : Set inif = SDB.Tools.IniFileByPath(inip)
  If Not (inif Is Nothing) Then
    inif.StringValue("ImportMPCasTXT","Filename") = "ImportMPCasTXT.vbs"
    inif.StringValue("ImportMPCasTXT","Procname") = "ImportMPCasTXT"
    inif.StringValue("ImportMPCasTXT","Order") = "16"
    inif.StringValue("ImportMPCasTXT","DisplayName") = "ImportMPCasTXT"
    inif.StringValue("ImportMPCasTXT","Description") = "Import MPC library as text"
    inif.StringValue("ImportMPCasTXT","Language") = "VBScript"
    inif.StringValue("ImportMPCasTXT","ScriptType") = "0"
    SDB.RefreshScriptItems
  End If
End Sub

Posted: Wed Dec 21, 2005 8:21 am
by trixmoto
:o NEW CODE BELOW :o

Posted: Wed Dec 21, 2005 4:20 pm
by Steegy
Let's not forget the script to relink imported database entries to their cd when it is entered. 8)
http://www.mediamonkey.com/forum/viewto ... 1039#31039

Cheers
Steegy

Posted: Thu Dec 22, 2005 4:58 am
by trixmoto
I did ask them to read that whole thread, but yes, that script is certainly an important part of it! :)

Posted: Thu Dec 22, 2005 6:33 am
by trixmoto
New version (1.1) is more stable when importing numeric strings (file size, bitrate and song length).

Code: Select all

See first post

Posted: Sun Aug 20, 2006 11:27 am
by trixmoto
Why have you quoted my code - did you not have anything to say?

Posted: Sun Aug 20, 2006 11:43 am
by Lowlander
This occurs frequently on the forum. Some "guest" things it's necessary to quote a post without posting any thing him/herself. Rather useless I think.

Posted: Sun Aug 20, 2006 3:10 pm
by Bex
Ive seen that alot! Could it be spammers who test something?

Posted: Sun Aug 20, 2006 3:27 pm
by Lowlander
I've checked the posts and there are no hidden links.

Posted: Wed Nov 07, 2007 11:28 am
by trixmoto
An MM3 installation package is now available from my website.