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.
romsomIT DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Big MontyWeb Ninja at largeCommented:
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
%>
romsomIT DeveloperAuthor Commented:
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?
Big MontyWeb Ninja at largeCommented:
since you're passing over the fileID in the query string, you need to do a lookup of the filename in the database to get the file name:

<%
'-- assuming you have a connection object created and open at this point
dim fileID : fileID = Request.QueryString("fileID")
if not isNumeric( fileID ) then fileID = -1       '-- make sure a valid parameter is being passed

sql = "select Attachment from HR_Letters where fileID = " & fileID
set rs = conn.Execute( sql )

if not rs.BOF and not rs.EOF then
   dim fs , fileName
   fileName = "\\Attachments\EPS\" & rs("Attachment")
   Set fs=Server.CreateObject("Scripting.FileSystemObject")
   if fs.FileExists( fileName ) then 
       fs.DeleteFile( fileName )
   end if
   set fs=nothing
end if

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&""

%>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

romsomIT DeveloperAuthor Commented:
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
Big MontyWeb Ninja at largeCommented:
change

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

to

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

Invalid column name 'fileID'.
romsomIT DeveloperAuthor Commented:
Thank you very much; it's working now.
romsomIT DeveloperAuthor Commented:
I really envy your brain Monty.
How can you always answer my question so quickly?
Thank you very much for your help.
Big MontyWeb Ninja at largeCommented:
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!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP

From novice to tech pro — start learning today.