ASP — File System
FileSystemObject
<%
Set fso = Server.CreateObject("Scripting.FileSystemObject")
%>
Read File
<%
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set file = fso.OpenTextFile(Server.MapPath("/data.txt"), 1)
Do While Not file.AtEndOfStream
line = file.ReadLine
Response.Write(line & "<br>")
Loop
file.Close
Set file = Nothing
Set fso = Nothing
%>
Write File
<%
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set file = fso.CreateTextFile(Server.MapPath("/output.txt"), True)
file.WriteLine("Hello, World!")
file.WriteLine("Second line")
file.Close
Set file = Nothing
Set fso = Nothing
%>
Check File Exists
<%
Set fso = Server.CreateObject("Scripting.FileSystemObject")
If fso.FileExists(Server.MapPath("/data.txt")) Then
Response.Write("File exists")
Else
Response.Write("File not found")
End If
Set fso = Nothing
%>
File Properties
<%
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set file = fso.GetFile(Server.MapPath("/data.txt"))
Response.Write("Size: " & file.Size)
Response.Write("Created: " & file.DateCreated)
Response.Write("Modified: " & file.DateLastModified)
Set file = Nothing
Set fso = Nothing
%>
Mini Practice
- Read a file
- Write a file
- Check if file exists
- Get file properties
Up Next
Continue with Database — database operations.
Related Topics
Frequently Asked Questions about File System
What is File System in ASP?
File System is a fundamental concept in ASP. This lesson explains it step by step with clear examples, making it easy for beginners to understand.
How do I learn File System?
Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn File System.
Why is File System important in ASP?
File System is essential for ASP development. Understanding this concept will help you write better code and solve real-world problems more effectively.