File Handling with FileSystemObject in Classic ASP

The Windows Script Runtime FileSystemObject can let Classic ASP inspect folders and work with text files when the server permits it. File operations run with the IIS application identity, so permissions and path control are part of the application security model.

Security: do not pass visitor-controlled paths directly to GetFile, OpenTextFile, DeleteFile, or similar methods. Keep paths server-controlled, grant only the minimum filesystem permissions required, and avoid writable application-code directories.

Create a FileSystemObject

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

Check whether a folder exists

If objFSO.FolderExists(Server.MapPath("db")) Then
    Response.Write "Folder exists"
Else
    Response.Write "Folder does not exist"
End If

Check whether a file exists

Dim FSOobj, FilePath
FilePath = Server.MapPath("text.txt")
Set FSOobj = Server.CreateObject("Scripting.FileSystemObject")
If FSOobj.FileExists(FilePath) Then
    Response.Write "File exists"
Else
    Response.Write "File does not exist"
End If
Set FSOobj = Nothing

List files in a folder

Dim objFSO, objFile, objFolder
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(Server.MapPath("/t/my_folder"))

For Each objFile In objFolder.Files
    Response.Write objFile.Name & "<br>"
Next

Set objFolder = Nothing
Set objFSO = Nothing

Show each file's last-modified time

For Each objFile In objFolder.Files
    Response.Write objFile.Name & " : "
    Response.Write objFile.DateLastModified & "<br>"
Next

Get the current ASP script name

Dim myArray, fileName
fileName = Request.ServerVariables("SCRIPT_NAME")

For a request such as:

/my_file/test.asp

split the path and read the final array element:

Dim myArray, fullName, fileName
Response.Write Request.ServerVariables("SCRIPT_NAME") & "<br>"
fullName = Request.ServerVariables("SCRIPT_NAME")
myArray = Split(fullName, "/")
fileName = myArray(UBound(myArray))
Response.Write fileName

Read text files

Read(n) reads up to the requested number of characters:

Response.Write OpenFileobj.Read(3)

Reading beyond available data can produce an end-of-file error:

Error Type:
Microsoft VBScript runtime (0x800A003E)

Input past end of file
writing.asp, line 28

Use AtEndOfStream when looping:

Do While Not OpenFileobj.AtEndOfStream

Read one line:

Response.Write OpenFileobj.ReadLine

Or read all remaining text:

Response.Write OpenFileobj.ReadAll

Complete read example

<%
Const ForReading = 1
Dim OpenFileobj, FSOobj, FilePath
FilePath = Server.MapPath("text.txt")
Set FSOobj = Server.CreateObject("Scripting.FileSystemObject")

If FSOobj.FileExists(FilePath) Then
    Set OpenFileobj = FSOobj.OpenTextFile(FilePath, ForReading)
    Do While Not OpenFileobj.AtEndOfStream
        Response.Write Server.HTMLEncode(OpenFileobj.ReadLine) & "<br>"
    Loop
    OpenFileobj.Close
    Set OpenFileobj = Nothing
Else
    Response.Write "File does not exist"
End If
Set FSOobj = Nothing
%>

The example HTML-encodes file contents before displaying them, which is safer when the file may contain characters that have special meaning in HTML.

Write to a text file

Set OpenFileobj = FSOobj.OpenTextFile(FilePath, ForWriting)

Writing requires filesystem permission. Otherwise a permission-denied error can occur:

Error Type:
Microsoft VBScript runtime (0x800A0046)
Permission denied
writing.asp, line 28

Write text without automatically adding a new line:

OpenFileobj.Write "my data here"

Write one line:

OpenFileobj.WriteLine "Welcome to Plus2net.com"

Add blank lines:

OpenFileobj.WriteBlankLines 4

Complete write example

<%
Const ForWriting = 2
Dim OpenFileobj, FSOobj, FilePath
FilePath = Server.MapPath("text.txt")
Set FSOobj = Server.CreateObject("Scripting.FileSystemObject")

If FSOobj.FileExists(FilePath) Then
    Set OpenFileobj = FSOobj.OpenTextFile(FilePath, ForWriting)
    OpenFileobj.Write "my data here"
    OpenFileobj.WriteLine " Welcome to Plus2net.com "
    OpenFileobj.Write "Learn ASP and VBScript"
    OpenFileobj.WriteBlankLines 4
    OpenFileobj.WriteLine "You can develop interactive web pages"
    OpenFileobj.Write "Thank you and visit again"
    OpenFileobj.Close
    Set OpenFileobj = Nothing
Else
    Response.Write "File does not exist"
End If
Set FSOobj = Nothing
%>
ForWriting replaces the existing file content. Use append mode when existing content must remain.

Append to a text file

Const ForAppending = 8
Set OpenFileobj = FSOobj.OpenTextFile(FilePath, ForAppending)

Append a submitted value

