PowerShell script to add files to iTunes
PowerShell is Microsoft’s CMD.exe replacement. It is a very powerful shell that does an excellent job of bridging the gap of the DOS command line, COM objects, vbscript, and .NET. It is hard to describe it’s capabilities. You really have to experience it for yourself and have your own epiphany.
For my first PowerShell script I decided to replace the combo batch file and javascript file that I currently use to load all of the podcasts that I subscribe to into my iTunes. The old way was to run the batch file which scanned a folder and all subfolders looking for files with the archive bit set. For each file that it found it would then call the javascript file, which would load the one file into iTunes. The whole process was kind of crude. It would load any file modified in that subfolder. The files would sometimes include jpg or pdf files or even partial downloads.
Using Powershell I was able to easily include in one script the recursing of the folder structure for files with the archive bit set, selection by file extension, and calling the iTunes COM object api. It works quite well.
I basically have a server that runs Juice 24 hours a day collecting podcasts. When I get ready to synch my iPod I just run the PowerShell script and iTunes is updated. I have smart playlists that automatically organize the podcasts when they are loaded.
Read a more detailed explanation of the script here.
Get the script here: Download AddPodcastsToiTunes PowerShell script
AddPodcastsToiTunes Source Code
#************************************************ # Add Podcasts to iTunes # # Description: # # Scans a folder and all it's sub folders # for files with the Archive bit set and # a file extension that matches the list of # file extensions in the variable $Extensions # # Version: 1.0 # Created: 02/10/2008 # Author: Bruce Atkinson (bruceatk@comcast.net) #************************************************ param( [string] $PodCastPath, [string[]] $Extensions) #************************************************ # Initialize variables #************************************************ If ($Extensions -eq $Null) { $Extensions = ".mp3",".mp4",".m4a",".m4p",".m4v" #List of extensions to add to iTunes } 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 " " } #************************************************ # Search all sub-folders for new podcasts with # Archive bit set and proper extension #************************************************ $Files = get-childitem $PodCastPath -recurse $NewFiles = ($Files | Where-Object {($_.Attributes -match "Archive") -and ($Extensions -contains $_.Extension) }) #************************************************ # If some files were returned then add files to iTunes #************************************************ $i=0 If ($NewFiles -ne $Null) { write-host "Adding new files to iTunes" #************************************************ # Setup iTunes COM object and position to the # Playlist called "Library" #************************************************ $iTunes = New-Object -ComObject iTunes.Application $LibrarySource = $iTunes.LibrarySource foreach ($PList in $LibrarySource.Playlists) { if ($PList.Name -eq "Library") { $PlayList = $PList Break } } #************************************************ # For each file add to iTunes and then reset # the Archive bit #************************************************ 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 } #************************************************ # Do some cleanup of COM objects #************************************************ [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) } #************************************************ # If no files added to iTunes then display a message #************************************************ if ($i -eq 0) { write-host "No files added to iTunes" } #************************************************ # Pause so any error message can be read. #************************************************ write-host "Press any key to continue" $host.ui.ReadLine()