AddPodcastsToiTunes Description
For those new to Powershell here is a breakdown of how the script works.
param( [string] $PodCastPath, [string[]] $Extensions)
The first line describes the parameters to the script. The script takes two parameters. The first is $PodCastPath and it is a string containing the path to the folder structure that contains the podcasts to load into iTunes. The second paramater is an optional parameter that is used to pass in a an array of extensions. In Powershell a string like this “.mp3″,”.m4p”,”.m4a”,”.mp4″ on the command line is a single parameter containing an array of strings.
_________________________________________________________________________________
If ($Extensions -eq $Null)
{
$Extensions = ".mp3",".mp4",".m4a",".m4p",".m4v" #List of extensions to add to iTunes
}
$Files = get-childitem $PodCastPath -recurse
$NewFiles = ($Files | Where-Object {($_.Attributes -match "Archive") -and ($Extensions -contains $_.Extension) })
The first If checks to see if any extensions were passed in. If they weren’t then it is initialized to the default of looking for MP3, MP4, M4A, M4P, and M4v files.
_________________________________________________________________________________
If ($PodCastPath -eq "")
{
write-host " "
write-host "Usage: AddPodcastsToiTunes path [list of extensions]"
write-host " "
write-host "Example:"
write-host " AddPodcastsToiTunes ""e:\Podcasts"""
write-host " AddPodcastsToiTunes ""\\bedrock\Podcasts"" "".mp3"","".m4p"","".m4a"""
break
}
else
{
write-host " "
write-host "Add Podcasts to iTunes"
write-host " "
write-host "Searching "$PodCastPath
write-host "for (" $Extensions ") files"
write-host " "
}
This If checks if anything was passed in for a path. If not a Usage message is displayed and the script is exited. If something was passed in then some informational information is displayed in the console window. The cmdlet write-host writes to the console.
_________________________________________________________________________________
$Files = get-childitem $PodCastPath -recurse
$NewFiles = ($Files | Where-Object {($_.Attributes -match "Archive") -and ($Extensions -contains $_.Extension) })
If we get here then the two variables we need have been initialized and we can start doing some work. The first line retrieves information about the entire folder structure passed in and puts that into the $File variable. The $File variable is an object that can be manipulated to get and set information about the file structure.
The second line uses $Files and pipes each file/folder to the cmdlet Where-Object. Each object is compared to see if it has the Archive bit set and to see if it’s extension matches one of the extensions in the $Extensions array. This line starts to show the power of Powershell. when this command is done the new variable $NewFiles will contain all files that match that criteria. Each entry in $NewFiles will also retain all the object properties and methods that it had in $Files. This will be useful later when we want to clear the Archive bit.
_________________________________________________________________________________
$i=0
If ($NewFiles -ne $Null)
{
This next piece of code does some housekeeping and then checks to see if $NewFiles is Null. If it is then we are done and if it isn’t that means we have some files to load into iTunes.
_________________________________________________________________________________ $iTunes = New-Object -ComObject iTunes.Application $LibrarySource = $iTunes.LibrarySource
If we have work to do then it’s time to setup the connection to iTunes. The first line will create the connection to iTunes. It returns an object that you can use to control iTunes or to get information from iTunes. The second line gets us the top level Library object.
_________________________________________________________________________________
foreach ($PList in $LibrarySource.Playlists)
{
if ($PList.Name -eq "Library")
{
$PlayList = $PList
Break
}
}
This code will loop through each playlist in the library until the one called “Library” is found. Once it is found it breaks out and $PlayList will contain the object that points to the Library playlist. I don’t have any error checking here. If you don’t have a “Library” playlist then you big problems with your iTunes.
_________________________________________________________________________________
foreach ($File in $NewFiles)
{
$OperationStatus = $PlayList.AddFile($File.FullName)
write-host "Added: " $OperationStatus.Tracks.item(1).Name
[String] $Attrib = $File.Attributes
$Attrib = $Attrib.Replace("Archive,","").Trim()
$File.Set_Attributes($Attrib)
$i += 1
}
This loop loops through all the files that we found earlier. For each file we do the following:
- Add it to iTunes
- Display the status of that operation. This should write out the name that it will be in iTunes.
- The list of attributes are retrieved
- The text “Archive,” is replaced to “”. The attribute list looks something like this “Archive, Compressed”. In this example we need it to end up as just “Compressed”.
- The updated attribute list is then applied back to the original file.
- The counter $i is incremented. This is used later to display a message if no files were added.
_________________________________________________________________________________ [void][System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$OperationStatus) [void][System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$LibrarySource) [void][System.Runtime.InteropServices.Marshal]::ReleaseComObject([System.__ComObject]$iTunes)
These 3 lines are used to clean up the COM objects that we used. The reality is that you could probably not do this. I felt since you never know if iTunes is open or when it will be close that it was a good idea to clean up these objects. This is another nice thing about PowerShell in that you have access to the the .NET library. I may be a little wordy to reference them but once you do it, it’s not a big deal.
_________________________________________________________________________________
if ($i -eq 0)
{
write-host "No files added to iTunes"
}
Display a message if there weren’t any files added to iTunes.
_________________________________________________________________________________ write-host "Press any key to continue" $host.ui.ReadLine()
Prompt and wait for a keystroke. If this isn’t done than the console will just close when the script ends. It is possible to start powershell with the -noexit parameter, but I prefer to have the option of double-clicking on them in explorer and get some meaningful information. In the case of this script it will display the usage information if you did that. Without the prompt, the console would just open up and close.
