Monday, January 24, 2005

Another iPodder VBScript (Delete Old PodCasts)

Another management problem with iPodder is clearing out old PodCasts. And I have another VBScript file that can help manage this problem as well.

The following script will scan the files in your iTunes looking for iPodder files that are a week old and HAVE been played. Also, because you may run across a PodCast that you would like to keep, I have added and additional check on the Rating column. If you would like to keep a PodCast just set the rating to something other then “None”.

To run the iDelOldPodCasts.vbs script you can just double click it. Or if you want to run it with the –S command line variable you will have to create a batch file or a shortcut.

My apologies for the formatting. (Formated)

001 '-----------------------------------------------------------------------------------
002 ' This is a script file that will delete old PodCasts. This works with iPodder that
003 ' downloads files to your "My Documents\My Received Podcasts" directory. The files
004 ' in your iTunes library will be scanned looking for files that have been played
005 ' and the “Last Played” date must be older than seven days and the rating must be 0
006 ' (This will not delete old files that have ratings). You will be prompted to delete
007 ' the files. If no files where found you will get a message telling you that no
008 ' files where found. You can use the –S command line argument so that all files
009 ' will be deleted without being prompted, otherwise you will be prompted for each
010 ' file.
011 '
012 ' By: Richard Todosichuk
013 ' rtodosic@hotmail.com
014 '-----------------------------------------------------------------------------------
015 Option Explicit
016
017 Dim fso ' Scripting.FileSystemObject object used to copy files.
018 Dim Shell ' WScript.Shell object used to access information about the windows shell.
019 Dim args ' The command line argurments.
020 Dim arg ' A command line argurment.
021 Dim iTunesApp ' iTunes.Application object used to access the iTunes application.
022 Dim mainLibrary ' The iTunes Library object.
023 Dim tracks ' The tracks collection object of the Library object.
024 Dim currTrack ' The current track object from the Library in iTunes.
025 Dim TestDate ' Now minus seven days.
026 Dim PodCastDir ' The iPodder root download directory.
027 Dim FileList ' The list of files that have been copied.
028 Dim PromptVal ' The value of the message box for whether to delete the file or not.
029 Dim SilentFlag ' Command line flag that if set will not prompt the use to delete the file.
030 Dim i ' A counter variable.
031
032 Set args = WScript.Arguments
033 Set fso = CreateObject("Scripting.FileSystemObject")
034 Set Shell = CreateObject("WScript.Shell")
035 Set iTunesApp = CreateObject("iTunes.Application.1")
036 Set mainLibrary = iTunesApp.LibraryPlaylist
037 Set tracks = mainLibrary.Tracks
038
039 TestDate = (Date - 7)
040 PodCastDir = Shell.SpecialFolders("MyDocuments") & "\My Received Podcasts\"
041 FileList = ""
042 SilentFlag = False
043
044 ' Scan command line arguments
045 For Each arg in args
046 ' Is it a flag.
047 If Instr(1, arg, "-", 1) = 1 or Instr(1, arg, "/", 1) = 1 Then
048 ' Check for list flag
049 If UCase(arg) = "-S" or UCase(arg) = "/S" then
050 SilentFlag = True
051 End If
052 End If
053 Next
054
055 PromptVal = MsgBox ("Delete all PodCasts that are a week old." _
056 & chr(13) & chr(10) & chr(13) & chr(10) & "Continue?", vbYesNo + vbQuestion, "Delete PodCasts")
057
058 If PromptVal = 6 then
059
060 i = tracks.Count
061
062 While i > 0
063 Set currTrack = tracks.Item(i)
064 If currTrack.Kind = 1 AND _
065 currTrack.PlayedDate <> 0 AND _
066 currTrack.DateAdded <>
067 currTrack.Rating = 0 AND _
068 currTrack.Location <> "" Then
069 If Instr(1, currTrack.Location, PodCastDir, 1) > 0 Then
070 If SilentFlag then
071 'Delete files
072 If (fso.FileExists(currTrack.Location)) Then
073 fso.DeleteFile(currTrack.Location)
074 End If
075 currTrack.Delete
076 Else
077 PromptVal = MsgBox ("Delete file " & currTrack.Location & "?", vbYesNo + vbQuestion, "Delete PodCast")
078
079 If PromptVal = 6 then
080 'Build msg of all files
081 If FileList <> "" Then
082 FileList = FileList & chr(13) & chr(10) & currTrack.Location
083 Else
084 FileList = currTrack.Location
085 End If
086
087 'Delete files
088 If (fso.FileExists(currTrack.Location)) Then
089 fso.DeleteFile(currTrack.Location)
090 End If
091 currTrack.Delete
092 End If
093 End If
094 End If
095 End If
096 i = i - 1
097 WEnd
098
099 If FileList = "" Then
100 WScript.Echo "No files where deleted!"
101 Else
102 WSCript.Echo "The following files have been deleted:" & chr(13) &amp;amp;amp;amp; chr(10) & chr(13) & chr(10) & FileList
103 End If
104
105 End IF


