Welcome to my ASP Code Website

DeleteFile - Deleting a File



You should be very cautious in using this function, because it allows an ASP script to permanently delete a file from your server's hard drive.

As you might imagine, there are actually many important applications for this. If you let clients load up ads onto your server, and then delete a client from your system, you can use DeleteFile to remove all those ad graphics. If you create online postcards, DeleteFile can let you sweep out the old ones each month.

Really, only two lines are necessary for this function:

Set ScriptObject = Server.CreateObject("Scripting.FileSystemObject")
ScriptObject.DeleteFile(filename)

You do of course have to set the "filename" variable to be a valid filename! :) You can either hard code this to be a certain file if you know what file will be deleted, you can take it as input from a form, or you can loop through a sequence of known values (if for example you named your postcards post1.html, post2.html, post3.html, etc).

Be VERY cautious that you never just let this function take in any file name at all. Hackers could then easily send in important file names and destroy your website.

ASP File Function List