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 will 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!
*Originally published at EasyCFM
Add to document.write(“Del.icio.us”) | Digg | Yahoo! My Web
Technorati:
EasyCFM.Com introduces at least three new tutorials each week, written by the webmaster (Pablo Varando) and also from individual people who post their own tutorials for visitors to learn from. For more information please visit: http://www.easycfm.com [EasyCFM is Hosted by Colony One On-Line – http://www.colony1.net]