Tuesday, October 6, 2009

mp3 Woes: Getting rid of tracks, revised

So here's an updated version of the track delete script. It uses a genre change to make the genre of the track "ToDelete", and I now believe this is a much cleaner way to get rid of your junk tracks.

Benefits of removing tracks this way are:
  • You can review them in the UI before removing them

  • It updates the modified date of the track, so you can run this script for a number of date ranges, and then pick them up using one O/S search

  • Because they're in the UI, and you'll be removing them via the UI, you have the option to delete them from disk as well, further lessening any followup needed


  • As always, have a backup of your library (and library of files) made before doing any of this. Caveat Emptor and all that. :)




    ' so here we begin the main portion of the script
    Const ForReading = 1, ForWriting = 2, ForAppending = 8
    Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
    Dim fso, MyFile, FileName, TextLine
    Set fso = CreateObject("Scripting.FileSystemObject")

    ' Open the file for output.
    FileName = ".\ResultsFile.txt"
    Set MyFile = fso.OpenTextFile(FileName, ForWriting, True)

    ' Write the result of the function directly to the file
    MyFile.WriteLine findfilesmod

    ' Because you have to close every door you open
    MyFile.Close


    function findFilesMod

    ' I really like the ability to run test runs, and having the timings makes it a cake
    resultstring = ""
    resultstring = "Beginning " & cstr(time) & vbcrlf

    ' Otherwise, make some variables!
    ITTrackKindFile = 1
    deletedTracks = 0
    counter = 0

    ' Get a copy of iTunes running
    set iTunesApp = WScript.CreateObject("iTunes.Application")
    set mainLibrary = iTunesApp.LibraryPlaylist

    ' Since we're trying to get to the full list of songs, we need this
    set tracks = mainLibrary.Tracks

    ' Now then! We loop the full library to touch the tracks
    for each currTrack in tracks

    ' is this a file track?
    if (currTrack.Kind = ITTrackKindFile) then

    ' if the track has the date we're looking for, in this case 1 January 2001, then
    if (cdate(currTrack.ModificationDate) >= cdate("1/1/2001 00:00:00")) and (cdate(currTrack.ModificationDate) < cdate("1/2/2001 00:00:00")) then

    ' keep track of what's deleted, so it hits the results file
    ' Then, mark the track
    resultstring = resultstring & currTrack.Name & _
    "(" & currtrack.genre & ") - " _
    & currTrack.ModificationDate & vbcrlf
    currtrack.genre = "ToDelete"

    ' increment the counter
    counter = counter + 1

    end if

    end if
    ' if you want an early out after deleting a set number of files, you can use this
    ' otherwise, this script will loop iTunes whole library, nuking as it goes
    'if counter >= 1 then exit for
    next

    ' the last output
    resultstring = resultstring & "Counted " & counter & " track(s)." & vbcrlf
    resultstring = resultstring & "Finished at " & cstr(time) & vbcrlf

    ' return the results
    findfilesmod = resultstring

    end function