As a follow up to my earlier post on cleaning up old files, here is a modified version that zips and deletes. This would be very handy for log directories.

<cfset logdir = "/Applications/ColdFusion8/logs">

<cfdirectory action="list" directory="#logdir#" name="files" type="file">

<cfset thirtydaysago = dateAdd("d", -50, now())>

<!--- get older files ---> <cfquery name="oldfiles" dbtype="query"> select name from files where datelastmodified < <cfqueryparam cfsqltype="cf_sql_timestamp" value="#thirtydaysago#"> and upper(name) not like '%.ZIP' </cfquery>

<cfoutput>Out of #files.recordCount# files, there are #oldfiles.recordCount# to zip.</cfoutput>

<cfif oldfiles.recordCount> <cfloop query="oldfiles"> <cfzip file="#logdir#/#name#.zip" source="#logdir#/#name#" action="zip"> <cffile action="delete" file="#logdir#/#name#"> </cfloop> </cfif>

This code is the exact same as the last version except for:

  • I switched the thirtydaysago variable to actually be 50 days ago. This was just to give me a good dataset in my log directory. The date really isn't critical.
  • My query of queries to get old files now also filters out any zips. Notice the use of upper. This ensures the case doesn't matter.
  • I added the cfzip tag, a new feature of ColdFusion 8. Nice and easy, right?