Chapter 52: ASP TextStream Object
1. What is the TextStream Object?
TextStream is not created directly with Server.CreateObject.
You get a TextStream object when you call one of these methods on a FileSystemObject:
- fso.OpenTextFile(path, mode, create, format)
- fso.CreateTextFile(path, overwrite, unicode)
It represents an open text file stream — you can:
- Read the entire file at once (.ReadAll)
- Read line by line (.ReadLine, .Read(n))
- Write text (.Write, .WriteLine)
- Skip lines (.Skip, .SkipLine)
- Check end of file (.AtEndOfStream)
- Close it when done (.Close)
Important modes (second parameter of OpenTextFile):
| Mode constant | Value | Meaning | Typical use |
|---|---|---|---|
| ForReading | 1 | Open for reading only | Read config, logs, data files |
| ForWriting | 2 | Open for writing (overwrites) | Create new log, report, export |
| ForAppending | 8 | Open for writing (adds to end) | Append to log file (most common) |
Unicode/ASCII flag (fourth parameter of OpenTextFile):
- True = Unicode (UTF-16)
- False = ASCII/ANSI (default, most common in old ASP)
2. Step-by-Step Example 1 – Read Entire File & Display It
|
0 1 2 3 4 5 6 |
<%@ Language=VBScript %> <% Option Explicit %> <!DOCTYPE html> <html> <body> <h2>Read Entire Text File with TextStream</h2> <% Dim fso, ts, path, content Set fso = Server.CreateObject("Scripting.FileSystemObject") path = Server.MapPath("data/welcome.txt") If fso.FileExists(path) Then ' Open for reading (mode 1) Set ts = fso.OpenTextFile(path, 1, False, False) ' ASCII, no create ' Read everything at once content = ts.ReadAll ts.Close ' Safe output Response.Write "<pre>" & Server.HTMLEncode(content) & "</pre>" Else Response.Write "<p style='color:red;'>File not found: " & Server.HTMLEncode(path) & "</p>" End If Set ts = Nothing Set fso = Nothing %> </body> </html> |
welcome.txt content:
|
0 1 2 3 4 5 6 7 8 9 |
Welcome to Webliance! Last updated: February 2026 Visitor count: 1,234 Special offer: Biryani ₹399 today! |
→ Browser shows the file content safely escaped inside <pre>.
3. Example 2 – Read Line by Line (Very Common for CSV/Logs)
|
0 1 2 3 4 5 6 |
<% Dim fso, ts, path, lineNum lineNum = 0 Set fso = Server.CreateObject("Scripting.FileSystemObject") path = Server.MapPath("data/visits.log") If fso.FileExists(path) Then Set ts = fso.OpenTextFile(path, 1) ' ForReading Response.Write "<table border='1'>" Response.Write "<tr><th>Line #</th><th>Entry</th></tr>" Do While Not ts.AtEndOfStream lineNum = lineNum + 1 Dim line line = ts.ReadLine Response.Write "<tr>" Response.Write "<td>" & lineNum & "</td>" Response.Write "<td>" & Server.HTMLEncode(line) & "</td>" Response.Write "</tr>" Loop Response.Write "</table>" ts.Close Else Response.Write "<p>No log file found.</p>" End If Set ts = Nothing Set fso = Nothing %> |
→ This is how many old sites displayed log files or simple CSV imports.
4. Example 3 – Write / Append to Log File (Most Common Real Use)
|
0 1 2 3 4 5 6 |
<% Dim fso, ts, logPath, logEntry logEntry = Now() & vbTab & Request.ServerVariables("REMOTE_ADDR") & vbTab & _ "Visited page: " & Request.ServerVariables("SCRIPT_NAME") Set fso = Server.CreateObject("Scripting.FileSystemObject") logPath = Server.MapPath("logs/visits.log") ' 8 = ForAppending, True = create if not exists Set ts = fso.OpenTextFile(logPath, 8, True) ts.WriteLine logEntry ts.Close Response.Write "<p>Visit logged successfully.</p>" Set ts = Nothing Set fso = Nothing %> |
→ Every page hit appends one line — classic simple analytics/logging pattern.
5. Security & Best Practice Warnings (Critical in 2026)
- Never allow users to control the path passed to OpenTextFile or CreateTextFile — classic ASP sites were hacked constantly via path traversal (../../etc/passwd) or writing malicious files to web folders.
- Disable write permissions to web folders in IIS if possible — many hosts block FSO write access now.
- Always use Server.MapPath — never hard-code physical paths (C:\inetpub\…).
- Always close TextStream (ts.Close) and set to Nothing.
- Use On Error Resume Next around file operations + check Err.Number.
- Log FSO errors — disk full, permission denied, etc. were common silent failures.
- Never expose raw file content without Server.HTMLEncode — even log files can contain user input.
6. Teacher Summary – ASP TextStream Object in Classic ASP
ASP TextStream Object is:
- Created by fso.OpenTextFile(path, mode, create) or fso.CreateTextFile(path)
- Represents an open text file for reading or writing
- Main methods: .ReadAll, .ReadLine, .Write, .WriteLine, .Close, .AtEndOfStream
- Modes: 1=ForReading, 2=ForWriting, 8=ForAppending
- Used for: reading config/logs/CSV, writing logs/reports/exports, appending visit data
- Always use Server.MapPath for path
- AlwaysServer.HTMLEncode when displaying file content
- Always close stream (ts.Close) and set Nothing
- Very powerful — but extremely dangerous if write access is not locked down
This is how millions of Classic ASP sites read config files, wrote logs, generated reports, imported CSV, and appended audit trails — and many legacy Indian systems still use exactly this TextStream pattern in 2026.
Next class?
- Want a full example combining FSO + TextStream + upload form?
- Or how to read CSV line by line and parse into table?
- Or secure FSO/TextStream usage (permissions, path validation)?
- Or move to the next W3Schools topic (ASP Cookies or ASP Sessions)?
Just tell me — I’m here! 🚀🇮🇳 Keep learning strong, Webliance! 😊
