Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete a file when session dies

Status
Not open for further replies.

Karl Blessing

Programmer
Feb 25, 2000
2,936
US
Two things I Want to do.

for starters, I Want to make a random filename, so that it may be used in the process of storing a file (assuming the same filename will be used during the whole session) but then , when the session ends, I would like that file to be deleted.

Anyone know the key points in doing this?
Karl Blessing aka kb244{fastHACK}
kblogo.jpg
 
this is what I have so far, it's creating the name, it's saving it just fine, but the Session_OnEnd isnt working, I even made an abort_session.asp that calls Session.abandon to check.

Code:
Sub Session_OnStart
	for j = 1 to 10
		Randomize
		RndNumber = Int(Rnd * 25) + 65
		RndName = RndName & chr(RndNumber)
	next
	Session("custom_thematic") = RndName
End Sub

Sub Session_OnEnd
	Set fs = Server.CreateObject("Scripting.FileSystemObject")
	if fs.FileExists(Request.ServerVariables("APPL_PHYSICAL_PATH") & "\images\bmp\custom\" & Session("custom_thematic") & ".bmp") then
		fs.DeleteFile Request.ServerVariables("APPL_PHYSICAL_PATH") & "\images\bmp\custom\" & Session("custom_thematic") & ".bmp", true
	end if
	Set fs = nothing
End Sub
Karl Blessing aka kb244{fastHACK}
kblogo.jpg
 
this is my abort_session.asp

Code:
<%@ Language=VBScript %>
<%
	Set fs = Server.CreateObject(&quot;Scripting.FileSystemObject&quot;)
	if fs.FileExists(Request.ServerVariables(&quot;APPL_PHYSICAL_PATH&quot;) & &quot;\images\bmp\custom\&quot; & Session(&quot;custom_thematic&quot;) & &quot;.bmp&quot;) then
		Response.Write &quot;The file exists&quot;
	else
		Response.Write &quot;The File does not exist&quot; & vbcrlf
	end if

Session.Abandon
Response.Write Request.ServerVariables(&quot;APPL_PHYSICAL_PATH&quot;) & &quot;\images\bmp\custom\&quot; & Session(&quot;custom_thematic&quot;) & &quot;.bmp&quot;

	if fs.FileExists(Request.ServerVariables(&quot;APPL_PHYSICAL_PATH&quot;) & &quot;\images\bmp\custom\&quot; & Session(&quot;custom_thematic&quot;) & &quot;.bmp&quot;) then
		Response.Write &quot;The file exists&quot;
	else
		Response.Write &quot;The File does not exist&quot; & vbcrlf
	end if
Set fs = nothing
%>

this is the output I get back from it when I run it.

The file exists
D:\Inetpub\The file exists Karl Blessing aka kb244{fastHACK}
kblogo.jpg
 
Hrm it would be nice if I Can get this working Karl Blessing aka kb244{fastHACK}
kblogo.jpg
 
The reason that you can't get FSO to delete your file is because that object is not available to ASP from inside the _OnEnd event (both Session and Application).

cut and paste from
The only objects that are available from within the Session_OnEnd event are
Application, Session, and Server.

hope this helps
leo
 
hrm, so I'm just wondering, how can I possibly clean things up then? Karl Blessing aka kb244{fastHACK}
kblogo.jpg
 
whoa wait, if thats true then how did I manage to even get back a True/False response on FileExist() function , furthermore how come no error was generated when I attempted to use it, and why did FSO continue to work when I came back from the .abandon function. Karl Blessing aka kb244{fastHACK}
kblogo.jpg
 
Well, could you not write and compile a custom object, which you could then use to do what you wanted???

ie.
set file = server.createObject(&quot;KB244.makeFile&quot;)

which could then give you the options you desire??

If you have access to the server, then the server would give you access to the file, yes?
penny.gif
penny.gif
 
Session.Abandon doesn't truly call until the calling script is done executing, so that's why you're still getting back the Session(&quot;custom_thematic&quot;) variable to appear, so the fileexists method is going to return a value.

taken from The Abandon method terminates a user session, destroys all the objects in the current Session object, and releases its resources. However, this deletion will not occur until all of the script is processed for the current page. When the session ends, the OnEnd event handler is called. By default, even if you do not call Abandon, the Session object is terminated after twenty minutes of idle time.

hope this helps
leo
 
Hrm , that kind of makes it hard for me to do anything in order to clean up a persion's files left behind from a session. Karl Blessing aka kb244{fastHACK}
kblogo.jpg
 
Something you might want to look into is creating an FSO object at the session level. You might get access to the object at the session level, but this opens up a whole new can of worms for you (threading model, performance degradation, FSO objects open for 10 minutes, etc.) If this fits your model, then that might be a solution.

the only other alternatives I can think of is a custom DLL to oversee the process, or using wsh and Windows Scheduler to delete files as necessary.

sorry i couldn't be of more help
leo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top