Hello everyone
I’m an experienced (yet out of practice!) programmer who has become excited about technology once again through the purchase of an iMac; I love it!
However, here’s my problem. I have a folder full of sub-folders and music files. In some of the folders there is a .wma and a .mp3 for each track. In addition, sometimes there are preview files which are 30 second snippets with the “-prv” appended to the filename. These may also be in either/both mp3 and WMA. For each track, I want to end up with either a single mp3 or a single WMA. Where I have both, I’d like to delete the WMA.
I now want to import that entire music collection into iTunes and wanted to clean up. Here’s what I’m looking to do:
- Create a script to point at a folder and go through entire tree
- For each filename {
if filename.extension=mp3 and ifexist filename.WMA then delete filename.WMA
delete filename-prv.*
}
Does anyone know an easy way to do this?
This code could be optimized a few different ways, or it could be approached differently all together, but it worked in my brief testing.
(*
This version can delete files that have the same name, but different extensions even though they may reside
in different locations and thus are different files.
See the version below for a version that checks path as part of the comparison. This version is left as it was
the starting point.
set folPath to (choose folder) as Unicode text
set nameList to {}
--
tell application "Finder"
try
set mp3List to (files of entire contents of folder folPath whose name extension is "mp3") as alias list
on error
set mp3List to (files of entire contents of folder folPath whose name extension is "mp3") as alias as list
end try
--
try
set wmaList to (files of entire contents of folder folPath whose name extension is "WMA") as alias list
on error
set wmaList to (files of entire contents of folder folPath whose name extension is "WMA") as alias as list
end try
--
repeat with a_mp3 in mp3List
if name of a_mp3 contains "-prv.mp3" then
move a_mp3 to trash
else
set filename to name of a_mp3
set end of nameList to text 1 thru -5 of filename
end if
end repeat
--
repeat with a_wma in wmaList
set filename to name of a_wma
if nameList contains (text 1 thru -5 of filename) or filename contains "-pr.wma" then
move a_wma to trash
end if
end repeat
end tell
*)
And another variation… same idea just using handlers to cut down on some redundant code.
set folPath to (choose folder) as Unicode text
set nameList to {}
set mp3List to listFiles(folPath, "mp3")
set wmaList to listFiles(folPath, "WMA")
set nameList to checkList(mp3List, "mp3", nameList)
set nameList to checkList(wmaList, "WMA", nameList)
on listFiles(folPath, extension)
tell application "Finder"
try
return (files of entire contents of folder folPath whose name extension is extension) as alias list
on error
return (files of entire contents of folder folPath whose name extension is extension) as alias as list
end try
end tell
end listFiles
on checkList(fileList, extension, nameList)
tell application "Finder"
repeat with aFile in fileList
if (aFile as Unicode text) ends with ("-prv." & extension) then
move aFile to trash
else if extension is "mp3" then
set end of nameList to text 1 thru -5 of (aFile as Unicode text)
else
if nameList contains (text 1 thru -5 of (aFile as Unicode text)) then move aFile to trash
end if
end repeat
end tell
return nameList
end checkList
Realized that files with the same name, but different locations could accidently be deleted using either script so the second has been updated and should work in any situation. The first version is NOT fixed, just the second.
James
That second script seems to work an absolute treat! Only issue is when I point it at a very large folder, the finder seems to time out with an event timeout error. It also crashes finder and I have to relaunch it. Any ideas?