Bookmark and Share

ASP File Operations

Try the demo or view the source code.

ASP provides access to the web server's file system by defining several objects representing drives, folders, files, etc. This article demonstrates the coding basics needed to perform the most common file operations: creating, deleting, reading from and writing to a file.

Security Note

File access permissions will apply to the script execution, so the user account which it runs under (such as an anonymous ID set up for web users) must have suitable permissions to the files and directories accessed.

Other access permissions may be applied as well, such on Microsoft IIS web servers. Check your system documentation.

The FileSystemObject Object

To access a file, you first create a FileSystemObject which provides the methods to access, create, delete or open a file for input or output. All of these methods require you to specify the physical path for the file.

<% set fs = CreateObject("Scripting.FileSystemObject")
   fs.DeleteFile("C:\temp\10312000.txt") %>

To find the path of a file on the local web site, the built-in Server.MapPath() function can be used to translate a URL to a physical path.

<% path = Server.MapPath("/webtmp/abc123.txt") %>

Which might return "C:\Inetpub\wwwroot\webtmp\abc123.txt" for example.

Another useful method provided by this object is FileExists() which returns a value of True or False. This is good for checking a file's status before attempting to create, delete or open it.

Creating a Text File

The CreateTextFile() method is used to create a new text file. Its parameters are described below.

CreateTextFile(filename [, overwrite [, unicode]])
Parameter Description Required Default
filename String specifying the path and filename. Yes n/a
overwrite Boolean flag. If True, any existing file with the given name is overwritten. No True
unicode Boolean flag. If True, the file is created as Unicode text file, otherwise ASCII. No False

The method returns a TextStream object that can then be used for outputting data, after which the file should be closed to ensure all output operations are completed. Here's an example.

<% set fs = CreateObject("Scripting.FileSystemObject")
   set file = fs.CreateTextFile("C:\temp\10312000.txt", true, false)
   file.WriteLine("Hello World!")
   file.Close %>

If overwrite is True, any existing file is destroyed and replaced with the new file. If you don't care about overwriting existing files, set it to True (the default). Otherwise, it's a good idea to use FileExists() to check a file's status first, so you can handle the situation in your code instead of having the method generate an error.

Deleting a File

The DeleteFile() method allows you to delete one or more files.

DeleteFile(filespec [, force])
Parameter Description Required Default
filespec String specifying the path and filename. Wildcards can be used on the filename. Yes n/a
force Boolean flag. If True, forces files marked 'read-only' to be deleted. No False

Note that the path can contain wildcard characters, such as "temp*.txt". Here's an example that deletes a single file.

<% set fs = CreateObject("Scripting.FileSystemObject")
   fs.DeleteFile("C:\temp\10312000.txt", true) %>

Again, it's a good idea to use FileExists() before attempting to delete a given file, to prevent any errors.

Analogous methods are provided to create and delete folders as well. Check any VBScript reference for details.

Reading and Writing Text Files

The OpenTextFile() method opens a file for input or output depending on the mode chosen.

OpenTextFile(filename[, iomode[, create[, format]]])
Parameter Description Required Default
filename String specifying the path and filename. Yes n/a
iomode One of three constants: ForReading (1), ForWriting (2) or ForAppending (8). No ForReading
create Boolean flag. If True, the file will be created if it doesn't already exist. No False
format Tristate flag, one of: TristateUseDefault (-2), TristateTrue (-1) or TristateFalse (0). No TristateFalse

Opening a file for writing causes the current contents to be overwritten. Using the append mode preserves the current file contents, new data is written starting at the end of the file.

If true, the create option cause the file to be created if it doesn't already exist. If it is created, format specifies whether Unicode (TristateTrue) or ASCII (TristateFalse) is used. A value of TristateUseDefault causes it to use whatever the system default is.