Asp Folder in ASP
In Classic ASP, working with folders is done via the FileSystemObject (FSO). You can create, delete, check existence, and list folders and their contents.
Using FSO to Work with Folders in ASP
1. Create the FileSystemObject
<%Dim fsoSet fso = Server.CreateObject("Scripting.FileSystemObject")%>2. Check if a Folder Exists
Dim folderPathfolderPath = Server.MapPath("uploads")If fso.FolderExists(folderPath) Then Response.Write "Folder exists."Else Response.Write "Folder does not exist."End If3. Create a 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 If4. Delete a 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 If5. Get Folder Properties
Dim folderPath, folderfolderPath = Server.MapPath("uploads")If fso.FolderExists(folderPath) Then Set folder = fso.GetFolder(folderPath) Response.Write "Folder Name: " & folder.Name & "<br>" Response.Write "Date Created: " & folder.DateCreated & "<br>" Response.Write "Date Last Modified: " & folder.DateLastModified & "<br>" Response.Write "Size (bytes): " & folder.Size & "<br>" Response.Write "Parent Folder: " & folder.ParentFolder & "<br>" Set folder = NothingElse Response.Write "Folder not found."End If6. 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 If7. List Subfolders in a Folder
Dim folderPath, folder, subfolderfolderPath = Server.MapPath("uploads")If fso.FolderExists(folderPath) Then Set folder = fso.GetFolder(folderPath) For Each subfolder In folder.SubFolders Response.Write "Subfolder: " & subfolder.Name & "<br>" Next Set folder = NothingElse Response.Write "Folder not found."End IfClean up:
Set fso = NothingNotes:
Always use
Server.MapPathto convert a virtual path (URL path) to a physical path on the server.Be cautious when deleting folders — ensure you are not deleting important data.
Folder size is cumulative size of all files in the folder.
If you want me to help with recursive folder listing, file upload to folder, or error handling with folders, just say!