Design903

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>

Related Blog Entries

Comments
Tony Nelson's Gravatar I believe you could also delete and recreate the directory.

<cfset directoryDelete(expandPath("export/"), true) />
<cfset directoryCreate(expandPath("export/")) />
# Posted By Tony Nelson | 2/13/10 10:14 AM
Web Design Company Serving:

Longview
Tyler
Kilgore
Marshall
Jefferson
Texarkana
Nacogdoches
Lufkin
East Texas

© 2009 JMP Consulting, Inc. dba Design903 - Longview, Texas Web Design

Home | Contact Us | Site Map | Work | Services | Pricing