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.
GetFile, OpenTextFile, DeleteFile, or similar methods. Keep paths server-controlled, grant only the minimum filesystem permissions required, and avoid writable application-code directories.Dim objFSO
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(Server.MapPath("db")) Then
Response.Write "Folder exists"
Else
Response.Write "Folder does not exist"
End If
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
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
For Each objFile In objFolder.Files
Response.Write objFile.Name & " : "
Response.Write objFile.DateLastModified & "<br>"
Next
Dim myArray, fileName
fileName = Request.ServerVariables("SCRIPT_NAME")For a request such as:
/my_file/test.aspsplit 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(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 28Use AtEndOfStream when looping:
Do While Not OpenFileobj.AtEndOfStreamRead one line:
Response.Write OpenFileobj.ReadLineOr read all remaining text:
Response.Write OpenFileobj.ReadAll
<%
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.
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 28Write 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<%
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.Const ForAppending = 8
Set OpenFileobj = FSOobj.OpenTextFile(FilePath, ForAppending)<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.
<%
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
%><%
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
%>
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.
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
<%
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
%><%
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
%>
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 = NothingDim 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
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.