Reply to topic
How to Recursively Delete Files
donblaylock


Joined: 01 Apr 2004
Posts: 1
Location: Orlando, Florida USA
Reply with quote
Does anyone know how to delete files on the server recursively? I need to develop a script that can delete files older than a specified amount of time (say 1 month) and recursively traverse all folders from the current folder (where the script is located) down.

Thanks in advance,

Don Blaylock
CF Savant Shocked
steve
HostMySite Developer

Joined: 02 Mar 2004
Posts: 30
Reply with quote
Hello Don,

You can use the <CFDirectory> and <CFFile> tag to perform this task. the code may look something like this:

<!--- this will get the list of files and directories in the rootdir folder --->
<cfdirectory directory="C:/websites/rootdir" name="qry_folder" action="LIST">

<!--- store the directory names in an array --->
<cfset dir_array=arraynew(1)>
<cfset i=1>
<cfloop query="qry_folder">
<cfif qry_folder.type EQ "dir">
<cfset dir_array[i]=qry_folder.name>
<cfset i = i + 1>
<!--- if it is a file and has not been modofied in a month use CFFile to delete it --->
<cfleseif qry_folder.type EQ "file" and qry_folder.dateLastModified LT amonthago>
<cffile action="delete" file="C:/websites/rootdir/#qry_folder.name#">
</cfif>
</cfloop>

<!--- Now that you have the array of directory names you can do the same thing as above for each directory --->

You can find more information on these tags at these links:
http://livedocs.macromedia.com/coldfusion/6.1/htmldocs/tags-a20.htm#wp1097918

http://livedocs.macromedia.com/coldfusion/6.1/htmldocs/tags-p27.htm#wp1098395

Good Luck Exclamation
byron
Forum Admin

Joined: 07 Mar 2004
Posts: 160
Location: Newark, DE, USA
Reply with quote
An alternative, if you're CF service is running under an account with proper permissions is to use <cfexecute> and call rmdir /q /s as a command line.

<cfexecute name="rmdir.exe" arguments"/q /s c:\mydir" timeout=30 variable="result"/>
CFExecute
jamie
HostMySite Sales Rep
HostMySite Sales Rep

Joined: 19 Mar 2004
Posts: 766
Location: Newark, De
Reply with quote
Sidenote: Unfortunately due to security reasons, CFExecute is a disabled tag on all of our shared servers, and I doubt this setting can ever change.
How to Recursively Delete Files
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT  
Page 1 of 1  

  
  
 Reply to topic