Click to See Complete Forum and Search --> : How to delete uploaded files on server?


Theo_Rie
July 19th, 2001, 09:04 AM
Hi there!

I'm trying to put up a small download-area (Pw protected) on my Internet Server. I already found a way to upload (using aspsmartupload) and i now can display the files uploaded in the folder like that:

File Name Size Date

Presentation.ppt 12330 07/19/01
Resellers.doc 54008 07/18/01
...

Code extract:

-----

<body>
<%
Dim strPathInfo, strPhysicalPath, linkText1, linkText2
'strPathInfo = Request.ServerVariables("SCRIPT_NAME")
strPathInfo = "/site/content/associates/filetransfer/etc/download/!dummy.txt"
strPhysicalPath = Server.MapPath(strPathInfo)


dim objFSO, objFile, objFileItem, objFolder, objFolderContents
set objFSO = CreateObject("Scripting.FileSystemObject")

'response.write(strPathInfo)
'response.write("<BR>")
'response.write(strPhysicalPath)
'response.write("<BR>")
'response.write(Request.ServerVariables("PATH_TRANSLATED"))

'if you have a virtual folder set up you need to use the next line, not the second line below
set objFile = objFSO.GetFile(strPhysicalPath)
'set objFile = objFSO.GetFile(Request.ServerVariables("PATH_TRANSLATED"))
set objFolder = objFile.ParentFolder
set objFolderContents = objFolder.Files
%>
<table cellpadding=5>
<tr align=center>
<th align=left >Filename</th>
<th align=right >Size</th>
<th align=right >Date</th>
</tr>

<%
For Each objFileItem In objFolderContents
Response.write("<tr><td align=left>")
linkText1 = "../etc/download/" & objFileItem.Name
linkText2 = objFileItem.Name
'Response.write(linkText1)
if linkText2 <> "!dummy.txt" then
%>
<a href=<%=linkText1%>><%=linkText2%></a>
<%
Response.write("</td><td align=right>")
Response.write(objFileItem.Size)
Response.write("</td><td align=right>")
Response.write(objFileItem.DateLastModified)
Response.write("</td></tr>")
end if
Next

%>
</table>

</body>
<%
set objFSO = nothing
set objFile = nothing
set objFileItem = nothing
set objFolder = nothing
set objFolderContents = nothing
%>
</html>

-----

I am now looking for a way to delete certain files from this folder like this:

File Name Size Date

Presentation.ppt 12330 07/19/01 |x|
Resellers.doc 54008 07/18/01 |x|

The |x| means a button, checkbox,... whatever...
1. If the button is clicked a dialog should apear (Are you sure you want to delete the file 'Presentation.ppt' ? Yes / No

2. If Yes is clicked the file should be deleted and the page refreshed.

If No is clicked then nothing should happen.

How can i do this ?

Thanx a lot for your help!

lingasamyk
August 10th, 2001, 06:29 AM
Hope this may help you


<%
'while displaying the files store the same file name in an hidden text box control array
'check the files which you want to delete
'while relaunching the page for deletion collect the file names stored in the hidden box of the one whose check box is checked in an array


Dim FileSystemObj, FolderObj, FileObj, FileinFold, FC
Dim StrPath

StrPath = Server.mappath(Request.ServerVariables("PATH_INFO"))
'Using this function get the physical path of the server where the file exists
'From this form the path where you have uploaded the files
'then the following code will take care of deleting the files

Set FileSystemObj = server.CreateObject("SCRIPTING.FileSystemObject")
Set FolderObj = FileSystemObj.GetFolder(StrPath)
Set FileObj = FolderObj.Files

For Each FC in FileObj
' here you check wether the name of the current file exists in the array in which you stored the files to be deleted
' if the current file name exists in the array then allow the following code to execute or else skip the following code inside the for loop
FC.Delete
Next

Set FolderObj = Nothing
Set FileObj = Nothing
Set FileSystemObj = Nothing
%>