Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting directories including subdirectories.

Status
Not open for further replies.

sknyppy

Programmer
May 14, 1999
137
US
I have an interface that allows an authorized user to select
a directory from his/her profile and delete it.
The problem I have is that if the directory has subdirectories in it I can't delete it if it contains files. Also if the subdirectory contains additional subdirectories and so on.. I need to delete everything
below the selected directory. I 'think' I was on the correct track using the code below but my mind has wandered....
Thanks,
Dave

<CFDIRECTORY action=&quot;LIST&quot; name=&quot;FilesToRemove&quot; directory=&quot;#REQUEST.UploadLocation#\#ListGetAt (FORM.DirectoryID,1)#&quot;>

<CFOUTPUT QUERY=&quot;FilesToRemove&quot;>
<CFIF FilesToRemove.Type is &quot;Dir&quot;>
<CFDIRECTORY action=&quot;LIST&quot;
name=&quot;SubDirFilesToRemove&quot;
directory=&quot;#REQUEST.UploadLocation#\#ListGetAt(FORM.DirectoryID,1)#\#SubDirFilesToRemove.Name#&quot;>
<CFOUTPUT QUERY=&quot;SubDirFilesToRemove&quot;>
<CFIF Len(SubDirFilesToRemove.Name) GT 2>
<CFFILE action=&quot;DELETE&quot;
file=&quot;#REQUEST.UploadLocation#\#ListGetAt(FORM.DirectoryID,1)#\#SubDirFilesToRemove.Name#&quot;>
</CFIF>
</CFOUTPUT>
<CFDIRECTORY action=&quot;DELETE&quot;
directory=&quot;#REQUEST.UploadLocation#\#ListGetAt(FORM.DirectoryID,1)#\#SubDirFilesToRemove.Name#&quot;>
<CFELSEIF FilesToRemove.Type is &quot;File&quot;>
<CFIF Len(FilesToRemove.Name) GT 2>
<CFFILE action=&quot;DELETE&quot;
file=&quot;#REQUEST.UploadLocation#\#ListGetAt(FORM.DirectoryID,1)#\#FilesToRemove.Name#&quot;>
</CFIF>
</CFIF>
</CFOUTPUT>
<CFDIRECTORY action=&quot;DELETE&quot; directory=&quot;#REQUEST.UploadLocation#\#ListGetAt(FORM.DirectoryID,1)#&quot;
 
Hey Dave,

This question comes up a lot as it involved directory recursion and the CF tags don't do this natively. I've written code that recursively called itself to process a variable number of sub-directories and it's not a fun thing to do. In your case, I think I would use the <cfexecute> tag to invoke the DOS &quot;rmDir&quot; command. You could do something like this to quickly delete a directory and all sub-directories.

<CFEXECUTE
NAME=&quot;rmDir.com&quot;
ARGUMENTS=&quot;/s /q directoryToDelete&quot;
>

Hope this helps,
GJ
 
I tried using that and it doesn't work but no error is returned. Is my code syntax incorrect?

<CFEXECUTE NAME=&quot;C:\WINNT\system32\CMD.EXE&quot;
ARGUMENTS=&quot;RMDIR /S /Q D:\Inet\</CFEXECUTE>

Thanks,
Dave
 
Hey Dave,

Did you try it as I have it listed? I think you want to execute &quot;rmdir.com&quot; instead of &quot;command.com&quot;. I haven't done this in a while so I may have to setup a test but I think it should work (with some possible tweaking) if you use &quot;rmdir.com&quot;.

GJ
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top