Deleting a File with ASP?

K

Keith

Is it possible to use ASP to delete a file on the web server?

I have a form that allows users to submit files to my site, but I now want
them to be able to delete their files too if possible.

I know the location and filename of the files (this is taken from a
recordset).

I am not 100% adept in ASP (I am using Dreamweaver to make my site) so an
example would be good please.

Thanks
 
C

Christopher Brandsdal

Here is a code example:

Dim objFSO
Dim objDelFile
Dim strFilePath
strFilePath= "filepath"

on error resume next
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objDelFile = objFSO.GetFile(Server.MapPath(strFilePath))
objDelFile.Delete

Set objDelFile = nothing
Set objFSO = nothing
 
K

Keith

Thanks

How can I make it use the following as the filepath/name? I am not familiar
enough to modify the code sample.

"../../../sections/policies/files/policies/" &
RS_Remove_Policy.Fields.Item("Policy_Category") & "/" &
RS_Remove_Policy.Fields.Item("Policy_File_Name")

When I use this in a Response.Write it displays the correct path and
filename so I know this bit works, just not sure how to put it in the code
sample - I always get confused with " and '
 
C

Christopher Brandsdal

Try this..
If it doesen't work, then just change the strFilePath....
Just to test that the string is valid, insert the followingtag, run the
code, and se if the result is the same as the actual path:

<%=Server.MapPath("../../../sections/policies/files/policies/" &
RS_Remove_Policy.Fields.Item("Policy_Category") & "/" &
RS_Remove_Policy.Fields.Item("Policy_File_Name"))%>


Here is the code withe the string you provided! Try this:


Dim objFSO
Dim objDelFile
Dim strFilePath

strFilePath= "../../../sections/policies/files/policies/" &
RS_Remove_Policy.Fields.Item("Policy_Category") & "/" &
RS_Remove_Policy.Fields.Item("Policy_File_Name")
on error resume next
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objDelFile = objFSO.GetFile(Server.MapPath(strFilePath))
objDelFile.Delete

Set objDelFile = nothing
Set objFSO = nothing
 
T

Tim Slattery

Keith said:
Is it possible to use ASP to delete a file on the web server?

I have a form that allows users to submit files to my site, but I now want
them to be able to delete their files too if possible.

I know the location and filename of the files (this is taken from a
recordset).

Not totally clear just what you're trying to do. You can use the
FileSystemObject to delete files on the server. You cannot delete
files on the client side.
 
A

Aaron [SQL Server MVP]

By the way nice servername Aaron; MacGyver ;)

I have some much more interesting ones. I have dual workstations at work,
and at one point they were called cromagnon and neanderthal (one machine is
still neanderthal). I've had airwolf and papasmurf ... some of us always
have fun with our workstation and server names; others are pretty boring
(first initial last name).

A
 
K

Keith

Thank you

That worked fine.

Is it possible (and if so how) to check if when a file is deleted the folder
it is in would be empty, and if this is the case, delete the folder too?

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,416
Latest member
LionelQ387

Latest Threads

Top