Deleting all files in a directory with ColdFusion
This not something I have to do very often so I tend to forget how to do it although it is very easy. I have a folder that is holding temp files that get written by a data export. I need to go through and clean out these files on a daily basis.
I needed code to read a directory, loop through all files and delete them.
This is a simple task with CFDirectory and CFFile.
Here is a quick example:
<!--- Read Directory --->
<cfdirectory
action="list"
directory="#ExpandPath( 'export\' )#"
recurse="true"
listinfo="name"
name="qFile"
/>
<!--- Loop through file query and delete files --->
<cfloop query="qFile">
<cffile action="delete" file="#ExpandPath( 'export\' )##qFile.name#">
</cfloop>
Work
View Some Of Our Work
Services
What We Are Capable Of
Pricing
View Our Pricing and Packages

<cfset directoryDelete(expandPath("export/"), true) />
<cfset directoryCreate(expandPath("export/")) />