๐ ASP โ File System API: FileSystemObject
, File
, and Folder
๐งฒ Introduction โ What Is the File System API in Classic ASP?
Classic ASP offers powerful server-side file management through the File System Object (FSO). This API allows your ASP scripts to create, read, write, delete, and manage files and folders on the web server using objects like FileSystemObject
, File
, and Folder
.
๐ฏ In this guide, youโll learn:
- How to use
FileSystemObject
in Classic ASP - Create and manipulate files and folders
- Read/write text files on the server
- Best practices with security and path handling
๐งฑ Step 1 โ Create the FileSystemObject
<%
Dim fso
Set fso = Server.CreateObject("Scripting.FileSystemObject")
%>
๐ This object gives access to all file/folder methods.
๐ Folder Handling
โ Create a Folder
<%
If Not fso.FolderExists("C:\inetpub\wwwroot\data") Then
fso.CreateFolder("C:\inetpub\wwwroot\data")
End If
%>
โ Delete a Folder
<%
If fso.FolderExists("C:\data\old") Then
fso.DeleteFolder("C:\data\old")
End If
%>
๐ Get Folder Info
<%
Dim folder
Set folder = fso.GetFolder(Server.MapPath("data"))
Response.Write "Created: " & folder.DateCreated & "<br>"
Response.Write "Size: " & folder.Size & " bytes"
%>
๐ File Handling
โ Create or Overwrite a File
<%
Dim file
Set file = fso.CreateTextFile(Server.MapPath("data/info.txt"), True)
file.WriteLine("Hello from Classic ASP!")
file.Close
%>
๐ Append Text to a File
<%
Set file = fso.OpenTextFile(Server.MapPath("data/log.txt"), 8, True)
file.WriteLine "User accessed on: " & Now()
file.Close
%>
๐ข Modes:
1
= Read2
= Write (overwrite)8
= Append
๐งพ Read File Content
<%
Set file = fso.OpenTextFile(Server.MapPath("data/info.txt"), 1)
Do Until file.AtEndOfStream
Response.Write file.ReadLine & "<br>"
Loop
file.Close
%>
โ Delete a File
<%
If fso.FileExists(Server.MapPath("data/old.txt")) Then
fso.DeleteFile(Server.MapPath("data/old.txt"))
End If
%>
๐ Summary โ Recap & Next Steps
The File System API in ASP gives developers the ability to programmatically manage files and folders on the server. With FileSystemObject
, your scripts can handle logs, uploads, data caching, and more.
๐ Key Takeaways:
- Use
CreateObject("Scripting.FileSystemObject")
to manage files/folders - Use
OpenTextFile
for reading/writing - Always use
Server.MapPath()
for secure file access - Clean up with
.Close
andSet obj = Nothing
โ๏ธ Real-world Use Cases:
- Logging user activity to text files
- Reading CSV or config files
- File-based content management systems
โ FAQs โ Classic ASP File System Object
โ What is Server.MapPath()
used for with FSO?
โ
It converts a virtual path to a physical path, ensuring safe access to files within your ASP site.
โ Can I read and write binary files with FSO?
โ No. FSO works with text files. Use ADODB.Stream
for binary data (e.g., images, PDFs).
โ Is FSO safe to use on a web server?
โ
Yes, but restrict it to safe paths and validate all file names to avoid directory traversal attacks.
Share Now :