A question I tend to see a lot in the forums is:

"How do I delete a directory and all files/directories inside of that directory?".

The truth is that ColdFusion does not provide an easy way to achieve the desired result. So to be able to do it you have to get a little creative :) (Gettin creative is a good thing, trust me!)

First take a step back and define what exactly you want to delete, so we'll define a folder (path):

<cfset tDirectory = "C:\myFolder\"> <!--- this can be passed in a varaible or whatever --->

Next we will take advantage of Window's DOS (Remember DOS? yup, it still exists!) command RMDIR. We will create a string that we will write into a .bat file (.bat file is used to be executed in windows later down in this tutorial).
<cfset tString ="RMDIR /S /Q " & tDirectory> <!--- This is what we will put in the bat file --->

I want to take a moment and explain a bit further what RMDIR really is. RMDIR is a built in command that Windows operating system uses (via DOS) to actually do just what we need. It will delete a directory and all directoris and files under the directory. Now, l et's take a look at how you call it and what it's attributes are:

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\pvarando>rmdir /?

Removes (deletes) a directory.

RMDIR [/S] [/Q] [drive:]path
RD [/S] [/Q] [drive:]path

/S Removes all directories and files in the specified directory
in addition to the directory itself. Used to remove a directory
tree.

/Q Quiet mode, do not ask if ok to remove a directory tree with /S

(NOTE: To see this in your computer, click "Start" > "Run" > Type in : "cmd" (without the quotes) and the enter key. This will bring up a DOS window. Next type in : rmdir /? This will bring you the available options.

Now that we have defined what will go into the .bet file, let's actually create the .bat file:
<!--- generate a .BAT file for later execution --->
<cffile action="WRITE" file="#expandPath(".")#\delete.bat" output="#tString#">

Ok, at this point you have defined the path, the bat file and the commend calls for the .bat file, at this point let's execute it withing ColdFusion:
<!--- Now execute the file to delete everything (Folder and all sub-folders and files)--->
<cfexecute name="#expandPath(".")#\delete.bat" timeout="60"></cfexecute>

Now just to clean things up, let's delete the .bet file. (this step is optional, I just like to clean up after myself :)
<!--- now delete the bat file --->
<cffile action="DELETE" file="#expandPath(".")#\delete.bat">

That is it, that is all you need to do to delete an entire folder and all associated files (sub-folders) beneath it.

Entire code base example
<cfset tDirectory = "C:\myFolder\"> <!--- this can be passed in a varaible or whatever --->

<cfset tString ="RMDIR /S /Q " & tDirectory> <!--- This is what we will put in the bat file --->

<!--- generate a .BAT file for later execution --->
<cffile action="WRITE" file="#expandPath(".")#\delete.bat" output="#tString#">

<!--- Now execute the file to delete everything (Folder and all sub-folders and files)--->

<cfexecute name="#expandPath(".")#\delete.bat" timeout="60"></cfexecute>

<!--- now delete the bat file --->
<cffile action="DELETE" file="#expandPath(".")#\delete.bat">

All files and directories have been removed.

I hope this help you overcome your challenges of deleting multiple files and folders at the same time! If you have any questions or comments let me know!

 

About This Tutorial
Author: Pablo Varando
Skill Level: Intermediate 
 
 
 
Platforms Tested: CF4,CF5,CFMX,CFMX7,BlueDragon
Total Views: 52,958
Submission Date: September 07, 2005
Last Update Date: June 05, 2009
All Tutorials By This Autor: 47
Discuss This Tutorial
  • I have seen many websites which does not enable the Tag. In shared hosting envoronment, What to do in that instance, Otherwise the tutorial is ok

  • Thats for this, I was using the recursive CF method of looping every file and directory and deleting them one at a time. I have shortened your method to remove the batch file completely. I use the following cfexecute name='CMD' arguments='/C RMDIR /S /Q "C:\myFolder\"' timeout='60'

  • Gonna use this myself, very handy and a different slant on what should be standard in CF but it's not... Might be worth pointing out though that this will only work on a box you own as no host in their right mind would offer CFEXECUTE on a shared box! ;-)

  • Man I love this tutorial so much I had to compliment it twice. Keep the PSP for yourself...you deserve it for this one. Coldfusion doesn't make it easy, but you sure do.

  • I'm glad you dedicated a tutorial to my recent question on the forum. The tutorial is very detail exactly what I was looking for, it really explains everything well. You might want to warn users of this code to make sure they really want to delete the directory, because once its gone...its gone. Thanks again, Caliber NEW LOGO CONTEST!

Advertisement

Sponsored By...
Powered By...