ASP File System Object FSO
Syntax
FileSystemObject.OpenTextFile(fname,mode,create,format)
| Parameter |
Description |
| fname |
Required. The name of the file to open |
| mode |
Optional. How to open the file
1=ForReading - Open a file for reading. You cannot write to
this file.
2=ForWriting - Open a file for writing.
8=ForAppending - Open a file and write to the end of the file. |
| create |
Optional. Sets whether a new file can be
created if the filename does not exist. True indicates that a
new file can be created, and False indicates that a new file
will not be created. False is default |
| format |
Optional. The format of the file
0=TristateFalse - Open the file as ASCII. This is default.
-1=TristateTrue - Open the file as Unicode.
-2=TristateUseDefault - Open the file using the system default. |
|
The following code creates a text file (c:\test.txt) and then writes some
text to the file:
Change the file name from c:\test.txt to your virtual path in a shared
hosting environment; use this code as your file path instead, server.mappath("/test.txt").
<%
dim fs,fname
set fs=Server.CreateObject("Scripting.FileSystemObject")
set fname=fs.CreateTextFile("c:\test.txt",true)
'change to virtual path in shared hosting environment
fname.WriteLine("Hello World!")
fname.Close
set fname=nothing
set fs=nothing
%> |
The following code is to check if a file exists.
<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
If (fs.FileExists(server.mappath("/test.txt")))=true Then
Response.Write("File exists.")
Else
Response.Write("File does not exist.")
End If
set fs=nothing
%> |
The following code is to get a files extension.
<%
Set fs=Server.CreateObject("Scripting.FileSystemObject")
Response.Write("The file extension of the file is: ")
Response.Write(fs.GetExtensionName(server.mappath("/test.txt")))
set fs=nothing
%> |
The following code is to read from a file.
<%
' create the fso object
set fso = Server.Createobject("Scripting.FileSystemObject")
path = server.mappath("/test.txt")
' open the file
set file = fso.opentextfile(path, 1) <-- For reading
do until file.AtEndOfStream
Response.write("Name: " & file.ReadLine & " ")
Response.write("Home Page: " & file.ReadLine & " ")
Response.write("Email: " & file.ReadLine & "<p>")
loop
' close and clean up
file.close
set file = nothing
set fso = nothing
%> |
|