Sorry I was vague about things. I think the script you posted is a good direction - and certainly beneficial for anyone in the scenario as you interpreted it - but my user needs to have more of the legwork performed by the script.
Basically, I have a script (below) that renames files in a large directory based on a delimited tabbed text file. One column has the old name, the other contains the corresponding new name. That seems to work swimmingly, with the able advice of my fellow posters.
After examining the contents of the directory in which I’m renaming, I’ve realized there are duplicate instances of the same filename. For example, aaa.psd will appear in one subfolder and then aaa.psd will appear again at some subfolder. The original script does a try, posts an error that it can’t rename more than one file of the same name and moves on.
What I’m now trying to do is accommodate those duplicates. Whenever it has multiple file names with the same string of characters, it should rename the newest one (by date modified) and delete the rest. I found a very helpful post by user “kel” for a script that does just that - but my script gets hung up. It can’t seem to sort the contents of the directory by name - it tells me the handler can’t handle a class at:
set file_refs to (sort file_refs by name)
There may be other minor defects downstream because of my not knowing how to fix this. It seems like a great script - and one that is very nearly finished - but I’m in need of a coder better than I to give it a look.
I apologize to post such bloated code, but the part in question is really just the section of code (and subroutine it refers to) that attempts to sort all the file names, find the duplicates and delete.
Thanks so much for your responses and insight.
-- DECLARE PROPERTIES
--basic file path and names
property g_home_folder_path : (path to desktop)
property g_log_file_name : "Failed_to_Rename.txt"
set sourceFolder to (choose folder) as text
set textFile to (choose file of type "TEXT") as text
-- get rid of duplicates by modification date
set file_refs to MakeFileList(sourceFolder)
set num_items to count file_refs
if num_items > 1 then
tell application "Finder"
set file_refs to (sort file_refs by name)
repeat with i from 2 to num_items
set j to i - 1
set ref_i to item i of file_refs
set ref_j to item j of file_refs
set name_i to name of ref_i
set name_j to name of ref_j
if name_i is name_j then
set mod_date_i to modification date of ref_i
set mod_date_j to modification date of ref_j
else
if mod_date_i > mod_date_j then
delete ref_j
else
delete ref_i
end if
end if
end repeat
end tell
end if
-- read in the list of names
set filenameList to paragraphs of (read file textFile)
-- in order to break out the two fields
-- with text item delimiters
set {oldDelims, text item delimiters} to {text item delimiters, tab}
set err to 0
set allFiles to count filenameList
repeat with aFile in filenameList
set {codified_name, real_name} to text items of aFile
set foundFile to do shell script "/usr/bin/find " & quoted form of POSIX path of sourceFolder & " -name " & quoted form of codified_name
if foundFile is "" then
set err to err + 1
my logMe(codified_name as text, 0)
else
try
tell application "Finder" to set name of (POSIX file foundFile as alias) to real_name
on error m number n
my logMe("RENAME FAILED: " & foundFile & " (" & codified_name & ") to " & real_name & "; error " & n & ": " & m, 1)
--error m number n -- reissue the error; uncomment this line to make the script abort on an error here
end try
end if
end repeat
set text item delimiters to oldDelims
set msg to (allFiles as text) & " files processed"
if err > 0 then set msg to return & (msg & err as text) & " files not found"
display dialog msg
--Log Entry Generation
on logMe(log_string, indent_level)
set log_target to g_home_folder_path & g_log_file_name as text
try
set log_file_ref to open for access file log_target with write permission
repeat indent_level times
write tab to log_file_ref starting at eof
end repeat
write ((log_string as text) & return) to log_file_ref starting at eof
close access log_file_ref
return true
on error
try
close access file log_target
end try
return false
end try
end logMe
-- Makes List of Files in Directory
on MakeFileList(sourceFolder)
set {saveDelims, text item delimiters} to {text item delimiters, ASCII character (13)}
set lst to list folder sourceFolder without invisibles
set i to do shell script "echo " & lst & "|sort"
set sortedList to text items of i
set text item delimiters to saveDelims
-- end try
return saveDelims
end MakeFileList