๐Ÿงฐ Common ASP References (Shared)
Estimated reading: 4 minutes 65 views

๐Ÿ—‚๏ธ 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 โ€“ ForReading
  • 2 โ€“ ForWriting
  • 8 โ€“ 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 DeniedEnsure IIS has write access to the directory
Path Not FoundUse absolute path (C:\...), not virtual path
File Not FoundCheck file existence with fso.FileExists(path)
Script TimeoutAvoid large files; increase timeout if needed
Security RiskNever 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿ“‚ ASP โ€“ File System API: Drive, TextStream

Or Copy Link

CONTENTS
Scroll to Top