Link to home
Start Free TrialLog in
Avatar of Paulconsulting
Paulconsulting

asked on

coldfusion delete files based on datelastmodified

Can someone write me a coldfusion script which looks at a directory and deletes any files which are older than 30 minutes?  Thanks in advance.
Avatar of _agx_
_agx_
Flag of United States of America image


<cfdirectory action="list" directory="c:\temp\stuff" type="file" name="getFiles">
<cfdump var="#getFiles#">
<cfquery name="getFilesToDelete" dbtype="query">
      SELECT      Directory, Name
      FROM    getFiles
      WHERE   CAST(DateLastModified AS DATE) < #dateAdd("n", -30, now())#
</cfquery>
<cfloop query="getFilesToDelete">
      <cffile action="delete" file="#Directory#/#Name#">
</cfloop>
Avatar of Paulconsulting
Paulconsulting

ASKER

aqx i am using coldfusion server 6.1 which could have issues with this syntax.  I am getting an error, here is the error:

Attribute validation error for tag directory.  
The tag does not have an attribute called type. The valid attribute(s) are action, directory, name, filter, sort, newdirectory, mode.  
ok i removed the type tag and now its down to the query but its returning this error on the query now:

Error Executing Database Query.  

Query Of Queries syntax error.
Encountered "CAST" at line 0, column 0. Incorrect conditional expression, Expected one of [like|null|between|in|comparison] condition,  
 
The error occurred in E:\Inetpub\wwwroot\egrants\reports.cfm: line 8
 
6 :       SELECT      Directory, Name
7 :       FROM    getFiles
8 :       WHERE   CAST(DateLastModified AS DATE) < #dateAdd("n", -30, now())#
9 : </cfquery>
10 : <cfloop query="getFilesToDelete">

 

--------------------------------------------------------------------------------
 
SQL    SELECT Directory, Name FROM getFiles WHERE CAST(DateLastModified AS DATE) <{ts '2010-07-01 11:26:57'}  
 
ASKER CERTIFIED SOLUTION
Avatar of _agx_
_agx_
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ugh, it doesn't support "directory" either.  But try this

<cfset direc = "c:\temp\stuff">
<cfdirectory action="list" directory="#direc#" name="getFiles">
<cfset archiveTime = dateAdd("n", -30, now())>
<cfloop query="getFiles">
      <cfif type is "file" and dateCompare(dateLastModified, archiveTime) lt 0>
            <cfoutput>debug::: deleting file #name#<br></cfoutput>
            <cffile action="delete" file="#direc#/#Name#">
      </cfif>
</cfloop>
Thanks i did that and it works great
Welcome :)