
Asp Filesystem in ASP
In Classic ASP, the FileSystemObject (FSO) is the main tool to work with the file system — it lets you manipulate drives, folders, and files on the server.
What is FileSystemObject?
A COM object provided by Microsoft scripting runtime.
Used to create, read, write, delete files and folders.
Allows checking file/folder existence, getting properties, etc.
Creating the FileSystemObject in ASP
<%Dim fsoSet fso = Server.CreateObject("Scripting.FileSystemObject")%>
Common FileSystemObject Methods & Properties
1. Check if File Exists
If fso.FileExists(Server.MapPath("data.txt")) Then Response.Write "File exists."Else Response.Write "File does not exist."End If
2. Check if Folder Exists
If fso.FolderExists(Server.MapPath("uploads")) Then Response.Write "Folder exists."Else Response.Write "Folder does not exist."End If
3. Create Folder
Dim folderPathfolderPath = Server.MapPath("uploads")If Not fso.FolderExists(folderPath) Then fso.CreateFolder(folderPath) Response.Write "Folder created."Else Response.Write "Folder already exists."End If
4. Delete Folder
Dim folderPathfolderPath = Server.MapPath("oldfolder")If fso.FolderExists(folderPath) Then fso.DeleteFolder(folderPath) Response.Write "Folder deleted."Else Response.Write "Folder does not exist."End If
5. Create and Write to a File
Dim filePath, filefilePath = Server.MapPath("data.txt")Set file = fso.CreateTextFile(filePath, True) ' True = overwrite if existsfile.WriteLine "Hello from ASP FileSystemObject!"file.CloseSet file = Nothing
6. Read from a File
Dim filePath, file, contentfilePath = Server.MapPath("data.txt")If fso.FileExists(filePath) Then Set file = fso.OpenTextFile(filePath, 1) ' 1 = ForReading content = file.ReadAll file.Close Set file = Nothing Response.Write Server.HTMLEncode(content)Else Response.Write "File not found."End If
7. List Files in a Folder
Dim folderPath, folder, filefolderPath = Server.MapPath("uploads")If fso.FolderExists(folderPath) Then Set folder = fso.GetFolder(folderPath) For Each file In folder.Files Response.Write "File: " & file.Name & "<br>" Next Set folder = NothingElse Response.Write "Folder not found."End If
8. Get File or Folder Properties
Dim filePath, filefilePath = Server.MapPath("data.txt")If fso.FileExists(filePath) Then Set file = fso.GetFile(filePath) Response.Write "File size: " & file.Size & " bytes<br>" Response.Write "Date created: " & file.DateCreated & "<br>" Response.Write "Date last modified: " & file.DateLastModified & "<br>" Set file = NothingEnd If
Summary
Use FileSystemObject for file/folder management.
Always use
Server.MapPath
to map virtual paths to physical paths.Handle errors carefully — files may be locked or permissions restricted.
Remember to release objects (
Set obj = Nothing
).
If you want, I can help with more advanced examples like:
File upload handling
Reading/writing binary files
Recursive folder scanning
Error handling best practices for file operations
Just ask!