We help IT Professionals succeed at work.

coldfusion delete files based on datelastmodified

Paulconsulting
Paulconsulting used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2015

Commented:

<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>

Author

Commented:
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.  

Author

Commented:
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'}  
 
Most Valuable Expert 2015
Commented:
Hmm... I don't know if 6.1 even supports CAST.  If worse comes to worse, you could loop and do the date comparison there I guess.  Not ideal... but the right way isn't supported ...
Most Valuable Expert 2015

Commented:
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>

Author

Commented:
Thanks i did that and it works great
Most Valuable Expert 2015

Commented:
Welcome :)