Music Library Cleaning with PowerShell – Removing Missing Files from iTunes

I wrote a VBS a long time ago that deletes files out of ITunes if the location does not exist.  I need to rewrite this into PowerShell…

(new-object –com itunes.application).LibraryPlaylist.Tracks | ?{ $_.Location -eq $null } | %{ $_.Delete() }

 

The PowerShell above only removes the item out of ITunes if the location is NULL.  I need to put in a step to check to see if the location is valid.

ITTrackKindFile = 1

deletedTracks = 0

on error resume next



set iTunesApp = WScript.CreateObject("iTunes.Application")

set mainLibrary = iTunesApp.LibraryPlaylist

set tracks = mainLibrary.Tracks

Set FSO = CreateObject("Scripting.FileSystemObject")



for each currTrack in tracks

	' is this a file track?

	if (currTrack.Kind = ITTrackKindFile) then

		' yes, does it have an empty location?

		if (currTrack.Location = "") 		then 

			' yes, delete it

			wscript.echo currtrack.name & " - " & currTrack.Location

			currTrack.Delete()

			deletedTracks = deletedTracks + 1

		else

			'wscript.echo currTrack.Location

			if not fso.fileExists(currTrack.Location) then

				wscript.echo currTrack.name & currTrack.Location

				pause

			end if	

		end if

	end if

next 



wscript.echo "-----------------------------------------------------"

wscript.echo "deletedTracks = " & deletedTracks
email

Written by , Posted .