![]() |
| How to Recursively Delete Files |
|
donblaylock
|
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 |
||||||||||||
|
|
|||||||||||||
|
steve
HostMySite Developer
|
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 |
||||||||||||
|
|
|||||||||||||
|
byron
Forum Admin
|
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
![]()
|
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 |
|
||
|



