Delete a file from Global.asa file

I have a webpage that gets a file and copies it to a temp directory on the server (at server.mappath(temp).  THe file is created using the sessionID as the filename.(98855322.pdf for example)  Once the session ends, I want to delete this file, but I'm having trouble doing it.  I don't have a problem creating the file and then opening it in my ASP code, but for some reason When I use the FSO in the sub session_OnEnd() in the global.asa file, nothing happens.  Could I get a code segment that works for sure that will delete the file in the OnEnd of the global.asa so I can see if my problem is perhaps somewhere else.  
This is my pseudo code, as I'm at home and don't have the code in front of me:

Sub Session_OnEnd()

declare vars
create FSO
set folder fso.getobject(folderpath)

set ofiles to folder.files
for each ofile in ofiles
 if instr(1,ofile.name, session.sessionID) > 0 then
     fso.deletefile(folder & ofile.name, true)
end if
next

end sub

I am checking the name of the file, and if it contains the sessionID, then I want to delete it.  This is the behavior I need, but please don't grade my code segment :)  

Thanks
daronAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
mgfranzConnect With a Mentor Commented:
How about using an onUnload function that runs your Server-side function;

<body onUnload="deleteIt.asp">

<% ' DeleteIt.asp

Dim fso, f1
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.GetFile("c:\temp\"&Session.SessionID&".pdf")
' Delete the files.
f1.Delete
%>

That's all you really need to do.



0
 
mgfranzCommented:
Good question... I had tried this once myself, never got it to work either...  My thinking was that once the global.asa file is executing the Session_onEnd(), it can't go back into the session and perform any duties on the session.  I would love to hear other experts comments regarding this.

I know Weesong and Silvers are good people to hit-up on global.asa problems.  Wee should be on later this evening and silvers probably in the morning, (one is in Malasia and the other in Russia)
0
 
daronAuthor Commented:
I always get good info from Weesiong!
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
daronAuthor Commented:
mgfranz,  did you find a work around for this behavior?  My purpose for this is when the user finishes viewing these pages that have been created using their sessionID, they need to be deleted from the server to keep from having a ton of files end up existing on the server.
0
 
manihopeverCommented:
I think you know,
A session OnEnd() will fire when we call Session.Abandon or
Session.Timeout is setted.

I you don't heared about the above then try at once.

So when it fires the subroutine it destroys all those sessions.  So don't try for getting SessionId in the OnEnd subroutine.

I accept mqfranz's comment.

thanks,
manie.v

0
 
hongjunCommented:
There's a bug in IIS and that's sometimes Session_OnEnd does not get fire up. That's really bad.

Moreover, you should not use Session.SessionID. It will get destroyed when OnEnd event is called.

You may use a cookie to store the SessionID and then use it to remove it.

hongjun
0
 
Michel SakrCommented:
hongjun is correct.. and mgfranz too.. about the experts :o))

Alas IIS4 and 5 as detected.. have a session timout bug, it is roughly called the "session on end bug" since it is reflected clearly here.. on IIS sessions time out randomly, or never timeout, despit the timeout set in IIS or in the script or the default timeout that is 20 minutes..  this is one of the main reasons why your code never works in the session on end since for that session IIS didn't timout yet.. also this is the main reason that prevent us from storing big portions of info or arrays or recordsets or connection objects in session objects.. since the server resources will be consumed and it will crash on a timely basis.. ok enough descriptions let's cure.. your best way to prevent this is to log the user's traffic in a database.. where you'll store their session id and a timestamp now the main point is on each request you update their corresponding record with the current timestamp to check that they're still browsing the site (on top of each page you can include a file that holds the script to update the timestamp) , now externally you can create an application that will scan/parse this log table and check for records that have a timestamp older than the session timeout.. if found the records will be deleted and their corresponding files.. you can do this also in session on start..

Session abandon will help only if used by catching the onunload event in the body.. you popup an asp page that will fire the session.abandon but alas this is not 100% efficace since the browser can be set not to browse in same context (guess it's called like this).. sessions won't be preserved..

Cookies won't help too since users might not return...

rgrds.. hope got things cleared out..
0
 
mgfranzCommented:
What I have used in the past is something like what silvers stated, I have a onUnload() fire requesting the user to "log out", this fires a script that clears all session parameters and empties the temp database table for the user.  Let me know if you need code, but it's pretty basic stuff...
0
 
daronAuthor Commented:
...And I was so excited earlier today for getting the session onend to work at home like I want :)   Well, time to re-think.  Here is where I am:  the user selects a choice on the asp page and a file is copied to a temp directory on the server.  This is necessary for a bunch of reasons I won't get into(user can't view it directly from it's location).  So the name of the file is anything I want to make it.  In my asp page, I am copying the file to the temp directory and then opening a new window that points to that file(so the file can be viewed automatically).  So at some point, These temporary files I have created need to be deleted so as to keep it from filling up the server.   I like the timestamp/db method since it is replicating the onend functionality, but I am under time constraints that may keep me from being able to do that(unless you know of a good sample to work with).  
I also tried your idea mgfranz, but my asp page is reloaded each time a user selects something and the onunload is firing each time.  I'd be content with this if I could work around that.  So I am working on it again tomorrow...

Thanks for the responses.
0
 
hongjunCommented:
Interesting.

hongjun
0
 
daronAuthor Commented:
Is there any way when using the OnUnload() method to determine if the page is being reloaded?  I have a form action that points back to this same page, so the page is reloaded 4 or 5 times before a file is copied.  
0
 
Michel SakrCommented:
OnUnload you should popup a new window.. XXX sites style.. :o)
BTW I forgot to mention that this bug is random.. on some web servers the session on end works like a charm.. on others it's just stubborn like a monkey.. but from home you might be using PWS..
0
 
cscheepersCommented:
Try this - it works for me, but only on a single file:

Sub DeleteFile(FileName)
  Set FileObject = Server.CreateObject("Scripting.FileSystemObject")
  FileObject.DeleteFile(FileName)
  Set FileObject = Nothing
End Sub

If this maybe helps, let me know, then we can figure a way to delete multiple files.
0
 
mgfranzCommented:
cscheepers, you need to read the comments and the question completely, the problem is not in the FSO.
0
 
daronAuthor Commented:
This is currently working fine.  I think it may be better to have the database setup and check it for the expired sessions, but for now this seems to be working every time.  My next test is to see how it does under a load.  That is what I was needing.  Thanks for all the comments.

Daron
0
All Courses

From novice to tech pro — start learning today.