Applecore Pages on Microsoft Access

Reading in the cdplayer.ini file

If you use the CD-ROM player on your computer to play audio CDs, you can store information about them in a file called 'cdplayer.ini'. Using Access, it is quite easy to import the data from this file into a database. Unfortunately, due to the format of the data, you need to use some VBA to import the data.

[21763D]
EntryType=1
artist=Metallica
title=The Unforgiven II
numtracks=4
0=The Unforgiven II
1=No Remorse
2=Am I Evil?
3=The Unforgiven II
order=0 1 2 3 
numplay=4

[264F3D]
EntryType=1
artist=Metallica
title=The Unforgiven II
numtracks=4
0=The Unforgiven II
1=The Thing That Should Not Be
2=The Memory Remains
3=King Nothing
order=0 1 2 3 
numplay=4

From the above sample, we can see a pattern emerging, which will allow us to use some VBA to loop through the file, and insert the required information into the correct places.

The table setup that I am using in this case is:

tblCD
CDIDAutoNumber
BandIDLong Integer
CDReferenceNumberLong Integer
CDNameText
NumberTracksByte
tblBand
BandIDAutoNumber
BandNameText
tblTrack
TrackIDAutoNumber
CDIDLong Integer
TrackNumberByte
TrackNameText

Then copy and paste the code below into a new module in your database, and then save this as 'mdlCDImport'. Run the procedure called 'sImportCDPlayerINI()' and then check the tables - you should have imported all of the data correctly.

    
Sub sImportCDPlayerINI()
    On Error GoTo E_Handle
    Dim db As Database
    Dim rsCD As Recordset, rsTrack As Recordset, rsBand As Recordset
    Dim strFile As String, strText As String, strCriteria As String
    Dim lngCDID As Long, lngCDReference As Long
    Dim blnNewRecord As Boolean     Set db = DBEngine(0)(0)
    Set rsCD = db.OpenRecordset("tblCD", dbOpenDynaset)
    Set rsBand = db.OpenRecordset("tblBand", dbOpenDynaset)
    Set rsTrack = db.OpenRecordset("tblTrack", dbOpenDynaset)
    strFile = "C:\Windows\cdplayer.ini" ' The normal location of the file under Windows 95/98.
    Open strFile For Input As #1
    Do While Not EOF(1)
        Line Input #1, strText
        If Left(Trim(strText), 1) = "[" Then ' We know that this is the first line of a new CD, so start adding
            lngCDReference = fHex2Dec(Mid(strText, 2, Len(strText) - 2))
            blnNewRecord=fNewRecord(lngCDReference)
            If blnNewRecord = True Then
                rsCD.AddNew
                rsCD!CDReferenceNumber = lngCDReference
                lngCDID = rsCD!CDID
                Line Input #1, strText ' EntryType line - ignore this
                Line Input #1, strText
                strCriteria = "[BandName]='" & Mid(strText, 8) & "'"
                rsBand.FindFirst strCriteria ' Check to see if the Band already exists in tblBand and add if needed.
                If rsBand.NoMatch Then
                    With rsBand
                        .AddNew
                        !BandName = Mid(strText, 8)
                        .Update
                    End With
                End If
                rsCD!BandID = DLookup("[BandID]", "[tblBand]", "[BandName]='" & Mid(strText, 8) & "'")
                Line Input #1, strText
                rsCD!CDName = Mid(strText, 7)
                Line Input #1, strText
                rsCD!NumberTracks = Mid(strText, 11)
                rsCD.Update
            End If
        End If
        If IsNumeric(Left(strText, 1)) And blnNewRecord = True Then
            With rsTrack
                .AddNew
                !CDID = lngCDID
                !TrackNumber = Left(strText, InStr(1, strText, "=") - 1) + 1 'Tracks are 0-indexed
                !TrackName = Mid(strText, InStr(1, strText, "=") + 1)
                .Update
            End With
        End If
    Loop
sExit:
    On Error Resume Next
    Close #1
    rsCD.Close
    rsBand.Close
    rsTrack.Close
    Set rsCD = Nothing
    Set rsBand = Nothing
    Set rsTrack = Nothing
    Set db = Nothing
    Exit Sub
E_Handle:
    MsgBox Err.Description, vbOKOnly + vbCritical, "Error: " & Err.Number
    Resume sExit
End Sub
 
Function fHex2Dec(strValue As String) As Long
    If Left(strValue, 2) <> "&H" Then strValue = "&H" & strValue
    If InStr(1, strValue, ".") Then strValue = Left(strValue, InStr(1, strValue, ".") - 1)
    fHex2Dec = CLng(strValue)
End Function
 
Function fNewRecord(lngCDID As Long) As Boolean
    If IsNull(DLookup("[CDID]", "[tblCD]", "[CDReferenceNumber]=" & lngCDID)) Then
        fNewRecord = True
    Else
        fNewRecord = False
    End If
End Function

I have rewritten this code slightly, to allow it to be repeatedly used on the cdplayer.ini file and not to import duplicates for CDs that had already been imported.

Top

 


HOME | NEW | TABLES | QUERIES | FORMS | REPORTS | GENERAL | API | DOWNLOADS | TUTORIAL | RESOURCES
E-MAIL
Copyright & Disclaimer

 

Last modified at 06/06/2006 14:55:24