๐๏ธ ASP โ File System API: Drive, TextStream Objects Explained
Classic ASP provides the FileSystemObject (FSO) to interact with the serverโs file system. Using this powerful API, developers can read, write, create, delete, and inspect drives, folders, and text files. Two of the most commonly used objects in this API are:
Drive: Used to get drive-level information (C:, D:, etc.)TextStream: Used for reading and writing text files
This article walks you through how to use both effectively in Classic ASP.
๐ง Introduction to FileSystemObject
The FileSystemObject (FSO) is a COM component used in Classic ASP via VBScript to perform file I/O operations on the server.
โ How to Create FSO:
<%
Set fso = Server.CreateObject("Scripting.FileSystemObject")
%>
Key Components of FSO:
- Drives, Drive
- Folder, File
- TextStream
๐ฝ Working with Drive Object
The Drive object provides information about physical drives on the server.
๐ Example: Accessing Drive Info
<%
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set drive = fso.GetDrive("C:")
Response.Write "Drive Letter: " & drive.DriveLetter & "<br>"
Response.Write "Drive Type: " & drive.DriveType & "<br>"
Response.Write "Free Space: " & drive.FreeSpace & "<br>"
Response.Write "Total Size: " & drive.TotalSize & "<br>"
Response.Write "Volume Name: " & drive.VolumeName & "<br>"
%>
๐งพ Output Might Be:
Drive Letter: C
Drive Type: 2 ' (2 = Fixed Drive)
Free Space: 4294967296
Total Size: 10737418240
Volume Name: OS_Drive
๐ง Note: DriveType returns an integer (0 = Unknown, 1 = Removable, 2 = Fixed, 3 = Network, etc.)
๐ Reading Files with TextStream
To read a file, you use the TextStream object returned by the OpenTextFile method.
๐ Example: Reading Line by Line
<%
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile("C:\data\info.txt", 1) ' 1 = ForReading
Do While Not file.AtEndOfStream
Response.Write file.ReadLine & "<br>"
Loop
file.Close
%>
๐ Modes:
1โ ForReading2โ ForWriting8โ ForAppending
โ๏ธ Writing to Files with TextStream
You can create or write to files using FSO and TextStream.
โ๏ธ Example: Writing to a File
<%
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set file = fso.CreateTextFile("C:\data\output.txt", True)
file.WriteLine("Hello, world!")
file.WriteLine("Welcome to ASP File System.")
file.Close
%>
๐ The second parameter of CreateTextFile(path, overwrite) allows overwriting if set to True.
๐งพ Example: Appending to File
Set file = fso.OpenTextFile("C:\data\output.txt", 8, True)
file.WriteLine("New line added.")
file.Close
๐งช Full ASP Example Using Drive & TextStream
<%
Set fso = Server.CreateObject("Scripting.FileSystemObject")
' Get drive info
Set drive = fso.GetDrive("C:")
Response.Write "<h3>Drive Info:</h3>"
Response.Write "Free Space: " & drive.FreeSpace & "<br>"
' Write to a new file
Set writer = fso.CreateTextFile("C:\data\report.txt", True)
writer.WriteLine("Report Generated")
writer.WriteLine("Drive Free Space: " & drive.FreeSpace)
writer.Close
' Read and display the file content
Set reader = fso.OpenTextFile("C:\data\report.txt", 1)
Response.Write "<h3>Report Contents:</h3>"
Do While Not reader.AtEndOfStream
Response.Write reader.ReadLine & "<br>"
Loop
reader.Close
%>
๐ Common Errors and Best Practices
| โ ๏ธ Issue | ๐ก Solution |
|---|---|
| Permission Denied | Ensure IIS has write access to the directory |
| Path Not Found | Use absolute path (C:\...), not virtual path |
| File Not Found | Check file existence with fso.FileExists(path) |
| Script Timeout | Avoid large files; increase timeout if needed |
| Security Risk | Never expose full paths to user input |
โ Summary
- FileSystemObject lets you manipulate files and folders using VBScript in Classic ASP.
- Use the Drive object to get storage information.
- Use the TextStream object to read and write text files.
- Always handle exceptions and file access carefully on a live server.
โ FAQs
Q1. Can I use FileSystemObject in ASP.NET?
Yes, but ASP.NET offers better classes like System.IO.File and Directory.
Q2. How can I check if a file exists before reading?
Use:
If fso.FileExists("C:\data\file.txt") Then
' Proceed to open
End If
Q3. How do I append instead of overwrite a file?
Open the file with mode 8 (ForAppending).
Q4. Is FileSystemObject secure?
Itโs powerful but dangerous if mishandled. Never let users supply paths directly.
Share Now :
