Your source for ASP-related frequently asked questions and answers! : ASP FAQS : FileSystemObject

Question: How can I delete a file from the Web server's file system?

Answer: The FileSystemObject, which provides access to the Web server's file system, contains a method called DeleteFile. As its name implies, DeleteFile is used to delete a file from the Web server's file system.

DeleteFile takes one required and one optional parameter. The first parameter, which is required, is the physical file name of the file to delete, like C:\MyFiles\foo.txt. The second parameter, which is option, is a Boolean value (True or False) and, if not specified, defaults to a value of False. This second parameter determines whether or not read-only files can be deleted. If you will be deleting read-only files, you must specify the second parameter as True.

So, if we wanted to delete the file C:\Foo\readme.txt, we could use the following code:

'Create the FileSystemObject
Dim objFSO
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")

'Delete the file
objFSO.DeleteFile "C:\Foo\readme.txt", False

Since we specify the second parameter as False, C:\Foo\readme.txt will not be deleted if it is marked as a read-only file.

For DeleteFile to work properly, the anonymous Internet user account must have Delete permissions for the file/directory to be deleted.

On a side note, if you want to convert a local URL path (like /WebDir1/FileName.html) into a physical file path (like C:\Inetpub\wwwroot\WebDir1\FileName.html) simply use the Server.MapPath function. For more information on Server.MapPath be sure to read: Using Server.MapPath.

-- View the technical docs for DeleteFile

FAQ posted by Scott Mitchell at 12/27/2000 9:04:41 PM to the FileSystemObject category. This FAQ has been viewed 63,028 times.

Copyright 2017 QuinStreet Inc. All Rights Reserved.
Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers