PoshCode Archive  Artifact [1700dd48ce]

Artifact 1700dd48cec0cd8740f75dda14c2fa7897185da6820b32b71ff553584ead6088:

  • File Remove-DeadITunesTracks.ps1 — part of check-in [67c3689e17] at 2018-06-10 13:41:46 on branch trunk — This script will go through your ITunes library and check the paths for each of the Tracks. If it doesn’t find a file at the specified location it will remove that track from your ITunes Library. (user: Mark Schill size: 1964)

# encoding: ascii
# api: powershell
# title: Remove-DeadITunesTracks
# description: This script will go through your ITunes library and check the paths for each of the Tracks. If it doesn’t find a file at the specified location it will remove that track from your ITunes Library.
# version: 0.1
# author: Mark Schill
# license: CC0
# x-poshcode-id: 4486
# x-archived: 2015-05-06T17:07:07
# x-published: 2015-09-24T04:36:00
#
#
$ITunes = New-Object -ComObject iTunes.Application
# The count is dynamic when you call this so you can't use it in the loop
# as you're deleting things or else Write-Progress throws errors at the end
$ItemCount = $ITunes.LibraryPlaylist.Tracks.Count 
$toDelete = @()
1..$ITunes.LibraryPlaylist.Tracks.Count | % {
    # Can't delete reliably in this loop since item numbers
    # get changed as you delete items causing items to be skipped
	$CurrentTrack = $ITunes.LibraryPlaylist.Tracks.Item($_)

	# File Track ??
	if ( $CurrentTrack.Kind -eq 1 )
		{
		if ( ! ([System.IO.File]::Exists($CurrentTrack.Location)) ) 
			{
			Write-Host "$($CurrentTrack.Artist) - $($CurrentTrack.Name) will be deleted."
			    $toDelete += New-Object -TypeName PSObject -Property (@{
                    Name   = $CurrentTrack.Name
                    Artist = $CurrentTrack.Artist
                    HighID = $ITunes.ITObjectPersistentIDHigh($CurrentTrack)
                    LowID  = $ITunes.ITObjectPersistentIDLow($CurrentTrack)
                    Path   = $CurrentTrack.Location
                })
			}
		}
	Write-Progress -activity "Collecting Dead Tracks" -status "$($CurrentTrack.Artist) - $($CurrentTrack.Name)" -percentComplete ( $_/$ItemCount*100)

}
$i = 0
$toDelete | % {
     Write-Progress -activity "Deleting Dead Tracks" -status "$($_.Artist) - $($_.Name)" -percentComplete (($i++)/$toDelete.Count*100)
     $ITunes.LibraryPlaylist.Tracks.ItemByPersistentID($_.HighID,$_.LowID).Delete()
}