First off, sorry for being a little bit inconclusive about this, but i am a complete newbie at Applescript so, I’m just going to lay my question out there, and hope that some of you can help me solve this.
I have a lot of files, and I mean a lot of them, that are saved with dates (dd/mm/yy) in that order, and for archiving reasons, that is not a good solution (Example: “010811 Hamburger”). So you get the picture? I need to change the name of all of these files so that the date order is (yy/mm/dd). Reversing the names will will help me locate the files according to years, months and then days, making it easier to find them. Can anyone help me with this, either by using Applescript or something in Xcode or anything at all?
Any help I get is very much appreciated! Thank you in advance!
I have made a script for you. I have thought the process to be like this:
First you take a backup of the folder with the files you are going to rename.
Then you keep a finder window in front of you, which show the contents of that folder in list view (just the contents of the folder, line by line), so that that finder window is the active one before you run the script.
You open the script below in AppleScript Editor, and run it by hitting the green button.
When you are done, you check the contents of the folder “rename conflicts” which was made in the process, and shows the contents of that folder also in list view, and rerun the script. Maybe you’ll have to run the script once more fore what I know to resolve the last conflicts, but I doubt it.
Then you copy the first level of rename conflicts files back into the main folder, then the second level of rename conflict files, and so on.
Remember to make a backup before you start!
script o
property flist : {}
property nmbers : {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
end script
tell application "Finder"
tell target of front Finder window
if not (exists folder "rename conflicts") then
make new folder at folder (name of it) of its container with properties {name:"rename conflicts"}
set tmp_fol to result as alias
else
set tmp_fol to folder "rename conflicts" as alias
end if
set o's flist to name of every file of it -- as alias list
repeat with i from 1 to (length of o's flist)
if (numbersWhereItShould of me for item i of o's flist) then
-- we can make up a new filename:
set nn to newname of me for item i of o's flist
if not text 1 thru 6 of nn = text 1 thru 6 of item i of o's flist then
-- 101010 and such are valid dates. but symmetrical
if exists file nn then
move file nn to tmp_fol replacing no
end if
set name of file (item i of o's flist) to nn
end if
end if
end repeat
end tell
end tell
on numbersWhereItShould for aname
tell (offset of "." in aname)
if it is 0 and length of aname < 7 then
return false
else if it < 7 then
return false
end if
end tell
repeat with ach in (characters 1 thru 6 of aname)
if ach is not in my o's nmbers then return false
end repeat
if text 7 of aname is not space then
return false
else
return true
end if
end numbersWhereItShould
on newname for aname
local dy, mnth, yer
set dy to text 1 thru 2 of aname
set mnth to text 3 thru 4 of aname
set yer to text 5 thru 6 of aname
return (yer & mnth & dy & text 7 thru -1 of aname)
end newname
I’m glad it worked, I had a bit of angst for that one, as for the rename conflicts. But the scheme by moving things into a conflicts folder, should at least make things resolveable. And now with quicklook and everything it should be easy to figure out what happened, (or what not). If you have Xcode installed, then you can later on also do a directory compare, with the filemerge utility (against the name conflicts directory, or an original backup) to further figure out what happened.
Renaming a bunch of files, should never be taken lightly when there is a chance for name conflicts, as it can easily turn into a mess. Always take a backup of the folder first!
The numbersWhereItShould handler may be simplified a bit
script o
property flist : {}
property nmbers : {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
end script
on numbersWhereItShould for aname
tell (offset of "." in aname)
if (it is 0 and length of aname < 7) or it < 7 then return false
end tell
repeat with ach in (characters 1 thru 6 of aname)
if ach is not in my o's nmbers then return false
end repeat
return text 7 of aname is space
end numbersWhereItShould
(numbersWhereItShould of me for "311243 azer.txt")
Yvan KOENIG (VALLAURIS, France) mercredi 13 novembre 2013 17:28:32
Now that is better for speed but it is a bit worser to read for the uninitiated. Sometimes people refuse to run script’s they don’t understand. I think your version is better, and I admit to having speed in mind when I wrote the script, since Finder is so slow.
By the way: The only sure thing in Computer Science is that everything is a trade-off.
By the way: here is an attempt of writing a cyclomatically correct handler, (one that has only one return-path), which is practical when the handlers are bigger, no point really with handlers the size as this one.
on numbersWhereItShould for aname
local was_ok
set was_ok to false
tell aname
if length of it > 6 and text 7 of it is space then ¬
tell (offset of "." in it) to if it > 7 then
set was_ok to true
repeat with ach in (characters 1 thru 6 of aname)
if ach is not in my o's nmbers then
set was_ok to false
exit repeat
end if
end repeat
end if
end tell
return was_ok
end numbersWhereItShould
on numbersWhereItShould for aname
try
set dtnm to text 1 thru 6 of aname
return (((dtnm as number) = (dtnm as integer)) and (character 7 of aname is space))
on error
return false
end try
end numbersWhereItShould
on newname for aname
set dtnm to (text 1 thru 6 of aname) as integer
return ((dtnm mod 100 * 10000 + dtnm div 100 mod 100 * 100 + dtnm div 10000) as text) & text 7 thru -1 of aname
end newname
set aname to "311243 azer.txt" -- L'anniversaire de quelqu'un. :)
if (numbersWhereItShould for aname) then set anewname to newname for aname
There were a couple of errors in the first handler which crept in somewhere between testing and posting. I’ve put them right now. Thanks for making me check! :rolleyes: