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.
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.
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.
ASKER
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|comp arison] condition,
The error occurred in E:\Inetpub\wwwroot\egrants \reports.c fm: 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'}
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|comp
The error occurred in E:\Inetpub\wwwroot\egrants
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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(dateLastModifi ed, archiveTime) lt 0>
<cfoutput>debug::: deleting file #name#<br></cfoutput>
<cffile action="delete" file="#direc#/#Name#">
</cfif>
</cfloop>
<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(dateLastModifi
<cfoutput>debug::: deleting file #name#<br></cfoutput>
<cffile action="delete" file="#direc#/#Name#">
</cfif>
</cfloop>
ASKER
Thanks i did that and it works great
Welcome :)
<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>