Back to Top
The nuts and bolts for
Creative web development

Delete Dynamically Named Folder

Question:


How can I delete a dynamically named folder with the record it belongs to? 
I created a member site that uses Pure ASP Upload to create a dynamically named folder for each member's images when they first upload. Sometimes we need to delete a member and want to delete the folder as well. Not all members upload images so not all members have folders. How can I delete the folders with the deleted record and insure the page doesn't crash when there is no folder?

Answer:


1. Create a hidden form field with the foldername as the value.

2. Insert the following code:

<%
' Delete folder with record 
If (CStr(Request("MM_delete")) <> "" And CStr(Request("MM_recordId")) <> "") Then
Set fso = CreateObject("Scripting.FileSystemObject")
folderpath = Server.MapPath("..\Uploads") 'change to the name the uploads root folder.
folderpath  =  folderpath  & "\" & (request.form("hiddenfield")) 'change to the name of your hidden field.
If fso.FolderExists(folderpath) Then
Set folderObject = fso.GetFolder(( folderpath) & "\")
folderObject.Delete
end if
Set folderObject = Nothing
Set fso = Nothing
End if
%>

James Threadgill

James ThreadgillJames Threadgill has authored numerous tutorials on ASP and ASP.NET web development, published on such sites as the Dynamic Zones and MSDN Accademic Alliance. He co-authored the Sam's book Dreamweaver MX: ASP.NET Web Development.

James first began computer programming in 1995 while attending Alvin Community College. He completed a certificate of computer science program and an Associate of Arts degree at Alvin before going on to the University of Houston-Clear Lake where he was awarded a Bachelor of Science and a Master of Arts.

James publishes fiction, poetry, and visual arts under the name Wayne James. His fiction first appeared in Raconteur in 1995 and since has been published numerous times: in Hadrosaur Tales 5 and 7, Bayousphere, and in the Write Gallery e-zine. His poetry first appeared in the small press magazine Lucidity in 1996 and has been published numerous times since. His collection of fiction and poetry, When Only the Moon Rages, was released in 2000. Most recently his work appeared in Tales of the Talisman winter 2010 and spring 2011 issues. James currently attends graduate school at the University of Houston and owns and operates small web design and internet marketing firm, WWWeb Concepts, with his wife, Karen, in Houston, TX USA.

See All Postings From James Threadgill >>

Share this Article

Comments

dynamic folder name from recordset

James Thank you for your code.

A quick question. my dir name is been called from a database <%=(Recseesee.Fields.Item("imagepku").Value)%>

my directory i want deleted is inside "Uploadimages"

you folderpath = Server.MapPath("..\Uploadimages") 'change to the name the uploads root folder.

When I use your code at present it deletes the "Uploadimages" folder which is something i dont want. how can I add the name the uploads root folder when my value is dynamic.

I have tried
you folderpath = Server.MapPath("..\Uploadimages") & (Recseesee.Fields.Item("imagepku").Value)

only to get an error.

regards

k

RE: RE: no error no work

I've alreayd found the problem.its works nice!

RE: no error no work

If you are not getting an error the code is more than likely correct and I see no problems. Check your permissions.

no error no work

ihave change it like his but it doesnt wors!no error no work!

<%
' Delete folder with record 
If (CStr(Request("MM_delete")) <> "" And CStr(Request("MM_recordId")) <> "") Then
Set fso = CreateObject("Scripting.FileSystemObject")
folderpath = Server.MapPath("userpics") 'change to the name the uploads root folder.
folderpath  =  folderpath  & "\" & (request.form("del_folder")) 'change to the name of your hidden field.
If fso.FolderExists(folderpath) Then
Set folderObject = fso.GetFolder(( folderpath) & "\")
folderObject.Delete
end if
Set folderObject = Nothing
Set fso = Nothing
End if
%>

mrx3m@hotmail.com for your suggestions!thnx