reading mp3 ID3 tag, how to delete file after reading from it, says locked in process

Jason Knapp

New member
Joined
Apr 25, 2011
Messages
2
Location
Port Washington, Wisconsin, United States
Programming Experience
1-3
Hello.
I am loading all the .mp3's in a folder using
Dim fileArray() = di.GetFiles("*.mp3")
I loop through all the files and read the ID3tags. My intention is to rename the mp3 file name using the ID3 tag. I was having issues doing this, so what I do is create a copy of the mp3 and then rename it accordingly. I want to delete the old files, and I had to add GC.Collect(), or this would not work, except for it will not delete the last file, because it is locked in a process. I assume the process is me reading from it. Is there a way to release the files I am reading from. My work around is to close the program, manually delete the last file, and then reload the program. Even sometimes I can't even delete the first file I read from. All in all, I would like to read the tags and release the file, but I just can't figure out how. If you have any suggestions please let me know.

VB.NET:
Private Sub btnGetID3Info_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetID3Info.Click
        Dim errCount As Integer = 0
        If txtBoxArtistTitlePath.Text = "" Then
            errCount = 3
        Else
            Dim strPath As String = getFilePath()
            Dim arySize As Integer = getArraySize()
            Dim buildFileName(arySize) As String
            Dim buildFilePath(arySize) As String
            Dim newFileNameArray(arySize) As String
            Dim oldFileNameArray(arySize) As String
            Dim di As New DirectoryInfo(strPath & txtBoxArtistTitlePath.Text)
            Dim fileArray() = di.GetFiles("*.mp3")
            Dim track As String = ""
            Dim songName As String = ""
            Dim i As Integer = 0
            Dim x As Integer = 0
            Dim n As Integer = 0
            Dim showChangesFrom As String = ""
            Dim showChangesTo As String = ""
            Dim iChar() As String = Nothing
            For Each file In fileArray
                Try
                    Dim id3 As New ID3Library.ID3v2Tag(file)
                    track = Mid(id3.TrackNumber, 1, 2)
                    songName = id3.Title
                    If track = "" Then
                        For n = 1 To Len(songName)
                            iChar(n) = Mid(songName, n, 1)
                            If IsNumeric(iChar(n)) Then
                                If iChar(n) = "0" Then
                                    If IsNumeric(iChar(n + 1)) Then
                                        track = iChar(n) & iChar(n + 1)
                                    End If
                                End If
                            End If
                        Next
                        If track = "" Then
                            If IsNumeric(Mid(file.Name, 1, 2)) = False Then
                                listBoxStatus.Items.Add("Incomplete ID3 Tag Info")
                                listBoxStatus.Items.Add(file.Name)
                                Exit Sub
                            Else
                                track = (Mid(file.Name, 1, 2))
                            End If
                        End If
                    End If
                    If track = "" Then
                        listBoxStatus.Items.Add("Could not find a suitable renaming scheme")
                    End If
                    If songName = "" Then
                        Dim counter As Integer = 1
                        Do While IsNumeric(Mid(file.Name, counter, counter))
                            counter += 1
                        Loop
                        songName = Mid(file.Name, counter, Len(file.Name))
                    End If
                    If Mid(songName, 1, 1) = " " Then
                        songName = Mid(songName, 2, Len(file.Name))
                    End If
                    If InStr(track, "/") > 0 Then
                        If Mid(track, 1, 1) = "0" Then
                            track = Mid(track, 1, 2)
                        Else
                            track = "0" & Mid(track, 1, 1)
                        End If
                    Else
                        If track < 10 Then
                            If Mid(track, 1, 1) = "0" Then
                                track = track
                            Else
                                track = "0" & track
                            End If
                        End If
                    End If
                    If InStr(songName, ".mp3") Then
                        buildFileName(i) = strPath & txtBoxArtistTitlePath.Text & "\" & track & " - " & songName
                        newFileNameArray(i) = track & " - " & songName
                    Else
                        buildFileName(i) = strPath & txtBoxArtistTitlePath.Text & "\" & track & " - " & songName & ".mp3"
                        newFileNameArray(i) = track & " - " & songName & ".mp3"
                    End If
                    oldFileNameArray(i) = file.Name
                    buildFilePath(i) = strPath & txtBoxArtistTitlePath.Text & "\" & file.Name
                    i += 1
                Catch ex As Exception
                    errCount = 1
                    MsgBox(ex.Message.ToString)
                End Try
            Next
            listBoxStatus.Items.Add("Finished getting ID3 Tag info")
            listBoxStatus.Items.Add(" ")
            listBoxRenameTo.Items.Add(" ")
            listBoxRenameTo.Items.Add(" ")
            fileArray = Nothing
            di = Nothing
            'GC.Collect()
            For x = 0 To arySize - 1
                showChangesFrom = oldFileNameArray(x)
                showChangesTo = newFileNameArray(x)
                listBoxStatus.Items.Add(showChangesFrom)
                listBoxRenameTo.Items.Add(showChangesTo)
            Next
            If Len(showChangesFrom) > 0 Then
                lblAcceptChanges.Visible = True
                CheckBoxNo.Visible = True
                CheckBoxYes.Visible = True
            End If
            Dim couldNotRename As String = ""
            If getUserInput() = "Yes" Then
                Try
                    For x = 0 To arySize - 1
                        If InStr(newFileNameArray(x), ":") = False Then
                            If File.Exists(buildFileName(x)) = False Then
                                File.Copy(buildFilePath(x), buildFileName(x))
                                GC.Collect()
                                File.Delete(buildFilePath(x))
                            End If
                        Else
                            couldNotRename += "Could not rename " & newFileNameArray(x) & " , name contains illegal characters" & vbCrLf
                        End If
                    Next
                    If Len(couldNotRename) > 0 Then
                        MsgBox(couldNotRename)
                        listBoxStatus.Items.Add("Could not rename some files")
                        listBoxStatus.Items.Add(" ")
                    End If
                    listBoxStatus.Items.Add("Finished copying and renaming files")
                    listBoxStatus.Items.Add(" ")
                Catch ex As Exception
                    errCount = 1
                    listBoxStatus.Items.Add("Could not rename all files: " & buildFilePath(x))
                    listBoxStatus.Items.Add(" ")
                    MsgBox(ex.Message.ToString)
                End Try

            Else
                errCount = 2
                'If MsgBox("Would you like to edit ID3 for track " & oldFileNameArray(x), MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                'Dim id3Rename As New CDDBCONTROLLibAOL.CddbID3Tag
                'id3Rename.LoadFromFile(buildFilePath(x), 0)
                'End If
            End If
        End If

        Select Case errCount
            Case 0
                listBoxStatus.Items.Add("Process complete")
                listBoxStatus.Items.Add(" ")
            Case 1
                listBoxStatus.Items.Add("Process did not complete successfully")
                listBoxStatus.Items.Add(" ")
            Case 2
                listBoxStatus.Items.Add("You did not approve the changes")
                listBoxStatus.Items.Add(" ")
            Case 3
                listBoxStatus.Items.Add("You did not input a Artist\Title")
                listBoxStatus.Items.Add(" ")
        End Select
    End Sub
 
Last edited by a moderator:

JohnH

VB.NET Forum Moderator
Staff member
Joined
Dec 17, 2005
Messages
15,438
Location
Norway
Programming Experience
10+
The only place I see files are opened are these two:
Dim id3 As New ID3Library.ID3v2Tag(file)
If that constructor doesn't close the file it read there must a be a Close or Dispose method available for that to happen later.
File.Copy(buildFilePath(x), buildFileName(x))
When Copy call returns the source/target file have been closed.
 
Top Bottom