Chapter 54: ASP File Object
ASP File Object
This topic usually appears right after ASP TextStream in the W3Schools Classic ASP files reference, and it is the object you use when you need metadata about a specific file — its name, size, creation date, last modified date, attributes (read-only? hidden?), path, short name, etc.
I will explain it like your favorite teacher who actually used File objects in production Classic ASP sites (file browsers, log rotation, upload validation, report generation, cleanup scripts) — slowly, clearly, with real working examples, good habits, common mistakes, security warnings (very important!), and the exact patterns you would see in legacy Indian business/intranet/ERP/government code that still runs this way in 2026.
1. What is the File Object?
The File object is not created directly with Server.CreateObject.
You get a File object in one of two ways:
- From FileSystemObject.GetFile(path) — most common
- From Folder.Files collection (when looping over files in a folder)
It represents one single file on disk and gives you read-only information about it:
- Name, path, short name (8.3 format)
- Size (in bytes)
- Dates (created, last accessed, last modified)
- Attributes (read-only, hidden, system, archive, etc.)
- Drive letter it lives on
Important: File object is read-only — you cannot rename, delete, copy, or modify the file through the File object itself. For those operations you use methods on the FileSystemObject (fso.DeleteFile, fso.CopyFile, fso.MoveFile).
2. How to Get a File Object
Way 1 – Most common: fso.GetFile(path)
|
0 1 2 3 4 5 6 |
Dim fso, file, path Set fso = Server.CreateObject("Scripting.FileSystemObject") path = Server.MapPath("data/report.pdf") If fso.FileExists(path) Then Set file = fso.GetFile(path) Response.Write "<p>File name: " & Server.HTMLEncode(file.Name) & "</p>" Response.Write "<p>Size: " & FormatNumber(file.Size / 1024, 2) & " KB</p>" Response.Write "<p>Created: " & file.DateCreated & "</p>" Response.Write "<p>Last modified: " & file.DateLastModified & "</p>" Else Response.Write "<p>File not found.</p>" End If Set file = Nothing Set fso = Nothing |
Way 2 – Loop over all files in a folder
|
0 1 2 3 4 5 6 |
Dim fso, folder, file Set fso = Server.CreateObject("Scripting.FileSystemObject") Set folder = fso.GetFolder(Server.MapPath("uploads/")) Response.Write "<h3>Files in uploads folder:</h3>" Response.Write "<table border='1'>" Response.Write "<tr><th>Name</th><th>Size (KB)</th><th>Modified</th></tr>" For Each file In folder.Files Response.Write "<tr>" Response.Write "<td>" & Server.HTMLEncode(file.Name) & "</td>" Response.Write "<td>" & Round(file.Size / 1024, 2) & "</td>" Response.Write "<td>" & file.DateLastModified & "</td>" Response.Write "</tr>" Next Response.Write "</table>" Set file = Nothing Set folder = Nothing Set fso = Nothing |
3. All Important Properties of File Object
| Property | What it returns | Example output (typical) | Notes / Use case |
|---|---|---|---|
| file.Name | File name with extension | “invoice_2026.pdf” | Display name |
| file.Path | Full physical path | “C:\inetpub\wwwroot\uploads\invoice.pdf” | Logging/debug |
| file.ShortName | 8.3 short name (old DOS format) | “INVOIC~1.PDF” | Rare now |
| file.Size | Size in bytes | 245760 | Show KB/MB |
| file.DateCreated | Creation date/time | 2025-12-01 14:30:22 | Audit trail |
| file.DateLastAccessed | Last time file was read | 2026-02-16 17:12:45 | Usage stats |
| file.DateLastModified | Last time file was changed | 2026-02-15 09:45:10 | Most useful date |
| file.Attributes | Bit flags (read-only, hidden, system, etc.) | 32 (archive) or 1 (read-only) | Check permissions |
| file.Drive | Drive letter (as string) | “C:” | Combine with Drive object |
| file.ParentFolder | Parent Folder object | Folder object of containing folder | Navigate up |
| file.ShortPath | Short 8.3 path | “C:\inetpub\wwwr~1\uploads\INVOIC~1.PDF” | Rare |
| file.Type | File type description | “Adobe Acrobat Document” | Display icon hint |
4. Real-World Example – Simple Admin File Browser
|
0 1 2 3 4 5 6 |
<%@ Language=VBScript %> <% Option Explicit %> <!DOCTYPE html> <html> <body> <h2>Files in /uploads/ folder</h2> <table border="1" cellpadding="5"> <tr> <th>Name</th> <th>Size (KB)</th> <th>Modified</th> <th>Attributes</th> </tr> <% Dim fso, folder, file Set fso = Server.CreateObject("Scripting.FileSystemObject") Set folder = fso.GetFolder(Server.MapPath("uploads")) For Each file In folder.Files Dim attr attr = "" If file.Attributes And 1 Then attr = attr & "Read-only " If file.Attributes And 2 Then attr = attr & "Hidden " If file.Attributes And 4 Then attr = attr & "System " If file.Attributes And 32 Then attr = attr & "Archive" Response.Write "<tr>" Response.Write "<td>" & Server.HTMLEncode(file.Name) & "</td>" Response.Write "<td align='right'>" & Round(file.Size / 1024, 2) & "</td>" Response.Write "<td>" & file.DateLastModified & "</td>" Response.Write "<td>" & attr & "</td>" Response.Write "</tr>" Next Set file = Nothing Set folder = Nothing Set fso = Nothing %> </table> </body> </html> |
5. Security & Best Practice Warnings (Critical in 2026)
- Never allow users to supply the path to GetFile or GetFolder — path traversal attacks (../../windows/system32/config/sam) were extremely common
- Disable write/delete permissions on web folders — many hacks used FSO to write webshells
- Never expose physical paths (file.Path) to users — only show file.Name
- Use On Error Resume Next around FSO calls + check Err.Number
- Close nothing — File object has no .Close (unlike TextStream)
- Log access — file browsing was often abused
- In 2026 legacy code → still used for admin tools, but very risky without strict permissions
6. Teacher Summary – ASP File Object in Classic ASP
ASP File Object is:
- A read-only metadata object you get from fso.GetFile(path) or folder.Files collection
- Represents one file on disk
- Main properties: Name, Path, Size, DateCreated, DateLastModified, DateLastAccessed, Attributes, Drive, ParentFolder
- Used for: file listings, admin browsers, size/date display, validation before download
- No read/write methods — use TextStream for content
- No delete/copy/move — use fso.DeleteFile / fso.CopyFile / fso.MoveFile
- Always check fso.FileExists(path) first
- Always use Server.MapPath
- AlwaysServer.HTMLEncode(file.Name) when displaying
- Very useful in admin tools — almost never shown to regular users
This is how serious Classic ASP sites listed files, showed sizes/dates, and built simple admin interfaces — and many legacy Indian systems still use exactly this File object pattern in 2026.
Next class?
- Want a full admin file browser example (list + download link + delete button)?
- Or how to combine File + TextStream (preview file content)?
- Or secure FSO/File usage (permissions, path validation)?
- Or move to the next W3Schools topic (ASP TextStream again or ASP Cookies)?
Just tell me — I’m here! 🚀🇮🇳 Keep learning strong, Webliance! 😊
