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.

No comments: