' iTunes_ExportPlaylistToFolder.vbs ' Export iTunes Playlist to a folder / drive ' ' Useful e.g. to export a Playlist into a specific folder structure ' on an USB-stick ' ' THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR ' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, ' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL ' DIRK HEDDERICH OR ANY OTHER CONTRIBUTOR BE LIABLE FOR ANY CLAIM, DAMAGES ' OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ' ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER ' DEALINGS IN THE SOFTWARE. ' ' (C) Dirk Hedderich, 2009; www.verheddert.de/itunes ' Version 1.0, 2009-02-01 Option Explicit Dim CONST_CheckModified, CONST_Classics_by_Composer, CONST_TargetRootFolder, CONST_LogFile, CONST_TestDrive ' ------------ Change here ------------ CONST_CheckModified = False ' True: Check if the file was modified / False: Check only if the file exists CONST_Classics_by_Composer = True ' True: Tracks classified as genre "Classical" will be copied by composer, not by artist CONST_TargetRootFolder = "F:/" ' Target folder; do not forget the / at the end! CONST_LogFile = "C:/temp/Export.log" ' Logfile for export status CONST_TestDrive = False ' True: Do not write folders, just create log file ' ------------ Do not change after this line ------------ ' CONST_TargetRootFolder = "C:/tmp/export/" ' For tests Dim iTunes Dim myPlaylist Dim currTrack Dim BtnCode Dim WshShell Dim track Dim myItem Dim fso Dim textFile Set WshShell = WScript.CreateObject("WScript.Shell") Set iTunes = CreateObject ("iTunes.Application") Set myPlaylist = iTunes.BrowserWindow.SelectedPlaylist Set fso = CreateObject("Scripting.FileSystemObject") Set textFile = fso.CreateTextFile(CONST_LogFile, true, true) Set currTrack = myPlaylist.tracks.Item(1) BtnCode = WshShell.Popup("Playlist '" + myPlaylist.Name + "'' will be copied to folder " + CONST_TargetRootFolder + "!", 0, "iTunes Export Playlist to Folder", 1 + 48) If BtnCode = 2 then WScript.Quit() End If Dim sortArtist Dim targetFolderA Dim targetFolderArtist Dim targetFolderArtistAlbum Dim targetFileName Dim bFileExists Dim fileObjSource Dim fileObjTarget Dim re ' Regular Expression to remove \/:*?"<>| Set re = new regexp re.Global = True ' Change globally in the whole string re.Pattern = "[\\/:\*\?""<>|]" re.IgnoreCase = False ' Track Item Variables Dim itemCompilation, itemGenre, itemSortComposer, itemComposer, itemSortArtist, itemArtist, itemAlbumArtist, itemLocation, itemAlbum, itemRead ' Loop over all Tracks of this Playlist for track = 1 to myPlaylist.Tracks.Count On Error Resume Next ' Catch errors using iTunes items ' Get item attributes do itemRead = True Err.Clear Set myItem = myPlaylist.Tracks.Item(track) If Err.Number = 0 Then itemCompilation = myItem.Compilation End If If Err.Number = 0 Then itemGenre = myItem.Genre End If If Err.Number = 0 Then itemSortComposer = myItem.SortComposer End If If Err.Number = 0 Then itemComposer = myItem.Composer End If If Err.Number = 0 Then itemSortArtist = myItem.SortArtist End If If Err.Number = 0 Then itemArtist = myItem.Artist End If If Err.Number = 0 Then itemAlbumArtist = myItem.AlbumArtist End If If Err.Number = 0 Then itemLocation = myItem.Location End If If Err.Number = 0 Then itemAlbum = myItem.Album End If textFile.WriteLine(track & ": " & itemLocation) If Err.Number <> 0 Then textFile.WriteLine("Error accessing iTunes!") itemRead = False BtnCode = WshShell.Popup("Error accessing iTunes!" + vbCrLf + "Please close all iTunes dialogues." + vbCrLf + vbCrLf + "Try again? Press Cancel to leave program.", 0, "iTunes Export Playlist to Folder", 5 + 16) If BtnCode = 2 then textFile.WriteLine("Program aborted.") WScript.Quit() End If End If Loop Until itemRead = True ' Read all item attributes, now do something ;-) On Error Goto 0 ' Normal Error Handling If itemCompilation = True Then sortArtist = "_ Compilation" ElseIf (CONST_Classics_by_Composer = True) And (itemGenre = "Classical") And ((itemSortComposer <> "") or (itemComposer <> "")) Then sortArtist = itemSortComposer If sortArtist = "" Then sortArtist = itemComposer End If Else sortArtist = itemAlbumArtist If sortArtist = "" Then sortArtist = itemSortArtist If sortArtist = "" Then sortArtist = itemArtist End If End If End If sortArtist = re.Replace(sortArtist, "_") ' F:\A\ targetFolderA = CONST_TargetRootFolder + UCase(Left(sortArtist, 1)) + "/" ' F:\A\Anastacia targetFolderArtist = targetFolderA + sortArtist + "/" ' F:\A\Anastacia\Freak of Nature targetFolderArtistAlbum = targetFolderArtist + re.Replace(itemAlbum, "_") + "/" ' F:\A\Anastacia\Freak of Nature\02 Paid My Dues.mp3 targetFileName = targetFolderArtistAlbum + Right(itemLocation, Len(itemLocation) - InStrRev(itemLocation, "\")) bFileExists = false ' Check if the file exists If fso.FileExists(targetFileName) Then if CONST_CheckModified = True Then ' Check if the file was modified Set fileObjSource = fso.GetFile(itemLocation) Set fileObjTarget = fso.GetFile(targetFileName) textFile.Write(fileObjSource.DateLastModified) textFile.Write(" - T: ") textFile.WriteLine(fileObjTarget.DateLastModified) If (Left(fileObjSource.DateLastModified, 16)) = (Left(fileObjTarget.DateLastModified, 16)) Then bFileExists = true textFile.WriteLine("E: " & targetFolderArtistAlbum) Else textFile.Write("M! ") End If Else bFileExists = True textFile.WriteLine("E: " & targetFolderArtistAlbum) End If End If ' File must be exported If not bFileExists Then textFile.WriteLine("C: " & targetFolderArtistAlbum) If Not CONST_TestDrive Then If Not fso.FolderExists(targetFolderA) Then fso.CreateFolder(targetFolderA) End If If Not fso.FolderExists(targetFolderArtist) Then fso.CreateFolder(targetFolderArtist) End If If Not fso.FolderExists(targetFolderArtistAlbum) Then fso.CreateFolder(targetFolderArtistAlbum) End If fso.CopyFile itemLocation, targetFolderArtistAlbum, True End If End If Next textFile.WriteLine("Done!") textFile.Close() WScript.Echo "Done!"