Friday 12 October 2012

Delete, Move, Copy, File Exists in asp


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 FS = Server.CreateObject("Scripting.FileSystemObject")
FS.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 FS = Server.CreateObject("Scripting.FileSystemObject")
FS.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 FS = Server.CreateObject("Scripting.FileSystemObject")
FS.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 FS = Server.CreateObject("Scripting.FileSystemObject")
FS.MoveFile "D:\source\a.txt", "D:\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 FS = Server.CreateObject("Scripting.FileSystemObject")
FS.CopyFile "D:\source\a.txt", "D:\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 FS = Server.CreateObject("Scripting.FileSystemObject")
FS.CopyFile "D:\source\a.txt", "D:\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.

Check if a file exists using the File System Object

The following example checks if a file exists. It uses a physical path to the file and returns true or false.

Set FSO = Server.CreateObject("Scripting.FileSystemObject")
Response.Write FSO.FileExists(FilePath)

No comments:

Post a Comment