Tuesday, September 29, 2009

mp3 Woes: Getting rid of tracks by date modified

So I'm needing to get rid of some files in iTunes. I wrote a script to seek and destroy files in my library that I imported on a specific date.

This script takes the iTunes library and loops through it, looking for tracks that were modified on a certain date. When it finds one, it deletes it from the iTunes library. This has the added benefit of removing it from all playlists as well.

It is worth noting the first comment in this script.



' this script only deletes the entries from iTunes playlist - it doesn't actually remove the files from disk!!


' 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
resultstring = resultstring & currTrack.Name & " - " & currTrack.ModificationDate & vbcrlf

' Delete the track
currTrack.delete

' 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