I want a script to rename all the files in a folder, ignoring folders within. All the names must be unique and I also want to delete any files older than 6 months. I thought, for convenience sake I’d rename the files putting the creation or modified date actually in the file name, although strictly speaking this probably isn’t necessary. I used to be able to do this stuff years agon on OS 9 but now I can’t even get Applescript to rename a single file. I’ve read what tutorials I could find and |'ve looked at Apple’s finder scripts and I find all this stuff with aliases baffling, I’m sure it never used to be like that.
What I have so far has been screwed up by me trying all sorts of different approaches. The result of what’s there is an error message saying it was unable to find the file (states first file in the folder)
The broader picture is this. I already have a working Transmit 2 script which downloads the last week’s logfile for a particular client. This file is always called clientName-access_log.1
So when downloaded it would overwrite the previous week’s file had it not been renamed to something else. I want to store 26 weeks of logs in 26 files and then after 26 weeks start deleting the oldest file.
My approach is like this:
download file log.1 to folder
rename log.1 to creationDate-logfile
test creation date of ALL files in the folder and remove any older than 6 months
Here’s what I have, but it’s pretty bad. I left out the transmit part since it works and I have no problem downloading the initial file.
Any help would be appreciated.
Lee
UPDATE: just to show I’m not completely lazy. I’ve managed to work out the majority of it. The only thing I’m having trouble with is detecting whether an item is a folder or not. This does not work…
tell application "Finder"
set the item_list to list folder theLocalDirectory without invisibles
set theLocalDirectory to theLocalDirectory as string
repeat with theNewLocalFile in item_list
set this_item to theLocalDirectory & ":" & theNewLocalFile as alias
set this_info to info for this_item
--display dialog (folder of this_info as string)
if folder of this_info is false then
set theCreationDate to creation date of this_item
set newname to clientShortName
set todaysDate to current date
if theCreationDate < todaysDate - 16000000 then
--display dialog "oops"
move this_item to the trash
end if
set theMonth to month of theCreationDate
set theDay to day of theCreationDate
set theYear to year of theCreationDate
set newname to newname & "-" & theYear & "-" & theMonth & "-" & theDay & ".log"
display dialog newname
set name of this_item to newname
end if
end repeat
empty trash
end tell