Renaming files and deleting stale ones

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

The way to identify whether an alias is a file or folder is to check its ‘class’ property:

...
      set this_item to theLocalDirectory & ":" & theNewLocalFile as alias 
      if class of this_item is folder then
            -- we have a folder
      else 
            -- we have something else
      end if
...

Thanks for the response, but it failed to work as expected. I can’t think what’s going wrong. Here’s the altered code.


tell application "Finder"
	set the item_list to list folder theLocalDirectory without invisibles
	--set the item_list to list of folder theLocalDirectory
	display dialog item_list as string
	set theLocalDirectory to theLocalDirectory as string
	repeat with theNewLocalFile from 1 to number of items in item_list
		set this_item to item theNewLocalFile of the item_list
		set this_item to (theLocalDirectory & ":" & this_item) as alias
		--display dialog (folder of this_info as string)
		if class of this_item is not folder 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

It still renames the folder contained in theLocalDirectory. Any ideas why this is happening :?

I’d love to be able to help, but your script works [with one small modification] on my machine. It skips folders, like it should.

Here’s the first mod:

set theCreationDate to creation date of this_item

should be

set theCreationDate to creation date of this_info

Here’s the second mod:

The only applescript statement which requires a tell Finder is the rename statement. So I removed the “Tell Finder” and “end Tell” and used

tell application "Finder" to set name of this_item to newname