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:
<%
set fs = CreateObject("Scripting.FileSystemObject")
%>
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.
<%
set fs = CreateObject("Scripting.FileSystemObject")
path = Server.MapPath("/myweb/intro.html")
%>
Which might return "C:\Inetpub\wwwroot\myweb\intro.html" 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:
<%
set fs = CreateObject("Scripting.FileSystemObject")
if fs.Filexists(Server.MapPath("/myweb/intro.html")) then
response.write "File exists!"
else
response.write "File does not exist!"
end if
%>
To delete a file you must use its physical path/filename, here the Server.MapPath function is used to make things easier:
<%
set fs = CreateObject("Scripting.FileSystemObject")
fs.DeleteFile(Server.MapPath("/myweb/intro.html"))
%>
The CreateTextFile() method is used to create a new text file. It has a couple of optional parameters:
CreateTextFile(filename, overwrite, unicode)
overwrite - Boolean flag. If True, any existing file with the given name is overwritten.
unicode - Boolean flag. If True, the file is created as Unicode text file, otherwise ASCII.
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:
<%
set fs = CreateObject("Scripting.FileSystemObject")
set file = fs.CreateTextFile("C:\Inetpub\wwwroot\myweb\textfile.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.
The OpenTextFile() method opens a file for input or output depending on the mode chosen. This method also has parameters:
OpenTextFile(filename, iomode, create, format)
iomode - Constant: 1 (ForReading) , 2 (ForWriting) , or 8 (ForAppending)
create - Boolean flag: If True, the file will be created if it doesn't already exist.
format - Constant: -2 (TristateUseDefault), -1 (TristateTrue), or 0 (TristateFalse)
The format parameter specifies what format the file will be written in:
TristateUseDefault = Use the system default setting
TristateTrue = Unicode
TristateFalse = ASCII
This example opens a text file for reading:
<%
Const ForReading = 1
set fs = CreateObject("Scripting.FileSystemObject")
set file = fs.OpenTextFile(Server.MapPath("/myweb/textfile.txt"), ForReading)
'Just close it again
file.Close()
%>
To access the contents of a file you can use the TextStream object. This object contains several methods:
Read(n) - Returns the specified number of characters.
ReadLine() - Returns all characters up to but not including the next newline character.
ReadAll() - Returns all characters in the file.
Skip(n) - Skips over the specified number of characters.
SkipLine() - Skips all characters up to and including the next newline character.
Write(str) - Writes the given text string to the file.
WriteLine(str) - Writes the given text string to the file followed by a newline character.
WriteBlankLines(n) - Writes the given number of newline characters to the file.
Close() - Closes the file.
Reading a file and displaying its contents onscreen:
<%
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
'Open and read a text file.
set fs = CreateObject("Scripting.FileSystemObject")
set file = fs.OpenTextFile(path, ForReading)
do while not file.AtEndOfStream
Response.Write(file.ReadLine & vbCrLf)
loop
file.Close()
%>
Overwriting a text file:
<%
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
'Overwrite a text file.
set fs = CreateObject("Scripting.FileSystemObject")
set file = fs.OpenTextFile(path, ForWriting)
file.WriteLine("Overwriting file with this text.")
file.Close()
%>
Appending text to a file:
<%
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
'Append to an existing text file.
set fs = CreateObject("Scripting.FileSystemObject")
set file = fs.OpenTextFile(path, ForAppending)
file.WriteLine("This line is added to the file.")
file.Close()
%>