Link to home
Start Free TrialLog in
Avatar of Stefan Motz
Stefan MotzFlag for United States of America

asked on

Delete files with Classic ASP

He Experts,
I store PDF, Word, JPG, etc. files in a folder. Each of these files is linked from a web page.
I would like the users be able to delete the selected file by clicking a link in the record.
e.g. how can a file be deleted without hard coding the image location in the code?
Can it be doen like
myAttachment Request.QueryString("Attachment")
then call the attachment in the code like
"\\myFiles\&myAttachment&"

I tried but it didn't work.
This is the code I'm using when the picture location is hard coded:

<%
dim fs
Set fs=Server.CreateObject("Scripting.FileSystemObject")
if fs.FileExists("\\myFiles\myPic.jpg") then
fs.DeleteFile("\\myFiles\myPic.jpg")
end if
set fs=nothing
%>





Thank you for your help.
Avatar of Big Monty
Big Monty
Flag of United States of America image

Are your files stored in a folder within your website? If so you can do something like:

<%
dim fs
Set fs=Server.CreateObject("Scripting.FileSystemObject")
MyFile = Server.mappath("/folder/myPic.jpg")
if fs.FileExists(MyFile)   then
fs.DeleteFile(myfile)
end if
set fs=nothing
%>
Avatar of Stefan Motz

ASKER

The name of the file is also stored in a database.
This is the link to the DeleteAttachment.asp
<a href="DeleteAttachment.asp?ID=<%=rs("ID")%>">DELETE</a>

The code of the DeleteAttachment.asp is the following:

<%

dim fs
Set fs=Server.CreateObject("Scripting.FileSystemObject")
if fs.FileExists("\\Attachments\EPS\OurJob.pdf") then
fs.DeleteFile("\\Attachments\EPS\OurJob.pdf")
end if
set fs=nothing


myId = Request.QueryString("ID")


StrSQL = "Update HR_Letters SET Attachment= '' Where ID="&myId&""
conn.Execute StrSQL


'Redirect to the UPDATED page
Response.Redirect "ViewLetter.asp?key="&myId&""

%>

When I hard code the picture location it is working fine. It resets the Attachment field of the record to empty and it deletes the file.

How can I take the value of the Attachment field from the database (in this case 'OurJob.pdf') without hard coding it?
ASKER CERTIFIED SOLUTION
Avatar of Big Monty
Big Monty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It gives me an error on line 7:

Microsoft OLE DB Provider for SQL Server error '80040e14'
Incorrect syntax near '='.

I tried to fix it but no success. The ID column is named ID in my database and it is INT data type
I find the records by ID,
The only change I made was renamint fileID to ID, so that it can find the record in my database
change

dim fileID : fileID = Request.QueryString("fileID")

to

dim fileID : fileID = Request.QueryString("ID")
Now it gives me this error:

Invalid column name 'fileID'.
Thank you very much; it's working now.
I really envy your brain Monty.
How can you always answer my question so quickly?
Thank you very much for your help.
when you've been doing this as long as I have, it comes pretty easy :)

you always seem to catch me at work when it's slow!