Monday, January 12, 2009

Removing duplicate itunes files

This is a quick script I modified from here that moves duplicate iTunes files so you can review them before deleting them. Get the full description of how to remove files from iTunes from here.

To run the script copy the text to a new file and go to a command prompt and type:

cscript SAVED_FILENAME
example: cscript movefiles.vbs
---------script below---------------------


Set objFSO = Wscript.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\Users\Public\Music\iTunes")
Set colSubFolders = objFolder.Subfolders
newBaseDir = "C:\removed\" 'top level path for folders/files to be moved into

'get inside each artist's folder
For Each objSubFolder in colSubFolders
'Wscript.Echo objSubFolder.Name
'once inside the folder do the following...
Set colSubSubFolders = objSubFolder.Subfolders
'Get inside each album folder
For Each objSubSubFolder in colSubSubFolders
'once inside do the following
'WScript.Echo vbtab & objSubSubFolder.name
Set colMP3Files = objSubSubFolder.Files
For Each objMP3File in colMP3Files
'Wscript.Echo vbtab & objMP3File.Name
strCurrentMP3Name = objMP3File.Name
For Each objMP3File2 in colMP3Files
'Wscript.Echo Left(strCurrentMP3Name, Len(strCurrentMP3Name)-4) & vbtab & Left(objMP3File2.Name, Len(objMP3File2.Name)-6)
If UCase(Left(strCurrentMP3Name, Len(strCurrentMP3Name)-4)) = UCase(Left(objMP3File2.Name, Len(objMP3File2.Name)-6)) Then
origDir = objMP3File2.Path & "\"
newDir = newBaseDir & objSubFolder.name & "\" & objSubSubFolder.name
newFile = newDir & "\" & objMP3File2.Name
Wscript.Echo("base file: " & UCase(strCurrentMp3Name & vbtab & vbtab & vbtab & "deleting file: " & objMP3File2.Name))
'Wscript.Echo vbtab & objMP3File2.Path
If Not objFSO.FolderExists(newDir) Then
WScript.echo vbtab & "creating folder: " & newDir
CreateFolder( newDir )
End If
WScript.Echo vbtab & "...done " & objMP3File2.Path & " newFile: " & newDir

objFSO.moveFile objMP3File2.Path, newFile
End If
Next
Next
Next
Next


'Recursive folder create, will create directories and Sub
Sub CreateFolder( strPath )
Dim objFSO1
Set objFSO1 = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
If strPath <> "" Then
If Not objFSO1.FolderExists( objFSO1.GetParentFolderName(strPath) ) then Call CreateFolder( objFSO1.GetParentFolderName(strPath) )
objFSO1.CreateFolder( strPath )
End If
End Sub



==========================================
This posting is provided "AS IS" with no warranties.