ASP Tutorials - Delete, move or copy files using the File System Object

The File System Object has methods to delete, move and copy files. They can be used with wild cards to process multiple files in a single command.

DeleteFile

The following example uses the DeleteFile method to delete a single file. It requires a physical path to the file so we have used Server.MapPath to find the physical path of a file, which is in the same directory as the script.

Set FSO = Server.CreateObject("Scripting.FileSystemObject")
FSO.DeleteFile Server.MapPath("file.ext")

The DeleteFile method does not have a return value and if it fails it will generate an internal server error. If the file does not exist it will have the message "File not found" with the error code 0x800A0035.

Multiple files can be deleted using wildcards. The following example deletes all text files from the directory containing the script.

Set FSO = Server.CreateObject("Scripting.FileSystemObject")
FSO.DeleteFile Server.MapPath(".") & "\*.txt"

Notice that DeleteFile can accept wildcards, but Server.MapPath cannot, so the physical path of the files to be deleted must be built by string addition. This code will also generate an error if no files match the description.

The star character is the most commonly used wildcard because matches any string of characters. The question mark can be used to replace a single character. The following example deletes all text files that have a name that is one character long.

Set FSO = Server.CreateObject("Scripting.FileSystemObject")
FSO.DeleteFile Server.MapPath(".") & "\?.txt"

MoveFile

The MoveFile method takes two parameters, the source and destination, which are physical paths. The destination file can be given a different name from the original so that it can be renamed as well as moved. This command will also give an error if the source files or the destination folder do not exist.

The following example moves a file from one directory to another while keeping the name unchanged.

Set FSO = Server.CreateObject("Scripting.FileSystemObject")
FSO.MoveFile "c:\source\a.txt", "c:\destintation\a.txt"

CopyFile

The CopyFile method is similar to the MoveFile method described above, except that the source file is not deleted. The following example copies a file from one directory to another while keeping the name unchanged.

Set FSO = Server.CreateObject("Scripting.FileSystemObject")
FSO.CopyFile "c:\source\a.txt", "c:\destintation\a.txt"

If a file by that name already exists in the destination folder, it will be overwritten. CopyFile has an optional third parameter which can be set to false to prevent overwriting. for example:

Set FSO = Server.CreateObject("Scripting.FileSystemObject")
FSO.CopyFile "c:\source\a.txt", "c:\destintation\a.txt", false

If the file already exists in the destination folder this code will generate an error saying "File already exists" with the code 0x800A003A.

Click here for more on finding if a file exists.

Click here for more on using Server.MapPath to find physical paths.

Notes:

The sample code in these tutorials is written for VBScript in classic ASP unless otherwise stated. The code samples can be freely copied.