(Download iDelOldPodCasts.vbs)

The code is not that complicated. Lines 45 to 53 handle searching the command line arguments that were passed into the script. Line 62 starts the loop that loops through all of the tracks in iTunes. Line 64 to 69 tests the track to see if it is in fact a file, the “Last Played” column is not blank, the “Date Added” column is older the seven days, the “Rating” column is blank, and the file is in the iPodder download directory. Then line 72 to 75 performs the delete without the prompt if –S was passed into that command line argument. And lines 77 to 85 will prompt you to delete the file and lines 88 to 91 perform the delete. After all of the tracks have been tested lines 99 to 103 will handle telling you either no files have been deleted or will display a list of all of the deleted files.

I should also mention that if you are running McAfee or other virus scanners this script may not work. Virus scanner writers like to re-implement the windows system Scripting.FileSystemObject and WScript.Shell objects. You can re-register the original windows COM objects and this will work fine. However, you should be aware that these files have been re-implemented for a reason. I don’t mean to scare people away from doing this but I mention this for two reasons. First I spent hours trying to figure out why things would not work and I don’t what you to do the same. Furthermore, if you have other virus scanners and spyware installed, you may be in luck. Better (well more script friendly) virus scanners and spyware software will detect that you are trying to run these objects and will prompt you to make sure that you really want to run them. This is a good approach. This will allow you to run VBScript files in a secure way. So, you CAN have your scripts and execute them too.

Friday, January 21, 2005

iPodder VBScript (Copy New PodCasts to My SD Card)

I have been an iPodder user for a while now. And like others I have been thinking about content management. When you are downloading large MP3 files it makes sense to find ways to try to manage these files. Because iPodder works with iTunes and because there is an API available for iTunes (on Windows) I have created some VBScript files to help me perform some common tasks.

First of all the way that I manage my files in iTunes is to display the “Last Played” column (if it is not visible in your iTunes just right click on the header and click “Last Played”). With the “Last Played” column I can quickly tell if I have listened to the PodCast or not.

The first thing that I wanted to manage was to be able to copy newly downloaded PodCasts to my Pocket PC (I have not broken down and bought and iPod yet). The script that I wrote will scan the files in iTunes looking for files that have been added in the last seven days (“Date Added” is within seven days), has not been listened to (“Last Played” is blank) and is in the iPodder download directory (this should be “My Documents\ My Received Files” directory). The files that are found will be copied to a directory that I pass into the script. I have also added a –U flag that will update the “Last Played” column to now (I assume that if I copy the files to my Pocket PC that I will listen to them on my Pocket PC).

Example:

I have a batch file with the following in it.
iNewPodCasts -U E:\Podcasts

Where iNewPodCasts is the VBScript file and –U is the flag to update the “Last Played” column and E:\Podcasts is the destination folder (my SD card that I will use in my Pocket PC). It should also be noted that the E:\Podcasts directory must exist.

Additionally I have also added a –L flag to the iNewPodCasts VBScript that will just list the new PodCast files. This is useful if you don’t want to copy the PodCast files but just see what the new PodCast are. If you use the –L flag the destination directory will be ignored.

Here is the code.
My apologies for the formatting. (Formatted)

001 '-----------------------------------------------------------------------------------
002 ' This will copy newly downloaded files from iPodder to a specified directory. Files
003 ' from the "My Documents\My Received Podcasts" directory that have not been played
004 ' and have been downloaded within the last seven days will be copied to the
005 ' destination folder. If the –U flag is in the command line argument then the
006 ' “Last Played” column will be set to now). If the –L flag is in the command line
007 ' then the files will only be listed and not copied.
008 '
009 ' By: Richard Todosichuk
010 ' rtodosic@hotmail.com
011 '-----------------------------------------------------------------------------------
012 Option Explicit
013
014 Dim fso ' Scripting.FileSystemObject object used to copy files.
015 Dim Shell ' WScript.Shell object used to access information about the windows shell.
016 Dim args ' The command line argurments.
017 Dim arg ' A command line argurment.
018 Dim iTunesApp ' iTunes.Application object used to access the iTunes application.
019 Dim mainLibrary ' The iTunes Library object.
020 Dim tracks ' The tracks collection object of the Library object.
021 Dim currTrack ' The current track object from the Library in iTunes.
022 Dim TestDate ' Now minus seven days.
023 Dim PodCastDir ' The iPodder root download directory.
024 Dim FileList ' The list of files that have been copied.
025 Dim DestFolder ' The destination folder.
026 Dim UpdateLastPlayed ' Flag to update Last Played column in iTunes.
027 Dim ListOnly ' Flag to display only a list of resent PodCasts.
028 Dim i ' A counter variable.
029 Dim Msg ' A string for a message.
030 Dim f ' A file object.
031
032 Set args = WScript.Arguments
033 Set fso = CreateObject("Scripting.FileSystemObject")
034
035 UpdateLastPlayed = False
036 ListOnly = False
037 DestFolder = ""
038
039 FileList = ""
040 TestDate = (Date - 7)
041
042 ' Scan command line arguments
043 For Each arg in args
044 ' Is it a flag.
045 If Instr(1, arg, "-", 1) = 1 or Instr(1, arg, "/", 1) = 1 Then
046 ' Check for list flag
047 If UCase(arg) = "-L" or UCase(arg) = "/L" then
048 ListOnly = True
049 End If
050
051 ' Check for update flag
052 If UCase(arg) = "-U" or UCase(arg) = "/U" Then
053 UpdateLastPlayed = True
054 End If
055 Else
056 ' Only grab the first none flag
057 If DestFolder = "" Then
058 DestFolder = arg
059 End If
060 End If
061 Next
062
063 If args.Count > 0 and (ListOnly or fso.FolderExists(DestFolder)) Then
064 If (not ListOnly) and (not fso.FolderExists(DestFolder)) Then
065 WScript.Echo "Folder " & chr(34) & DestFolder & chr(34) & " not found!"
066 Else
067 Set fso = CreateObject("Scripting.FileSystemObject")
068 Set Shell = CreateObject("WScript.Shell")
069 Set iTunesApp = CreateObject("iTunes.Application.1")
070 Set mainLibrary = iTunesApp.LibraryPlaylist
071 Set tracks = mainLibrary.Tracks
072
073 PodCastDir = Shell.SpecialFolders("MyDocuments") & "\My Received Podcasts\"
074
075 For i = 1 to tracks.Count
076 Set currTrack = tracks.Item(i)
077 IF currTrack.Kind = 1 And _
078 currTrack.DateAdded > TestDate And _
079 currTrack.PlayedDate = 0 AND _
080 currTrack.Location <> "" Then
081 If Instr(1, currTrack.Location, PodCastDir, 1) > 0 then
082 If (fso.FileExists(currTrack.Location)) Then
083 'Build msg of all files
084 If FileList <> "" Then
085 FileList = FileList & chr(13) & chr(10) & currTrack.Location
086 Else
087 FileList = currTrack.Location
088 End If
089
090 If not ListOnly Then
091 'Move the file
092 Set f = fso.GetFile(currTrack.Location)
093 f.Copy DestFolder & "\" & f.name
094 End If
095
096 If UpdateLastPlayed Then
097 'Set the lasted played to now.
098 currTrack.PlayedDate = Date
099 End If
100 End If
101 End If
102 End If
103 Next
104
105 If FileList = "" Then
106 WScript.Echo "No new files found!"
107 ElseIf ListOnly Then
108 WSCript.Echo "The following files are new PodCasts:" & _
109 chr(13) & chr(10) & chr(13) & chr(10) & FileList
110 Else
111 WSCript.Echo "The following files have been copied to " & chr(34) & DestFolder & chr(34) & ":" & _
112 chr(13) & chr(10) & chr(13) & chr(10) & FileList
113 End If
114 End If
115 Else
116 Msg = "Usage: iNewPodCasts [-L] [-U] [destFolder]"
117 Msg = Msg & chr(13) & chr(10) & chr(13) & chr(10) & _
118 "-L - Flag to only list new PodCasts. destFolder will be ignored if specified." & chr(13) & chr(10) & _
119 "-U - Flag to update the Last Played column in iTunes. destFolder must be specified." & chr(13) & chr(10) & _
120 "destFolder - The folder that new PodCasts will be copied too."
121 WScript.Echo Msg
122 End If

(download iNewPodCasts.vbs)
The code is a little long, but not that complicated. Lines 42 to 61 handle searching the command line arguments that were passed into the script. Line 63 checks to see if the arguments are valid and if not then lines 116 to 121 will display a message telling you how to call this script. If the command line arguments are valid then on line 69 the COM object that accesses iTunes is created and from that object the tracks collection of the LibraryPlayList are accessed. Then all tracks in the tracks collection are scanned and checks are performed at line the 77 to 82. Kind of 1 means that the track is a file. DateAdd must be less than the last seven days. PlayedDate is the same as the “Last Played” column and 0 means that it is blank. Location is the full file name of the track. I then check to see the location of the file is in the iPodder root directory and then I check to see that the file does in fact exist. (The file can be deleted or moved and iTunes may be out of date.) Lines 83 to 88 build a string that will be displayed at the end if the execution of the script. Line 93 does the actual copy. Line 98 updates the “Last Played” column to now. For the most part that is all there is to it.

I should also mention that if you are running McAfee or other virus scanners this script may not work.Virus scanner writers like to re-implement the windows system Scripting.FileSystemObject and WScript.Shell objects. You can re-register the original windows COM objects and this will work fine. However, you should be aware that these files have been re-implemented for a reason. I don’t mean to scare people away from doing this but I mention this for two reasons. First I spent hours trying to figure out why things would not work and I don’t what you to do the same. Furthermore, if you have other virus scanners and spyware installed, you may be in luck. Better (well more script friendly) virus scanners and spyware software will detect that you are trying to run these objects and will prompt you to make sure that you really want to run them. This is a good approach. This will allow you to run VBScript files in a secure way. So, you CAN have your scripts and execute them too.