<form method="post" action="">
<input type="email" name="email" required>
<input type="submit" value="Submit">
</form>
<%
Dim email
email = Trim(Request.Form("email"))
If Len(email) > 2 Then
    Const ForAppending = 8
    Dim OpenFileobj, FSOobj, FilePath
    FilePath = Server.MapPath("text.txt")
    Set FSOobj = Server.CreateObject("Scripting.FileSystemObject")
    If FSOobj.FileExists(FilePath) Then
        Set OpenFileobj = FSOobj.OpenTextFile(FilePath, ForAppending)
        OpenFileobj.WriteLine email
        OpenFileobj.Close
        Set OpenFileobj = Nothing
    Else
        Response.Write "File does not exist"
    End If
    Set FSOobj = Nothing
End If
%>

This remains a learning example. Validate expected data before storing it, and do not use a text file as a substitute for an appropriate database when concurrent writes or durable structured data are required.

Read file metadata

File size

<%
Dim FSOobj, FilePath, details
FilePath = Server.MapPath("text.txt")
Set FSOobj = Server.CreateObject("Scripting.FileSystemObject")
If FSOobj.FileExists(FilePath) Then
    Set details = FSOobj.GetFile(FilePath)
    Response.Write "File Size = " & details.Size & " Bytes"
    Set details = Nothing
Else
    Response.Write "File does not exist"
End If
Set FSOobj = Nothing
%>

Full path

<%
Dim FSOobj, FilePath, details
FilePath = Server.MapPath("text.txt")
Set FSOobj = Server.CreateObject("Scripting.FileSystemObject")
Set details = FSOobj.GetFile(FilePath)
Response.Write "File Path = " & Server.HTMLEncode(details.Path)
Set details = Nothing
Set FSOobj = Nothing
%>

Include reusable ASP files

Use a file-relative include:

<!--#include file="menu.asp" -->

Include from a child folder:

<!--#include file="my_dir/menu.asp" -->

Or use a virtual path from the web application:

<!--#include virtual="/connection.asp" -->

Includes are processed on the server before the ASP page runs; keep included files under your application's control.

Created and last-modified dates

Dim objFSO, objFile, fileName, fullName, myArray
fullName = Request.ServerVariables("SCRIPT_NAME")
myArray = Split(fullName, "/")
fileName = myArray(UBound(myArray))

Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(Server.MapPath(fileName))
Response.Write "Created date of file " & objFile.Name & " = " & objFile.DateCreated
Response.Write "<br>Last modified date of file " & objFile.Name & " = " & objFile.DateLastModified

Simple text-file hit counter

<%
Const ForReading = 1
Const ForWriting = 2
Dim OpenFileobj, FSOobj, FilePath, count
FilePath = Server.MapPath("count.txt")
Set FSOobj = Server.CreateObject("Scripting.FileSystemObject")
If FSOobj.FileExists(FilePath) Then
    Set OpenFileobj = FSOobj.OpenTextFile(FilePath, ForReading)
    count = CLng(OpenFileobj.ReadLine)
    OpenFileobj.Close
    Set OpenFileobj = Nothing

    count = count + 1
    Set OpenFileobj = FSOobj.OpenTextFile(FilePath, ForWriting)
    OpenFileobj.Write count
    OpenFileobj.Close
    Set OpenFileobj = Nothing
    Response.Write count
Else
    Response.Write "File does not exist"
End If
Set FSOobj = Nothing
%>
This counter demonstrates reading and writing a value, but file-based counters can lose updates when multiple requests write concurrently. Use analytics or a transactional data store for production counting.

Get the drive

<%
Dim FSOobj, FilePath, details
FilePath = Server.MapPath("text.txt")
Set FSOobj = Server.CreateObject("Scripting.FileSystemObject")
Set details = FSOobj.GetFile(FilePath)
Response.Write "Drive Name = " & details.Drive
Set details = Nothing
Set FSOobj = Nothing
%>

Delete a file

Dim FSOobj, FilePath
FilePath = Server.MapPath("images/myfile.asp")
Set FSOobj = Server.CreateObject("Scripting.FileSystemObject")
If FSOobj.FileExists(FilePath) Then
    FSOobj.DeleteFile FilePath, True
    Response.Write "File deleted"
Else
    Response.Write "File does not exist"
End If
Set FSOobj = Nothing
Destructive operation: never build the deletion path from untrusted request input. Confirm the exact server-controlled path and required permissions first.

File name, extension and base name

Dim FSOobj, FilePath
FilePath = Server.MapPath("images/all11.css")
Set FSOobj = Server.CreateObject("Scripting.FileSystemObject")
If FSOobj.FileExists(FilePath) Then
    Response.Write "File exists"
    Response.Write "<br>File Extension Name: " & FSOobj.GetExtensionName(FilePath)
    Response.Write "<br>File Name: " & FSOobj.GetFileName(FilePath)
    Response.Write "<br>File Base Name: " & FSOobj.GetBaseName(FilePath)
Else
    Response.Write "File does not exist"
End If
Set FSOobj = Nothing

ASP Home






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer