>I want to delete some temporary directories but only older than a
>certain number of hours (say 6)
>The actual deleting is no worries - it just that I can't work out
>to only select/delete directories older than x hours.
Here's a function I wrote to purge old files. Use the createTimeSpan()
function with the "age" argument to indicate what the age of the file you
want to delete.
You should be able to modify this to do directories pretty easily.
<!---//
deletes old files from a directory
//--->
<cffunction name="purgeOldFiles" access="public" description="deletes old
files from a directory" returntype="struct" output="false">
<!---// the directory in which to purge old files //--->
<cfargument name="directory" type="string" required="true">
<!---//
how old the files should be before removing
use createTimespan() to delete in something other than days
//--->
<cfargument name="age" type="numeric" required="true">
<!---//
specify a filter to use on the cfdirectory call. this will
allow you to only
delete files with a specific extension
//--->
<cfargument name="filter" type="string" default="*.*"
required="false">
<!---// specify whether or not to run in debug mode //--->
<cfargument name="debugMode" type="boolean" default="false"
required="false">
<!---// declare local variables //--->
<cfset var local = structNew()>
<!---// delete files old than this date //--->
<cfset var dtPurgeDate = createODBCDateTime(now() - arguments.age)>
<!---// the results will be returned in this structure //--->
<cfset var result = structNew()>
<!---// get the files to purge //--->
<cfdirectory
action="LIST"
directory="#arguments.directory#"
name="local.getFilesToPurge"
filter="#arguments.filter#">
<!---// get a list of files to purge //--->
<cfset result.files = arrayNew(1)>
<cfloop query="local.getFilesToPurge">
<!---//
only delete files, ignore directories and make sure
file matches age restriction
(if dateCompare() is -1, then the file is new than
age restriction; 0 is a match; 1 is older)
//--->
<cfif (local.getFilesToPurge.type eq "File") and
(dateCompare(dtPurgeDate, local.getFilesToPurge.dateLastModified) gt -1)>
<!---// get the new array position //--->
<cfset local.iArrayPos = arrayLen(result.files)+1>
<!---// add the directory to the file name //--->
<cfif structKeyExists(local.getFilesToPurge,
"directory")>
<cfset result.files[local.iArrayPos] =
local.getFilesToPurge.directory>
<cfelse>
<cfset result.files[local.iArrayPos] =
arguments.directory>
</cfif>
<!---// if the left character is not a directory
delimiter, make it one //--->
<cfif right(result.files[local.iArrayPos], 1) neq
"\">
<cfset result.files[local.iArrayPos] =
result.files[local.iArrayPos] & "\">
</cfif>
<!---// add the file name to the directory path,
this will be the full path to the file to delete //--->
<cfset result.files[local.iArrayPos] =
result.files[local.iArrayPos] & local.getFilesToPurge.name>
</cfif>
</cfloop>
<!---// get the length of the array //--->
<cfset result.total = arrayLen(result.files)>
<!---// loop through the array of file to purge and delete the
specified files //--->
<cfloop index="local.i" from="1" to="#result.total#">
<cfif NOT arguments.debugMode>
<!---// purge the file //--->
<cffile action="delete"
file="#result.files[local.i]#">
</cfif>
</cfloop>
<cfreturn result />
</cffunction>
- Dan
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Logware (www.logware.us): a new and convenient web-based time tracking
application. Start tracking and documenting hours spent on a project or with a
client with Logware today. Try it for free with a 15 day trial account.
http://www.houseoffusion.com/banners/view.cfm?bannerid=67
Message: http://www.houseoffusion.com/lists.cfm/link=i:4:221713
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/4
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:4
Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.4
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54