;;;How can I manipulate the exit or close button in the internet explorer
Not sure what you are asking here. In short, you can not.
;;;how can I execute it when they hit the [x] and close button
The only way I am aware of is to place an event inside the body tag. For descriptions and what they do go to http://www.w3schools.com/tags/ref_eventattributes.asp
If I may ask why do you need to write a temporary file and then delete it?
Wind is your friend